2023-08-16 14:37:39 +00:00
|
|
|
import handy_httpd;
|
|
|
|
import slf4d;
|
|
|
|
import slf4d.default_provider;
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
auto provider = new shared DefaultProvider(true, Levels.INFO);
|
2023-08-18 14:37:52 +00:00
|
|
|
// provider.getLoggerFactory().setModuleLevelPrefix("handy_httpd", Levels.WARN);
|
2023-08-16 14:37:39 +00:00
|
|
|
configureLoggingProvider(provider);
|
|
|
|
|
|
|
|
HttpServer server = initServer();
|
|
|
|
server.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
private HttpServer initServer() {
|
|
|
|
import handy_httpd.handlers.path_delegating_handler;
|
|
|
|
import handy_httpd.handlers.filtered_handler;
|
2023-08-18 14:37:52 +00:00
|
|
|
import handy_httpd.handlers.file_resolving_handler;
|
|
|
|
import d_properties;
|
2023-08-16 14:37:39 +00:00
|
|
|
import auth;
|
2023-08-17 15:55:05 +00:00
|
|
|
import lists;
|
2023-08-18 14:37:52 +00:00
|
|
|
import std.file;
|
|
|
|
import std.conv;
|
2023-08-16 14:37:39 +00:00
|
|
|
|
|
|
|
ServerConfig config = ServerConfig.defaultValues();
|
|
|
|
config.enableWebSockets = false;
|
|
|
|
config.workerPoolSize = 3;
|
|
|
|
config.connectionQueueSize = 10;
|
2023-08-18 14:37:52 +00:00
|
|
|
if (exists("application.properties")) {
|
|
|
|
Properties props = Properties("application.properties");
|
|
|
|
if (props.has("port")) {
|
|
|
|
config.port = props.get("port").to!ushort;
|
|
|
|
}
|
|
|
|
if (props.has("workers")) {
|
|
|
|
config.workerPoolSize = props.get("workers").to!size_t;
|
|
|
|
}
|
|
|
|
if (props.has("hostname")) {
|
|
|
|
config.hostname = props.get("hostname");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set some CORS headers to prevent headache.
|
2023-08-16 14:37:39 +00:00
|
|
|
config.defaultHeaders["Access-Control-Allow-Origin"] = "*";
|
2023-08-17 15:55:05 +00:00
|
|
|
config.defaultHeaders["Access-Control-Allow-Credentials"] = "true";
|
2023-08-18 01:38:10 +00:00
|
|
|
config.defaultHeaders["Access-Control-Allow-Methods"] = "*";
|
2023-08-17 15:55:05 +00:00
|
|
|
config.defaultHeaders["Vary"] = "origin";
|
|
|
|
config.defaultHeaders["Access-Control-Allow-Headers"] = "Authorization";
|
2023-08-16 14:37:39 +00:00
|
|
|
|
2023-08-18 14:37:52 +00:00
|
|
|
immutable string API_PATH = "/api";
|
2023-08-16 14:37:39 +00:00
|
|
|
|
|
|
|
auto mainHandler = new PathDelegatingHandler();
|
2023-08-18 14:37:52 +00:00
|
|
|
mainHandler.addMapping(Method.GET, API_PATH ~ "/status", (ref HttpRequestContext ctx) {
|
2023-08-16 14:37:39 +00:00
|
|
|
ctx.response.writeBodyString("online");
|
|
|
|
});
|
2023-08-17 15:55:05 +00:00
|
|
|
|
|
|
|
auto optionsHandler = toHandler((ref HttpRequestContext ctx) {
|
|
|
|
ctx.response.setStatus(HttpStatus.OK);
|
|
|
|
});
|
|
|
|
|
2023-08-18 14:37:52 +00:00
|
|
|
mainHandler.addMapping(Method.POST, API_PATH ~ "/register", &createNewUser);
|
|
|
|
mainHandler.addMapping(Method.POST, API_PATH ~ "/login", &handleLogin);
|
|
|
|
mainHandler.addMapping(Method.GET, API_PATH ~ "/me", &getMyUser);
|
|
|
|
mainHandler.addMapping(Method.DELETE, API_PATH ~ "/me", &deleteMyUser);
|
|
|
|
mainHandler.addMapping(Method.GET, API_PATH ~ "/renew-token", &renewToken);
|
|
|
|
|
|
|
|
mainHandler.addMapping(Method.GET, API_PATH ~ "/lists", &getNoteLists);
|
|
|
|
mainHandler.addMapping(Method.POST, API_PATH ~ "/lists", &createNoteList);
|
|
|
|
mainHandler.addMapping(Method.GET, API_PATH ~ "/lists/{id}", &getNoteList);
|
|
|
|
mainHandler.addMapping(Method.DELETE, API_PATH ~ "/lists/{id}", &deleteNoteList);
|
|
|
|
mainHandler.addMapping(Method.POST, API_PATH ~ "/lists/{listId}/notes", &createNote);
|
|
|
|
mainHandler.addMapping(Method.DELETE, API_PATH ~ "/lists/{listId}/notes/{noteId}", &deleteNote);
|
|
|
|
|
|
|
|
mainHandler.addMapping(Method.OPTIONS, API_PATH ~ "/**", optionsHandler);
|
|
|
|
|
|
|
|
mainHandler.addMapping("/**", new FileResolvingHandler("app-content", DirectoryResolutionStrategies.none));
|
2023-08-16 14:37:39 +00:00
|
|
|
|
|
|
|
return new HttpServer(mainHandler, config);
|
|
|
|
}
|