2024-07-24 20:32:08 +00:00
|
|
|
import slf4d;
|
|
|
|
import handy_httpd;
|
|
|
|
import handy_httpd.handlers.path_handler;
|
2024-07-31 17:20:17 +00:00
|
|
|
import handy_httpd.handlers.filtered_handler;
|
2024-07-24 20:32:08 +00:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
ServerConfig cfg;
|
|
|
|
cfg.workerPoolSize = 5;
|
|
|
|
cfg.port = 8080;
|
2024-07-31 17:20:17 +00:00
|
|
|
HttpServer server = new HttpServer(buildHandlers(), cfg);
|
|
|
|
server.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
PathHandler buildHandlers() {
|
|
|
|
import profile;
|
|
|
|
|
|
|
|
const API_PATH = "/api";
|
2024-07-24 20:32:08 +00:00
|
|
|
PathHandler pathHandler = new PathHandler();
|
2024-07-31 17:20:17 +00:00
|
|
|
|
|
|
|
// Generic, public endpoints:
|
|
|
|
pathHandler.addMapping(Method.GET, API_PATH ~ "/status", (ref ctx) {
|
2024-07-24 20:32:08 +00:00
|
|
|
ctx.response.writeBodyString("online");
|
|
|
|
});
|
2024-07-31 17:20:17 +00:00
|
|
|
pathHandler.addMapping(Method.OPTIONS, API_PATH ~ "/**", (ref ctx) {});
|
|
|
|
|
|
|
|
// Auth Entrypoints:
|
|
|
|
import auth.api;
|
|
|
|
import auth.service;
|
|
|
|
pathHandler.addMapping(Method.POST, API_PATH ~ "/login", &postLogin);
|
|
|
|
pathHandler.addMapping(Method.POST, API_PATH ~ "/register", &postRegister);
|
|
|
|
|
|
|
|
// Authenticated endpoints:
|
|
|
|
PathHandler a = new PathHandler();
|
|
|
|
a.addMapping(Method.GET, API_PATH ~ "/me", &getMyUser);
|
|
|
|
a.addMapping(Method.GET, API_PATH ~ "/profiles", &handleGetProfiles);
|
|
|
|
a.addMapping(Method.POST, API_PATH ~ "/profiles", &handleCreateNewProfile);
|
|
|
|
a.addMapping(Method.DELETE, API_PATH ~ "/profiles/:name", &handleDeleteProfile);
|
|
|
|
a.addMapping(Method.GET, API_PATH ~ "/profiles/:profile/properties", &handleGetProperties);
|
|
|
|
a.addMapping(Method.GET, API_PATH ~ "/profiles/:profile/accounts", (ref ctx) {
|
|
|
|
ctx.response.writeBodyString("your accounts!");
|
|
|
|
});
|
|
|
|
|
|
|
|
HttpRequestFilter tokenAuthenticationFilter = new TokenAuthenticationFilter(SECRET);
|
|
|
|
pathHandler.addMapping(API_PATH ~ "/**", new FilteredRequestHandler(
|
|
|
|
a,
|
|
|
|
[tokenAuthenticationFilter]
|
|
|
|
));
|
|
|
|
|
|
|
|
return pathHandler;
|
2024-07-23 21:20:10 +00:00
|
|
|
}
|