Added current project files
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
import html.Document;
|
||||
import tink.http.containers.*;
|
||||
import tink.http.Response;
|
||||
import tink.web.routing.*;
|
||||
import haxe.Json;
|
||||
|
||||
|
||||
|
||||
typedef T_project = {
|
||||
name : String,
|
||||
description : String,
|
||||
url : String
|
||||
}
|
||||
typedef T_projects = {
|
||||
version : Int,
|
||||
projects : Array<T_project>
|
||||
}
|
||||
|
||||
class Server {
|
||||
static function main() {
|
||||
var container = new NodeContainer(8080);
|
||||
//var container = PhpContainer.inst; //use PhpContainer instead of NodeContainer when targeting PHP
|
||||
var router = new Router<Root>(new Root());
|
||||
container.run(function(req) {
|
||||
return router.route(Context.ofRequest(req))
|
||||
.recover(OutgoingResponse.reportError);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class Root {
|
||||
public function new() {}
|
||||
|
||||
|
||||
@:get('/')
|
||||
@:produces('text/html')
|
||||
public function root(){
|
||||
return Document.render();
|
||||
}
|
||||
|
||||
@:sub('/projects')
|
||||
public function projects()
|
||||
return new Projects();
|
||||
|
||||
@:get('/hello')
|
||||
@:produces('application/json')
|
||||
public function hello(name = 'world'){
|
||||
var greeting = { hello: name, foo: 42 };
|
||||
var strout:String = tink.Json.stringify(greeting);
|
||||
return strout;
|
||||
}
|
||||
}
|
||||
|
||||
class Projects {
|
||||
public function new() {}
|
||||
|
||||
public var path:String = "./res/json/projects.json";
|
||||
|
||||
@:get('/')
|
||||
@:produces('application/json')
|
||||
public function projects(){
|
||||
var json:String = Utils.getJson(path);
|
||||
return json;
|
||||
}
|
||||
@:get('/project/by_name/$name')
|
||||
@:produces('application/json')
|
||||
public function project(name:String){
|
||||
var json:String = Utils.getJson(path);
|
||||
var _projects:T_projects = Json.parse(json);
|
||||
json = "{}";
|
||||
for(_project in _projects.projects){
|
||||
if(_project.name == name){
|
||||
json = tink.Json.stringify(_project);
|
||||
}
|
||||
}
|
||||
return json;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user