Added readConfig() to configure app for production environment.
This commit is contained in:
parent
7f8218db3c
commit
abe04db627
|
@ -0,0 +1,6 @@
|
||||||
|
vars {
|
||||||
|
username: testuser0
|
||||||
|
password: testpass
|
||||||
|
profile: test-profile-0
|
||||||
|
base_url: https://finnow.andrewlalis.com/api
|
||||||
|
}
|
|
@ -9,9 +9,11 @@ private const API_PATH = "/api";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines the Finnow API mapping with a main PathHandler.
|
* 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.
|
* Returns: The handler to plug into an HttpServer.
|
||||||
*/
|
*/
|
||||||
HttpRequestHandler mapApiHandlers() {
|
HttpRequestHandler mapApiHandlers(string webOrigin) {
|
||||||
PathHandler h = new PathHandler();
|
PathHandler h = new PathHandler();
|
||||||
|
|
||||||
// Generic, public endpoints:
|
// Generic, public endpoints:
|
||||||
|
@ -71,7 +73,7 @@ HttpRequestHandler mapApiHandlers() {
|
||||||
a
|
a
|
||||||
));
|
));
|
||||||
|
|
||||||
return new CorsHandler(h);
|
return new CorsHandler(h, webOrigin);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void getStatus(ref ServerHttpRequest request, ref ServerHttpResponse response) {
|
private void getStatus(ref ServerHttpRequest request, ref ServerHttpResponse response) {
|
||||||
|
@ -108,13 +110,15 @@ private void map(
|
||||||
|
|
||||||
private class CorsHandler : HttpRequestHandler {
|
private class CorsHandler : HttpRequestHandler {
|
||||||
private HttpRequestHandler handler;
|
private HttpRequestHandler handler;
|
||||||
|
private string webOrigin;
|
||||||
|
|
||||||
this(HttpRequestHandler handler) {
|
this(HttpRequestHandler handler, string webOrigin) {
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
|
this.webOrigin = webOrigin;
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle(ref ServerHttpRequest request, ref ServerHttpResponse response) {
|
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-Methods", "*");
|
||||||
response.headers.add("Access-Control-Allow-Headers", "Authorization, Content-Type");
|
response.headers.add("Access-Control-Allow-Headers", "Authorization, Content-Type");
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -4,10 +4,28 @@ import slf4d.default_provider;
|
||||||
|
|
||||||
import api_mapping;
|
import api_mapping;
|
||||||
|
|
||||||
|
struct AppConfig {
|
||||||
|
ushort port;
|
||||||
|
string webOrigin;
|
||||||
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
const AppConfig config = readConfig();
|
||||||
auto provider = new DefaultProvider(Levels.INFO);
|
auto provider = new DefaultProvider(Levels.INFO);
|
||||||
configureLoggingProvider(provider);
|
configureLoggingProvider(provider);
|
||||||
|
|
||||||
HttpTransport transport = new TaskPoolHttp1Transport(mapApiHandlers());
|
HttpTransport transport = new TaskPoolHttp1Transport(mapApiHandlers(config.webOrigin), config.port);
|
||||||
transport.start();
|
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);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue