app.d 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import std;
  2. // Docs: https://trikko.github.io/serverino/
  3. // Tips and tricks: https://github.com/trikko/serverino/wiki/
  4. // Examples: https://github.com/trikko/serverino/tree/master/examples
  5. import serverino;
  6. mixin ServerinoMain;
  7. import settings_toml; // get settings from settings.toml, settings keys, settings validation etc
  8. @onServerInit ServerinoConfig setup(){
  9. ServerinoConfig sc = ServerinoConfig.create(); // Config with default params
  10. //writeln("ServerinoConfig sc = ", sc);
  11. // ServerinoConfig sc = ServerinoConfig(
  12. // DaemonConfig(all, 10485760, 10 secs, 3 secs, 0, 5, 2048, false, true, false, []),
  13. // WorkerConfig(5 secs, 10 secs, 6 hours, 1 hour, 1 minute, true, "", "", false), [], 0, false)
  14. read_settings_toml(); // read settings, settings validation
  15. //sc.addListener("127.0.0.1", 8081);
  16. 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() );
  17. //writeln("ServerinoConfig sc = ", sc);
  18. //ServerinoConfig sc = ServerinoConfig(
  19. // DaemonConfig(all, 10485760, 10 secs, 3 secs, 0, 5, 2048, false, true, false, [serverino.config.Listener]),
  20. // WorkerConfig(5 secs, 10 secs, 6 hours, 1 hour, 1 minute, true, "", "", false), [], 0, false)
  21. return sc;
  22. }
  23. // Accept a new connection only if the request path is "/echo"
  24. @onWebSocketUpgrade bool onUpgrade(Request req){
  25. return req.path == "/echo";
  26. }
  27. // Handle the WebSocket connection
  28. @endpoint void echo(Request r, WebSocket ws){
  29. // Read messages from the client
  30. while(true){
  31. // Only if the message is valid
  32. if( WebSocketMessage msg = ws.receiveMessage() ){
  33. // Send the message back to the client
  34. ws.send("I received your message: `" ~ msg.asString ~ "`");
  35. }
  36. }
  37. }
  38. @route!"/"
  39. @endpoint void index(Request r, Output o){ o.serveFile("static/index.html"); }
  40. @route!"/style.css"
  41. @endpoint void css(Request r, Output o){ o.serveFile("static/style.css"); }