123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- import std;
- // Docs: https://trikko.github.io/serverino/
- // Tips and tricks: https://github.com/trikko/serverino/wiki/
- // Examples: https://github.com/trikko/serverino/tree/master/examples
- import serverino;
- mixin ServerinoMain;
- import settings_toml; // get settings from settings.toml, settings keys, settings validation etc
- @onServerInit ServerinoConfig setup(){
- ServerinoConfig sc = ServerinoConfig.create(); // Config with default params
- //writeln("ServerinoConfig sc = ", sc);
- // ServerinoConfig sc = ServerinoConfig(
- // DaemonConfig(all, 10485760, 10 secs, 3 secs, 0, 5, 2048, false, true, false, []),
- // WorkerConfig(5 secs, 10 secs, 6 hours, 1 hour, 1 minute, true, "", "", false), [], 0, false)
-
- read_settings_toml(); // read settings, settings validation
- //sc.addListener("127.0.0.1", 8081);
- sc.addListener(toml_s[s_toml_http][s_toml_http_host].str().idup, cast(ushort)toml_s[s_toml_http][s_toml_http_port].integer() );
-
- //writeln("ServerinoConfig sc = ", sc);
- //ServerinoConfig sc = ServerinoConfig(
- // DaemonConfig(all, 10485760, 10 secs, 3 secs, 0, 5, 2048, false, true, false, [serverino.config.Listener]),
- // WorkerConfig(5 secs, 10 secs, 6 hours, 1 hour, 1 minute, true, "", "", false), [], 0, false)
- return sc;
- }
- // Accept a new connection only if the request path is "/echo"
- @onWebSocketUpgrade bool onUpgrade(Request req){
- return req.path == "/echo";
- }
- // Handle the WebSocket connection
- @endpoint void echo(Request r, WebSocket ws){
- // Read messages from the client
- while(true){
- // Only if the message is valid
- if( WebSocketMessage msg = ws.receiveMessage() ){
- // Send the message back to the client
- ws.send("I received your message: `" ~ msg.asString ~ "`");
- }
- }
- }
- @route!"/"
- @endpoint void index(Request r, Output o){ o.serveFile("static/index.html"); }
- @route!"/style.css"
- @endpoint void css(Request r, Output o){ o.serveFile("static/style.css"); }
|