2023-07-17 19:10:58 +00:00
|
|
|
module csgs.http;
|
|
|
|
|
|
|
|
import handy_httpd;
|
|
|
|
import handy_httpd.handlers.path_delegating_handler;
|
|
|
|
import handy_httpd.handlers.file_resolving_handler;
|
|
|
|
import slf4d;
|
|
|
|
import std.path;
|
|
|
|
|
|
|
|
import csgs.extract;
|
|
|
|
|
|
|
|
void startServer() {
|
|
|
|
ServerConfig config = ServerConfig.defaultValues();
|
|
|
|
config.workerPoolSize = 3;
|
|
|
|
config.connectionQueueSize = 10;
|
|
|
|
config.port = 8100;
|
|
|
|
|
|
|
|
PathDelegatingHandler handler = new PathDelegatingHandler();
|
2023-07-28 18:57:00 +00:00
|
|
|
handler.addMapping(Method.GET, "/extracts", &handleExtract);
|
|
|
|
handler.addMapping(Method.GET, "/extracts/{extractId}", &getExtract);
|
|
|
|
handler.addMapping(Method.POST, "/item-reports", &handleItemReport);
|
|
|
|
handler.addMapping(Method.GET, "/item-reports", &getItemReports);
|
|
|
|
handler.addMapping(Method.GET, "/status", (ref HttpRequestContext ctx) {
|
2023-07-23 15:40:07 +00:00
|
|
|
ctx.response.setStatus(HttpStatus.OK);
|
|
|
|
ctx.response.writeBodyString("online");
|
|
|
|
});
|
2023-07-17 19:10:58 +00:00
|
|
|
|
|
|
|
FileResolvingHandler fileHandler = new FileResolvingHandler("site", DirectoryResolutionStrategies.serveIndexFiles);
|
|
|
|
handler.addMapping("/**", fileHandler);
|
|
|
|
new HttpServer(handler, config).start();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleExtract(ref HttpRequestContext ctx) {
|
|
|
|
import std.json;
|
|
|
|
|
|
|
|
MultipartFormData data = ctx.request.readBodyAsMultipartFormData();
|
|
|
|
if (!validateExtractRequest(data, ctx.response)) return;
|
|
|
|
string extractId = doExtract(data);
|
2023-07-28 18:57:00 +00:00
|
|
|
JSONValue result = JSONValue(string[string].init);
|
2023-07-17 19:10:58 +00:00
|
|
|
result.object["extractId"] = JSONValue(extractId);
|
|
|
|
ctx.response.writeBodyString(result.toJSON(), "application/json");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void getExtract(ref HttpRequestContext ctx) {
|
|
|
|
string extractId = ctx.request.getPathParamAs!string("extractId");
|
|
|
|
const extractFile = buildPath(EXTRACTS_DIR, extractId ~ ".json");
|
|
|
|
fileResponse(ctx.response, extractFile, "application/json");
|
2023-07-24 13:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void handleItemReport(ref HttpRequestContext ctx) {
|
|
|
|
import std.string : strip;
|
|
|
|
import std.stdio;
|
|
|
|
|
|
|
|
string itemName = ctx.request.readBodyAsString().strip();
|
|
|
|
File f = File("item-reports.txt", "a");
|
|
|
|
f.writeln(itemName);
|
|
|
|
f.close();
|
|
|
|
}
|
2023-07-28 18:57:00 +00:00
|
|
|
|
|
|
|
private void getItemReports(ref HttpRequestContext ctx) {
|
|
|
|
import handy_httpd.components.responses : fileResponse;
|
|
|
|
fileResponse(ctx.response, "item-reports.txt", "text/plain");
|
|
|
|
}
|