62 lines
1.7 KiB
D
62 lines
1.7 KiB
D
|
module server;
|
||
|
|
||
|
import std.file;
|
||
|
import handy_httpd;
|
||
|
import handy_httpd.handlers.path_delegating_handler;
|
||
|
import d_properties;
|
||
|
|
||
|
void startServer() {
|
||
|
Properties props;
|
||
|
if (exists("sitestat.properties")) {
|
||
|
props = Properties("sitestat.properties");
|
||
|
}
|
||
|
|
||
|
import slf4d;
|
||
|
import slf4d.default_provider;
|
||
|
auto provider = new shared DefaultProvider(true, Levels.INFO);
|
||
|
provider.getLoggerFactory().setModuleLevelPrefix("handy_httpd", Levels.WARN);
|
||
|
// provider.getLoggerFactory().setModuleLevel("live_tracker", Levels.DEBUG);
|
||
|
configureLoggingProvider(provider);
|
||
|
|
||
|
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;
|
||
|
PathDelegatingHandler pathHandler = new PathDelegatingHandler();
|
||
|
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");
|
||
|
}
|
||
|
return config;
|
||
|
}
|