app.d 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 tr;
  15. import mustache;
  16. alias MustacheEngine!(string) Mustache;
  17. /* test unproper arguments order */
  18. /* do not */
  19. /*
  20. alias test_key1 = int;
  21. alias test_key2 = int;
  22. string test_args_types_mismash(test_key1 x, test_key2 y){
  23. return ( to!string(x) ~ " + " ~ to!string(y) ~ " = " ~ to!string( x + y ) );
  24. }
  25. */
  26. /* do not */
  27. /*
  28. import std.typecons : Typedef;
  29. alias test_key5 = Typedef!int;
  30. alias test_key6 = Typedef!int;
  31. string test_args_types_mismash(test_key5 x, test_key6 y){
  32. return ( to!string(x) ~ " + " ~ to!string(y) ~ " = " ~ to!string( x + y ) );
  33. }
  34. */
  35. /* do like */
  36. import std.typecons : Typedef;
  37. alias test_key5 = Typedef!(int, int.init, "key5");
  38. alias test_key6 = Typedef!(int, int.init, "key6");
  39. string test_args_types_mismash(test_key5 x, test_key6 y){
  40. return ( to!string(x) ~ " + " ~ to!string(y) ~ " = " ~ to!string( x + y ) );
  41. }
  42. /* or - do like */
  43. struct test_key3 { int v; }
  44. struct test_key4 { int v; }
  45. string test_args_types_mismash2(test_key3 x, test_key4 y){
  46. return ( to!string(x.v) ~ " + " ~ to!string(y.v) ~ " = " ~ to!string( x.v + y.v ) );
  47. }
  48. void main(){
  49. auto settings = new HTTPServerSettings;
  50. settings.port = 8080;
  51. settings.bindAddresses = ["::1", "127.0.0.1"];
  52. //auto fsettings = new HTTPFileServerSettings;
  53. //fsettings.serverPathPrefix = "/static";
  54. auto router = new URLRouter;
  55. //router.get("/", &index);
  56. ////router.get("static/*", serverStaticFiles("public/", fsettings) );
  57. //router.get("/", staticTemplate!"index.html");
  58. router.get("/", serveStaticFile("public/index.html") );
  59. router.get("/ws", handleWebSockets(&ws_handle) );
  60. router.get("/test", &test);
  61. router.get("*", serveStaticFiles("public/"));
  62. //auto listener = listenHTTP(settings, &hello);
  63. auto listener = listenHTTP(settings, router);
  64. scope (exit){
  65. listener.stopListening();
  66. }
  67. logInfo("Please open http://127.0.0.1:8080/ in your browser.");
  68. runApplication();
  69. }
  70. void ws_handle(scope WebSocket sock){
  71. // simple echo server + :)
  72. while(sock.connected){
  73. auto msg = sock.receiveText();
  74. sock.send(msg ~ " :)");
  75. }
  76. }
  77. /*
  78. void index(HTTPServerRequest req, HTTPServerResponse res){
  79. res.writeBody("Hello, World!");
  80. }
  81. */
  82. /*
  83. void hello(HTTPServerRequest req, HTTPServerResponse res){
  84. res.writeBody("Hello, World!");
  85. }
  86. */
  87. void test(HTTPServerRequest req, HTTPServerResponse res){
  88. auto cache = memcachedConnect("127.0.0.1:11211");
  89. /* test unproper arguments order */
  90. /* do not */
  91. /*
  92. test_key1 x = 5;
  93. test_key2 y = 2;
  94. //string r1 = test_args_types_mismash(x, y); // proper arguments order
  95. 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..
  96. writeln("r1 = ", r1);
  97. */
  98. /* do not */
  99. /*
  100. test_key5 x = 5;
  101. test_key6 y = 2;
  102. //string r3 = test_args_types_mismash(x, y); // proper arguments order
  103. string r3 = test_args_types_mismash(y, x); // unproper - this compiles but we got logic error bug in runtime.. :( use struct for compiler check this..
  104. writeln("r3 = ", r3);
  105. */
  106. /* do like */
  107. test_key5 x = 5;
  108. test_key6 y = 2;
  109. string r3 = test_args_types_mismash(x, y); // proper arguments order
  110. //string r3 = test_args_types_mismash(y, x); // unproper - this not compiles
  111. writeln("r3 = ", r3);
  112. /* or - do like */
  113. test_key3 x2 = test_key3(5);
  114. test_key4 y2 = test_key4(2);
  115. string r2 = test_args_types_mismash2(x2, y2); // proper arguments order
  116. //string r2 = test_args_types_mismash2(y2, x2); // unproper - this not compiles
  117. writeln("r2 = ", r2);
  118. Language Lang = Language.uk;
  119. writeln("tr 1 = ", Tr(Lang, TKey.hello));
  120. writeln("tr 2 = ", Tr(Lang, TKey.welcome, ["username"], 0) );
  121. writeln("tr 3 = ", Tr(Lang, TKey.apples, [], 1) );
  122. writeln("tr 3 = ", Tr(Lang, TKey.apples, [], 2) ) ;
  123. writeln("tr 3 = ", Tr(Lang, TKey.apples, [], 5) );
  124. writeln("tr 4 = ", Tr(Lang, TKey.apples_n_oranges, ["6", "7"], 0) );
  125. writeln("get test1 = ", cache.get!string("test1"));
  126. string v1 = "value1 = 🔥🦀";
  127. if(cache.store("test1", v1) == RETURN_STATE.SUCCESS ){
  128. writeln("stored successfully");
  129. writeln("get stored: ", cache.get!string("test1") );
  130. }else{
  131. writeln("not stored");
  132. }
  133. string result = cache.get!string("test1");
  134. writeln("get test1 = ", result);
  135. writeln(cache.del("test1"));
  136. Mustache mustache2;
  137. auto context2 = new Mustache.Context;
  138. mustache2.path = "priv/folder2";
  139. mustache2.ext = "dtl";
  140. context2["param2"] = "blah blah blah ";
  141. Mustache mustache;
  142. auto context = new Mustache.Context;
  143. mustache.path = "priv";
  144. mustache.ext = "dtl";
  145. //context.useSection("boolean");
  146. //assert(mustache.renderString(" {{#boolean}}YES{{/boolean}}\n {{#boolean}}GOOD{{/boolean}}\n", context) == " YES\n GOOD\n");
  147. //{{#repo}}<b>{{name}}</b>{{/repo}}
  148. //{{^repo}}No repos :({{/repo}}
  149. // to
  150. //No repos :(
  151. context["lang"] = "en";
  152. context["number1"] = 42;
  153. context.useSection("maybe1");
  154. context["part1"] = mustache2.render("part1", context2);
  155. context["result1"] = "Hello, World!\n" ~ result;
  156. res.headers["Content-Type"] = "text/html; charset=utf-8";
  157. //res.writeBody("Hello, World!\n" ~ result);
  158. res.writeBody( mustache.render("main", context) );
  159. }