121 lines
3.1 KiB
Haxe
121 lines
3.1 KiB
Haxe
import html.WebDocument;
|
|
import tink.http.containers.*;
|
|
import tink.http.Response;
|
|
import tink.web.routing.*;
|
|
import tink.sql.drivers.Sqlite;
|
|
import db.Db;
|
|
import haxe.Json;
|
|
|
|
import model.MUser;
|
|
|
|
import controller.IDGen;
|
|
import controller.User;
|
|
|
|
typedef T_project = {
|
|
name : String,
|
|
description : String,
|
|
url : String
|
|
}
|
|
typedef T_projects = {
|
|
version : Int,
|
|
projects : Array<T_project>
|
|
}
|
|
|
|
class Server {
|
|
@await public static var db:Db;
|
|
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());
|
|
Db.createTables();
|
|
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 "yeet";
|
|
return WebDocument.render();
|
|
}
|
|
|
|
// @:get('/test')
|
|
// @:produces('text/html')
|
|
// @async
|
|
// public function create(){
|
|
// //return "yeet";
|
|
// var yeet = "yote";
|
|
// var driver = new tink.sql.drivers.Sqlite();
|
|
// @await var db = new Db('daba2', driver);
|
|
// @await var one = db.MUser.create();
|
|
// @await var two = db.MUser.insertOne({
|
|
// id: cast null,
|
|
// name: 'Alice',
|
|
// email: 'alice@example.com',
|
|
// password: 'jew'
|
|
// });
|
|
// @await var three = db.MUser.select({name: MUser.name, email: MUser.email}).where(MUser.email == 'alice@example.com').first().next(function(row) {
|
|
// trace(row.name);return "";
|
|
// });
|
|
// // trace(@await one);
|
|
// // trace(@await two);
|
|
// trace(@await three);
|
|
// return '$yeet';
|
|
// }
|
|
|
|
@:sub('/user')
|
|
@:produces('application/json')
|
|
public function user()
|
|
return new User();
|
|
|
|
@:sub('/projects')
|
|
@:produces('application/json')
|
|
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;
|
|
}
|
|
|
|
@:sub('/idgen')
|
|
@:produces('application/json')
|
|
public function idGen()
|
|
return new IDGen();
|
|
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|