teacher-tools/api/source/app.d

38 lines
1.1 KiB
D
Raw Normal View History

2024-12-16 20:04:42 +00:00
import handy_httpd;
2024-12-16 22:20:15 +00:00
import handy_httpd.handlers.path_handler;
import std.stdio;
import d2sqlite3;
import db;
import api_modules.auth;
static import api_modules.classroom_compliance;
2024-12-16 20:04:42 +00:00
void main() {
// Initialize the database on startup.
auto db = getDb();
db.close();
2024-12-16 22:20:15 +00:00
ServerConfig config;
config.enableWebSockets = false;
config.port = 8080;
config.workerPoolSize = 3;
config.defaultHeaders["Access-Control-Allow-Origin"] = "*";
config.defaultHeaders["Access-Control-Allow-Methods"] = "*";
config.defaultHeaders["Access-Control-Request-Method"] = "*";
config.defaultHeaders["Access-Control-Allow-Headers"] = "Authorization, Content-Length, Content-Type";
PathHandler handler = new PathHandler();
handler.addMapping(Method.OPTIONS, "/api/**", &optionsEndpoint);
handler.addMapping(Method.POST, "/api/auth/login", &loginEndpoint);
api_modules.classroom_compliance.registerApiEndpoints(handler);
2024-12-16 22:20:15 +00:00
HttpServer server = new HttpServer(handler, config);
2024-12-16 20:04:42 +00:00
server.start();
}
2024-12-16 22:20:15 +00:00
void optionsEndpoint(ref HttpRequestContext ctx) {
ctx.response.status = HttpStatus.OK;
}