app.d 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 std.array;
  12. import memcached4d;
  13. import std.conv : to;
  14. import mustache;
  15. alias MustacheEngine!(string) Mustache;
  16. /* test unproper arguments order */
  17. /* do not */
  18. alias test_key1 = int;
  19. alias test_key2 = int;
  20. string test_args_types_mismash(test_key1 x, test_key2 y){
  21. return ( to!string(x) ~ " + " ~ to!string(y) ~ " = " ~ to!string( x + y ) );
  22. }
  23. /* do like */
  24. struct test_key3 { int v; }
  25. struct test_key4 { int v; }
  26. string test_args_types_mismash2(test_key3 x, test_key4 y){
  27. return ( to!string(x.v) ~ " + " ~ to!string(y.v) ~ " = " ~ to!string( x.v + y.v ) );
  28. }
  29. void main(){
  30. auto settings = new HTTPServerSettings;
  31. settings.port = 8080;
  32. settings.bindAddresses = ["::1", "127.0.0.1"];
  33. //auto fsettings = new HTTPFileServerSettings;
  34. //fsettings.serverPathPrefix = "/static";
  35. auto router = new URLRouter;
  36. //router.get("/", &index);
  37. ////router.get("static/*", serverStaticFiles("public/", fsettings) );
  38. //router.get("/", staticTemplate!"index.html");
  39. router.get("/", serveStaticFile("public/index.html") );
  40. router.get("/ws", handleWebSockets(&ws_handle) );
  41. router.get("/test", &test);
  42. router.get("*", serveStaticFiles("public/"));
  43. //auto listener = listenHTTP(settings, &hello);
  44. auto listener = listenHTTP(settings, router);
  45. scope (exit){
  46. listener.stopListening();
  47. }
  48. logInfo("Please open http://127.0.0.1:8080/ in your browser.");
  49. runApplication();
  50. }
  51. void ws_handle(scope WebSocket sock){
  52. // simple echo server + :)
  53. while(sock.connected){
  54. auto msg = sock.receiveText();
  55. sock.send(msg ~ " :)");
  56. }
  57. }
  58. /*
  59. void index(HTTPServerRequest req, HTTPServerResponse res){
  60. res.writeBody("Hello, World!");
  61. }
  62. */
  63. /*
  64. void hello(HTTPServerRequest req, HTTPServerResponse res){
  65. res.writeBody("Hello, World!");
  66. }
  67. */
  68. void test(HTTPServerRequest req, HTTPServerResponse res){
  69. auto cache = memcachedConnect("127.0.0.1:11211");
  70. /* test unproper arguments order */
  71. /* do not */
  72. test_key1 x = 5;
  73. test_key2 y = 2;
  74. //string r1 = test_args_types_mismash(x, y); // proper arguments order
  75. 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..
  76. writeln("r1 = ", r1);
  77. /* do like */
  78. test_key3 x2 = test_key3(5);
  79. test_key4 y2 = test_key4(2);
  80. string r2 = test_args_types_mismash2(x2, y2); // proper arguments order
  81. //string r2 = test_args_types_mismash2(y2, x2); // unproper - this not compiles
  82. writeln("r2 = ", r2);
  83. writeln("get test1 = ", cache.get!string("test1"));
  84. string v1 = "value1 = 🔥🦀";
  85. if(cache.store("test1", v1) == RETURN_STATE.SUCCESS ){
  86. writeln("stored successfully");
  87. writeln("get stored: ", cache.get!string("test1") );
  88. }else{
  89. writeln("not stored");
  90. }
  91. string result = cache.get!string("test1");
  92. writeln("get test1 = ", result);
  93. writeln(cache.del("test1"));
  94. Mustache mustache2;
  95. auto context2 = new Mustache.Context;
  96. mustache2.path = "priv/folder2";
  97. mustache2.ext = "dtl";
  98. context2["param2"] = "blah blah blah ";
  99. Mustache mustache;
  100. auto context = new Mustache.Context;
  101. mustache.path = "priv";
  102. mustache.ext = "dtl";
  103. //context.useSection("boolean");
  104. //assert(mustache.renderString(" {{#boolean}}YES{{/boolean}}\n {{#boolean}}GOOD{{/boolean}}\n", context) == " YES\n GOOD\n");
  105. //{{#repo}}<b>{{name}}</b>{{/repo}}
  106. //{{^repo}}No repos :({{/repo}}
  107. // to
  108. //No repos :(
  109. context["lang"] = "en";
  110. context["number1"] = 42;
  111. context.useSection("maybe1");
  112. context["part1"] = mustache2.render("part1", context2);
  113. context["result1"] = "Hello, World!\n" ~ result;
  114. res.headers["Content-Type"] = "text/html; charset=utf-8";
  115. //res.writeBody("Hello, World!\n" ~ result);
  116. res.writeBody( mustache.render("main", context) );
  117. }