drivebyapi/api/src/Server.hx
2022-09-29 10:21:36 +02:00

85 lines
2.2 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 model.MUser;
import controller.IDGen;
import controller.User;
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 "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();
}