finnow/finnow-api/source/app.d

51 lines
1.6 KiB
D

import slf4d;
import handy_httpd;
import handy_httpd.handlers.path_handler;
import handy_httpd.handlers.filtered_handler;
void main() {
ServerConfig cfg;
cfg.workerPoolSize = 5;
cfg.port = 8080;
HttpServer server = new HttpServer(buildHandlers(), cfg);
server.start();
}
PathHandler buildHandlers() {
import profile;
const API_PATH = "/api";
PathHandler pathHandler = new PathHandler();
// Generic, public endpoints:
pathHandler.addMapping(Method.GET, API_PATH ~ "/status", (ref ctx) {
ctx.response.writeBodyString("online");
});
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;
}