app.d 5.1 KB

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