2023-10-04 17:30:00 +00:00
|
|
|
module server;
|
|
|
|
|
|
|
|
import std.file;
|
|
|
|
import handy_httpd;
|
2023-11-05 15:18:12 +00:00
|
|
|
import handy_httpd.handlers.path_handler;
|
2023-10-04 17:30:00 +00:00
|
|
|
import d_properties;
|
2023-10-05 16:35:52 +00:00
|
|
|
import slf4d;
|
2023-10-04 17:30:00 +00:00
|
|
|
|
|
|
|
void startServer() {
|
|
|
|
Properties props;
|
|
|
|
if (exists("sitestat.properties")) {
|
|
|
|
props = Properties("sitestat.properties");
|
2023-10-05 16:35:52 +00:00
|
|
|
} else {
|
|
|
|
warn("No sitestat.properties file was found. Using defaults!");
|
2023-10-04 17:30:00 +00:00
|
|
|
}
|
|
|
|
new HttpServer(prepareHandler(props), prepareConfig(props)).start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares the main request handler for the server.
|
|
|
|
* Returns: The request handler.
|
|
|
|
*/
|
|
|
|
private HttpRequestHandler prepareHandler(Properties props) {
|
|
|
|
import live_tracker;
|
2023-11-05 15:18:12 +00:00
|
|
|
PathHandler pathHandler = new PathHandler();
|
2023-10-04 17:30:00 +00:00
|
|
|
pathHandler.addMapping(
|
|
|
|
Method.GET,
|
|
|
|
"/ws",
|
|
|
|
new WebSocketHandler(new LiveTracker(props.get!uint(
|
|
|
|
"minSessionDurationMillis",
|
|
|
|
LiveTracker.DEFAULT_MIN_SESSION_DURATION
|
|
|
|
)))
|
|
|
|
);
|
|
|
|
return pathHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares the server's configuration using sensible default values that can
|
|
|
|
* be overridded by a "sitestat.properties" file in the program's working dir.
|
|
|
|
* Returns: The config to use.
|
|
|
|
*/
|
|
|
|
private ServerConfig prepareConfig(Properties props) {
|
|
|
|
ServerConfig config = ServerConfig.defaultValues();
|
|
|
|
config.workerPoolSize = 3;
|
|
|
|
config.port = 8081;
|
|
|
|
config.enableWebSockets = true;
|
|
|
|
if (props.has("server.host")) {
|
|
|
|
config.hostname = props.get("host");
|
|
|
|
}
|
|
|
|
if (props.has("server.port")) {
|
|
|
|
config.port = props.get!ushort("server.port");
|
|
|
|
}
|
|
|
|
if (props.has("server.workers")) {
|
|
|
|
config.workerPoolSize = props.get!size_t("server.workers");
|
|
|
|
}
|
2023-10-05 16:35:52 +00:00
|
|
|
config.defaultHeaders["Access-Control-Allow-Origin"] = "*";
|
|
|
|
config.defaultHeaders["Access-Control-Allow-Methods"] = "*";
|
|
|
|
config.defaultHeaders["Access-Control-Allow-Headers"] = "*";
|
2023-10-04 17:30:00 +00:00
|
|
|
return config;
|
|
|
|
}
|