2023-07-15 22:22:48 +00:00
|
|
|
import handy_httpd;
|
|
|
|
import handy_httpd.handlers.path_delegating_handler;
|
|
|
|
import handy_httpd.handlers.file_resolving_handler;
|
|
|
|
import slf4d;
|
|
|
|
import slf4d.default_provider;
|
2023-07-16 16:27:22 +00:00
|
|
|
import std.path;
|
|
|
|
import std.file;
|
2023-07-17 00:58:16 +00:00
|
|
|
import std.json;
|
2023-07-16 16:27:22 +00:00
|
|
|
|
|
|
|
const EXTRACTS_DIR = "extracts";
|
|
|
|
const EXTRACT_FILENAME = "__EXTRACT__.json";
|
|
|
|
const EXTRACT_COMMAND = ["java", "-jar", "materials-extractor-v1.0.0.jar"];
|
2023-07-15 22:22:48 +00:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
auto provider = new shared DefaultProvider(true, Levels.INFO);
|
|
|
|
configureLoggingProvider(provider);
|
|
|
|
|
|
|
|
ServerConfig config = ServerConfig.defaultValues();
|
|
|
|
config.workerPoolSize = 3;
|
|
|
|
config.connectionQueueSize = 10;
|
|
|
|
config.port = 8100;
|
|
|
|
|
|
|
|
PathDelegatingHandler handler = new PathDelegatingHandler();
|
|
|
|
handler.addMapping("POST", "/extracts", &handleExtract);
|
2023-07-16 16:27:22 +00:00
|
|
|
handler.addMapping("GET", "/extracts/{extractId}", &getExtract);
|
2023-07-15 22:22:48 +00:00
|
|
|
|
|
|
|
FileResolvingHandler fileHandler = new FileResolvingHandler("site", DirectoryResolutionStrategies.serveIndexFiles);
|
|
|
|
handler.addMapping("/**", fileHandler);
|
|
|
|
new HttpServer(handler, config).start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleExtract(ref HttpRequestContext ctx) {
|
|
|
|
import std.json;
|
2023-07-16 16:27:22 +00:00
|
|
|
import std.uuid;
|
|
|
|
import std.process;
|
|
|
|
import std.stdio;
|
|
|
|
|
|
|
|
immutable UUID extractId = randomUUID();
|
|
|
|
MultipartFormData data = ctx.request.readBodyAsMultipartFormData();
|
2023-07-17 00:58:16 +00:00
|
|
|
if (!validateExtractRequest(data, ctx.response)) return;
|
2023-07-16 16:27:22 +00:00
|
|
|
const extractDir = buildPath(EXTRACTS_DIR, extractId.toString());
|
|
|
|
if (!exists(extractDir)) {
|
|
|
|
mkdirRecurse(extractDir);
|
|
|
|
}
|
|
|
|
string[] filenames;
|
2023-07-17 00:58:16 +00:00
|
|
|
uint[] counts;
|
2023-07-16 16:27:22 +00:00
|
|
|
foreach (MultipartElement element; data.elements) {
|
2023-07-17 00:58:16 +00:00
|
|
|
if (element.name == "schematics") {
|
|
|
|
const filePath = buildPath(extractDir, element.filename.get());
|
|
|
|
std.file.write(filePath, element.content);
|
|
|
|
filenames ~= filePath;
|
|
|
|
} else if (element.name == "counts") {
|
|
|
|
import std.conv;
|
|
|
|
immutable uint count = element.content.to!uint;
|
|
|
|
counts ~= count;
|
|
|
|
}
|
2023-07-16 16:27:22 +00:00
|
|
|
}
|
2023-07-17 00:58:16 +00:00
|
|
|
|
2023-07-16 16:27:22 +00:00
|
|
|
infoF!"Running extract process on files: %s"(filenames);
|
2023-07-17 00:58:16 +00:00
|
|
|
auto extractionResult = execute(EXTRACT_COMMAND ~ filenames);
|
|
|
|
immutable int exitCode = extractionResult.status;
|
2023-07-16 16:27:22 +00:00
|
|
|
infoF!"Exit code: %d"(exitCode);
|
2023-07-17 00:58:16 +00:00
|
|
|
rmdirRecurse(extractDir);
|
2023-07-16 16:27:22 +00:00
|
|
|
if (exitCode != 0) {
|
|
|
|
ctx.response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
|
2023-07-17 00:58:16 +00:00
|
|
|
ctx.response.writeBodyString(extractionResult.output);
|
2023-07-16 16:27:22 +00:00
|
|
|
} else {
|
2023-07-17 00:58:16 +00:00
|
|
|
const extractJsonPath = extractDir ~ ".json";
|
|
|
|
// It was successful, so add __COUNT__ to each object in the extract data.
|
|
|
|
JSONValue extractData = parseJSON(extractionResult.output);
|
|
|
|
for (uint i = 0; i < extractData.array.length; i++) {
|
|
|
|
extractData.array[i].object["__COUNT__"] = JSONValue(counts[i]);
|
|
|
|
}
|
|
|
|
std.file.write(extractJsonPath, extractData.toPrettyString());
|
|
|
|
|
2023-07-16 16:27:22 +00:00
|
|
|
JSONValue result = JSONValue.emptyObject;
|
|
|
|
result.object["extractId"] = JSONValue(extractId.toString());
|
|
|
|
ctx.response.writeBodyString(result.toJSON(), "application/json");
|
2023-07-17 00:58:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool validateExtractRequest(ref MultipartFormData data, ref HttpResponse response) {
|
|
|
|
if (data.elements.length < 2) {
|
|
|
|
response.setStatus(HttpStatus.BAD_REQUEST);
|
|
|
|
response.writeBodyString("Requires at least 2 form data elements (schematic file and count).");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint nextElementIdx = 0;
|
|
|
|
while (nextElementIdx < data.elements.length) {
|
|
|
|
MultipartElement element = data.elements[nextElementIdx++];
|
|
|
|
if (element.name == "schematics") {
|
|
|
|
if (element.filename.isNull || element.filename.get().length < 5 || element.content.length < 10) {
|
|
|
|
response.setStatus(HttpStatus.BAD_REQUEST);
|
|
|
|
response.writeBodyString("Invalid or missing schematic file.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const string filename = element.filename.get();
|
|
|
|
if (nextElementIdx == data.elements.length) {
|
|
|
|
response.setStatus(HttpStatus.BAD_REQUEST);
|
|
|
|
response.writeBodyString("Missing count element for schematic: " ~ filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
MultipartElement countElement = data.elements[nextElementIdx++];
|
|
|
|
import std.conv;
|
|
|
|
try {
|
|
|
|
immutable uint count = countElement.content.to!uint;
|
|
|
|
if (count < 1 || count > 1000) throw new Exception("out of range: count should be between 1 and 1000, inclusive.");
|
|
|
|
} catch (Exception e) {
|
|
|
|
response.setStatus(HttpStatus.BAD_REQUEST);
|
|
|
|
response.writeBodyString("Invalid count element: " ~ e.msg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (element.name == "processing-terminal") {
|
|
|
|
// TODO: Check processing-terminal format.
|
|
|
|
} else {
|
|
|
|
response.setStatus(HttpStatus.BAD_REQUEST);
|
|
|
|
response.writeBodyString("Unknown element: " ~ element.name);
|
|
|
|
return false;
|
2023-07-16 16:27:22 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-17 00:58:16 +00:00
|
|
|
|
|
|
|
return true;
|
2023-07-15 22:22:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void getExtract(ref HttpRequestContext ctx) {
|
2023-07-16 16:27:22 +00:00
|
|
|
string extractId = ctx.request.getPathParamAs!string("extractId");
|
2023-07-17 00:58:16 +00:00
|
|
|
const extractFile = buildPath(EXTRACTS_DIR, extractId ~ ".json");
|
2023-07-16 16:27:22 +00:00
|
|
|
fileResponse(ctx.response, extractFile, "application/json");
|
2023-07-15 22:22:48 +00:00
|
|
|
}
|