app.d 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //import vibe.vibe;
  2. // https://github.com/vibe-d/vibe.d/blob/master/source/vibe/vibe.d
  3. import vibe.core.core;
  4. import vibe.http.router;
  5. import vibe.http.server;
  6. import vibe.http.fileserver;
  7. import vibe.http.websockets;
  8. import vibe.core.log;
  9. void main(){
  10. auto settings = new HTTPServerSettings;
  11. settings.port = 8080;
  12. settings.bindAddresses = ["::1", "127.0.0.1"];
  13. //auto fsettings = new HTTPFileServerSettings;
  14. //fsettings.serverPathPrefix = "/static";
  15. auto router = new URLRouter;
  16. router.get("/", &index);
  17. ////router.get("static/*", serverStaticFiles("public/", fsettings) );
  18. //router.get("/", staticTemplate!"index.html"); // todo fix
  19. router.get("/ws", handleWebSockets(&ws_handle) );
  20. router.get("*", serveStaticFiles("public/"));
  21. //auto listener = listenHTTP(settings, &hello);
  22. auto listener = listenHTTP(settings, router);
  23. scope (exit){
  24. listener.stopListening();
  25. }
  26. logInfo("Please open http://127.0.0.1:8080/ in your browser.");
  27. runApplication();
  28. }
  29. void ws_handle(scope WebSocket sock){
  30. // simple echo server + :)
  31. while(sock.connected){
  32. auto msg = sock.receiveText();
  33. sock.send(msg ~ " :)");
  34. }
  35. }
  36. void index(HTTPServerRequest req, HTTPServerResponse res){
  37. res.writeBody("Hello, World!");
  38. }
  39. /*
  40. void hello(HTTPServerRequest req, HTTPServerResponse res){
  41. res.writeBody("Hello, World!");
  42. }
  43. */