2024-08-01 17:01:50 +00:00
|
|
|
module api_mapping;
|
|
|
|
|
|
|
|
import handy_httpd;
|
|
|
|
import handy_httpd.handlers.path_handler;
|
|
|
|
import handy_httpd.handlers.filtered_handler;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines the Finnow API mapping with a main PathHandler.
|
|
|
|
* Returns: The handler to plug into an HttpServer.
|
|
|
|
*/
|
|
|
|
PathHandler mapApiHandlers() {
|
|
|
|
/// The base path to all API endpoints.
|
2024-09-11 20:15:53 +00:00
|
|
|
const API_PATH = "/api";
|
2024-08-01 17:01:50 +00:00
|
|
|
PathHandler h = new PathHandler();
|
|
|
|
|
|
|
|
// Generic, public endpoints:
|
|
|
|
h.addMapping(Method.GET, API_PATH ~ "/status", &getStatus);
|
|
|
|
h.addMapping(Method.OPTIONS, API_PATH ~ "/**", &getOptions);
|
2024-09-11 20:15:53 +00:00
|
|
|
|
2024-09-19 19:12:23 +00:00
|
|
|
// Dev endpoint for sample data: REMOVE BEFORE DEPLOYING!!!
|
|
|
|
h.addMapping(Method.POST, API_PATH ~ "/sample-data", &sampleDataEndpoint);
|
|
|
|
|
2024-08-01 17:01:50 +00:00
|
|
|
// Auth endpoints:
|
|
|
|
import auth.api;
|
|
|
|
h.addMapping(Method.POST, API_PATH ~ "/login", &postLogin);
|
|
|
|
h.addMapping(Method.POST, API_PATH ~ "/register", &postRegister);
|
2024-09-11 20:15:53 +00:00
|
|
|
h.addMapping(Method.GET, API_PATH ~ "/register/username-availability", &getUsernameAvailability);
|
2024-08-01 17:01:50 +00:00
|
|
|
|
|
|
|
// Authenticated endpoints:
|
|
|
|
PathHandler a = new PathHandler();
|
|
|
|
a.addMapping(Method.GET, API_PATH ~ "/me", &getMyUser);
|
2024-09-19 19:12:23 +00:00
|
|
|
a.addMapping(Method.DELETE, API_PATH ~ "/me", &deleteMyUser);
|
2024-09-11 20:15:53 +00:00
|
|
|
|
|
|
|
import profile.api;
|
2024-08-01 17:01:50 +00:00
|
|
|
a.addMapping(Method.GET, API_PATH ~ "/profiles", &handleGetProfiles);
|
|
|
|
a.addMapping(Method.POST, API_PATH ~ "/profiles", &handleCreateNewProfile);
|
|
|
|
/// URL path to a specific profile, with the :profile path parameter.
|
|
|
|
const PROFILE_PATH = API_PATH ~ "/profiles/:profile";
|
|
|
|
a.addMapping(Method.DELETE, PROFILE_PATH, &handleDeleteProfile);
|
|
|
|
a.addMapping(Method.GET, PROFILE_PATH ~ "/properties", &handleGetProperties);
|
|
|
|
|
2024-09-11 20:15:53 +00:00
|
|
|
import account.api;
|
|
|
|
a.addMapping(Method.GET, PROFILE_PATH ~ "/accounts", &handleGetAccounts);
|
2024-08-01 17:01:50 +00:00
|
|
|
a.addMapping(Method.POST, PROFILE_PATH ~ "/accounts", &handleCreateAccount);
|
|
|
|
a.addMapping(Method.GET, PROFILE_PATH ~ "/accounts/:accountId:ulong", &handleGetAccount);
|
|
|
|
a.addMapping(Method.DELETE, PROFILE_PATH ~ "/accounts/:accountId:ulong", &handleDeleteAccount);
|
|
|
|
|
2024-09-11 20:15:53 +00:00
|
|
|
// Protect all authenticated paths with a token filter.
|
2024-08-01 17:01:50 +00:00
|
|
|
import auth.service : TokenAuthenticationFilter, SECRET;
|
|
|
|
HttpRequestFilter tokenAuthenticationFilter = new TokenAuthenticationFilter(SECRET);
|
|
|
|
h.addMapping(API_PATH ~ "/**", new FilteredRequestHandler(
|
2024-09-11 20:15:53 +00:00
|
|
|
a,
|
|
|
|
[tokenAuthenticationFilter]
|
2024-08-01 17:01:50 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void getStatus(ref HttpRequestContext ctx) {
|
2024-09-11 20:15:53 +00:00
|
|
|
ctx.response.writeBodyString("online");
|
2024-08-01 17:01:50 +00:00
|
|
|
}
|
|
|
|
|
2024-09-11 20:15:53 +00:00
|
|
|
private void getOptions(ref HttpRequestContext ctx) {
|
|
|
|
}
|
2024-09-19 19:12:23 +00:00
|
|
|
|
|
|
|
private void sampleDataEndpoint(ref HttpRequestContext ctx) {
|
|
|
|
import util.sample_data;
|
|
|
|
import core.thread;
|
|
|
|
Thread t = new Thread(() => generateSampleData());
|
|
|
|
t.start();
|
|
|
|
}
|