app.d 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. import std.stdio;
  10. import std.string;
  11. import memcached4d;
  12. import std.conv : to;
  13. /* test unproper arguments order */
  14. /* do not */
  15. alias test_key1 = int;
  16. alias test_key2 = int;
  17. string test_args_types_mismash(test_key1 x, test_key2 y){
  18. return ( to!string(x) ~ " + " ~ to!string(y) ~ " = " ~ to!string( x + y ) );
  19. }
  20. /* do like */
  21. struct test_key3 { int v; }
  22. struct test_key4 { int v; }
  23. string test_args_types_mismash2(test_key3 x, test_key4 y){
  24. return ( to!string(x.v) ~ " + " ~ to!string(y.v) ~ " = " ~ to!string( x.v + y.v ) );
  25. }
  26. void main(){
  27. auto settings = new HTTPServerSettings;
  28. settings.port = 8080;
  29. settings.bindAddresses = ["::1", "127.0.0.1"];
  30. //auto fsettings = new HTTPFileServerSettings;
  31. //fsettings.serverPathPrefix = "/static";
  32. auto router = new URLRouter;
  33. //router.get("/", &index);
  34. ////router.get("static/*", serverStaticFiles("public/", fsettings) );
  35. //router.get("/", staticTemplate!"index.html");
  36. router.get("/", serveStaticFile("public/index.html") );
  37. router.get("/ws", handleWebSockets(&ws_handle) );
  38. router.get("/test", &test);
  39. router.get("*", serveStaticFiles("public/"));
  40. //auto listener = listenHTTP(settings, &hello);
  41. auto listener = listenHTTP(settings, router);
  42. scope (exit){
  43. listener.stopListening();
  44. }
  45. logInfo("Please open http://127.0.0.1:8080/ in your browser.");
  46. runApplication();
  47. }
  48. void ws_handle(scope WebSocket sock){
  49. // simple echo server + :)
  50. while(sock.connected){
  51. auto msg = sock.receiveText();
  52. sock.send(msg ~ " :)");
  53. }
  54. }
  55. /*
  56. void index(HTTPServerRequest req, HTTPServerResponse res){
  57. res.writeBody("Hello, World!");
  58. }
  59. */
  60. /*
  61. void hello(HTTPServerRequest req, HTTPServerResponse res){
  62. res.writeBody("Hello, World!");
  63. }
  64. */
  65. void test(HTTPServerRequest req, HTTPServerResponse res){
  66. auto cache = memcachedConnect("127.0.0.1:11211");
  67. /* test unproper arguments order */
  68. /* do not */
  69. test_key1 x = 5;
  70. test_key2 y = 2;
  71. //string r1 = test_args_types_mismash(x, y); // proper arguments order
  72. string r1 = test_args_types_mismash(y, x); // unproper - this compiles but we got logic error bug in runtime.. :( use struct for compiler check this..
  73. writeln("r1 = ", r1);
  74. /* do like */
  75. test_key3 x2 = test_key3(5);
  76. test_key4 y2 = test_key4(2);
  77. string r2 = test_args_types_mismash2(x2, y2); // proper arguments order
  78. //string r2 = test_args_types_mismash2(y2, x2); // unproper - this not compiles
  79. writeln("r2 = ", r2);
  80. writeln("get test1 = ", cache.get!string("test1"));
  81. string v1 = "value1 = 🔥🦀";
  82. if(cache.store("test1", v1) == RETURN_STATE.SUCCESS ){
  83. writeln("stored successfully");
  84. writeln("get stored: ", cache.get!string("test1") );
  85. }else{
  86. writeln("not stored");
  87. }
  88. string result = cache.get!string("test1");
  89. writeln("get test1 = ", result);
  90. writeln(cache.del("test1"));
  91. res.writeBody("Hello, World!\n" ~ result);
  92. }