teacher-tools/api/source/app.d

54 lines
1.6 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;
2024-12-28 17:28:07 +00:00
import std.process;
2024-12-16 22:20:15 +00:00
2025-01-22 17:55:14 +00:00
static import api_modules.auth;
2025-01-29 19:29:59 +00:00
static import api_modules.announcement;
2025-01-23 17:10:32 +00:00
static import api_modules.classroom_compliance.api;
2024-12-16 20:04:42 +00:00
void main() {
2024-12-28 17:28:07 +00:00
string env = environment.get("TEACHER_TOOLS_API_ENV", "DEV");
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";
2024-12-28 17:28:07 +00:00
if (env == "PROD") {
config.port = 8107;
config.workerPoolSize = 5;
}
2024-12-16 22:20:15 +00:00
PathHandler handler = new PathHandler();
handler.addMapping(Method.OPTIONS, "/api/**", &optionsEndpoint);
2025-01-29 19:29:59 +00:00
if (env == "DEV") {
handler.addMapping("/api/initialize-schema", &initializeSchemaEndpoint);
handler.addMapping("/api/generate-sample-data", &sampleDataEndpoint);
}
2025-01-22 17:55:14 +00:00
api_modules.auth.registerApiEndpoints(handler);
2025-01-29 19:29:59 +00:00
api_modules.announcement.registerApiEndpoints(handler);
2025-01-23 17:10:32 +00:00
api_modules.classroom_compliance.api.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;
}
2025-01-29 19:29:59 +00:00
void initializeSchemaEndpoint(ref HttpRequestContext ctx) {
import db : initializeSchema;
initializeSchema();
}
void sampleDataEndpoint(ref HttpRequestContext ctx) {
import sample_data : insertSampleData;
insertSampleData();
}