Added readConfig() to configure app for production environment.

This commit is contained in:
Andrew Lalis 2025-08-09 09:45:10 -04:00
parent 7f8218db3c
commit abe04db627
3 changed files with 33 additions and 5 deletions

View File

@ -0,0 +1,6 @@
vars {
username: testuser0
password: testpass
profile: test-profile-0
base_url: https://finnow.andrewlalis.com/api
}

View File

@ -9,9 +9,11 @@ private const API_PATH = "/api";
/**
* Defines the Finnow API mapping with a main PathHandler.
* Params:
* webOrigin: The origin to use when configuring CORS headers.
* Returns: The handler to plug into an HttpServer.
*/
HttpRequestHandler mapApiHandlers() {
HttpRequestHandler mapApiHandlers(string webOrigin) {
PathHandler h = new PathHandler();
// Generic, public endpoints:
@ -71,7 +73,7 @@ HttpRequestHandler mapApiHandlers() {
a
));
return new CorsHandler(h);
return new CorsHandler(h, webOrigin);
}
private void getStatus(ref ServerHttpRequest request, ref ServerHttpResponse response) {
@ -108,13 +110,15 @@ private void map(
private class CorsHandler : HttpRequestHandler {
private HttpRequestHandler handler;
private string webOrigin;
this(HttpRequestHandler handler) {
this(HttpRequestHandler handler, string webOrigin) {
this.handler = handler;
this.webOrigin = webOrigin;
}
void handle(ref ServerHttpRequest request, ref ServerHttpResponse response) {
response.headers.add("Access-Control-Allow-Origin", "http://localhost:5173");
response.headers.add("Access-Control-Allow-Origin", webOrigin);
response.headers.add("Access-Control-Allow-Methods", "*");
response.headers.add("Access-Control-Allow-Headers", "Authorization, Content-Type");
try {

View File

@ -4,10 +4,28 @@ import slf4d.default_provider;
import api_mapping;
struct AppConfig {
ushort port;
string webOrigin;
}
void main() {
const AppConfig config = readConfig();
auto provider = new DefaultProvider(Levels.INFO);
configureLoggingProvider(provider);
HttpTransport transport = new TaskPoolHttp1Transport(mapApiHandlers());
HttpTransport transport = new TaskPoolHttp1Transport(mapApiHandlers(config.webOrigin), config.port);
transport.start();
}
AppConfig readConfig() {
import std.file : exists, readText;
import std.json;
import std.conv : to;
// Local dev environment if no config is given.
if (!exists("finnow-api-config.json")) {
return AppConfig(8080, "http://localhost:5173");
}
JSONValue obj = parseJSON(readText("finnow-api-config.json"));
return AppConfig(obj.object["port"].integer.to!ushort, obj.object["webOrigin"].str);
}