app.d 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. import vibe.db.postgresql;
  18. import vibe.data.bson;
  19. //import vibe.data.json;
  20. // https://github.com/vibe-d/vibe.d/blob/master/source/vibe/vibe.d
  21. PostgresClient client;
  22. /* test unproper arguments order */
  23. /* do not */
  24. /*
  25. alias test_key1 = int;
  26. alias test_key2 = int;
  27. string test_args_types_mismash(test_key1 x, test_key2 y){
  28. return ( to!string(x) ~ " + " ~ to!string(y) ~ " = " ~ to!string( x + y ) );
  29. }
  30. */
  31. /* do not */
  32. /*
  33. import std.typecons : Typedef;
  34. alias test_key5 = Typedef!int;
  35. alias test_key6 = Typedef!int;
  36. string test_args_types_mismash(test_key5 x, test_key6 y){
  37. return ( to!string(x) ~ " + " ~ to!string(y) ~ " = " ~ to!string( x + y ) );
  38. }
  39. */
  40. /* do like */
  41. import std.typecons : Typedef;
  42. alias test_key5 = Typedef!(int, int.init, "key5");
  43. alias test_key6 = Typedef!(int, int.init, "key6");
  44. string test_args_types_mismash(test_key5 x, test_key6 y){
  45. return ( to!string(x) ~ " + " ~ to!string(y) ~ " = " ~ to!string( x + y ) );
  46. }
  47. /* or - do like */
  48. struct test_key3 { int v; }
  49. struct test_key4 { int v; }
  50. string test_args_types_mismash2(test_key3 x, test_key4 y){
  51. return ( to!string(x.v) ~ " + " ~ to!string(y.v) ~ " = " ~ to!string( x.v + y.v ) );
  52. }
  53. void main(){
  54. auto settings = new HTTPServerSettings;
  55. settings.port = 8080;
  56. settings.bindAddresses = ["::1", "127.0.0.1"];
  57. //auto fsettings = new HTTPFileServerSettings;
  58. //fsettings.serverPathPrefix = "/static";
  59. auto router = new URLRouter;
  60. //router.get("/", &index);
  61. ////router.get("static/*", serverStaticFiles("public/", fsettings) );
  62. //router.get("/", staticTemplate!"index.html");
  63. router.get("/", serveStaticFile("public/index.html") );
  64. router.get("/ws", handleWebSockets(&ws_handle) );
  65. router.get("/test", &test);
  66. router.get("*", serveStaticFiles("public/"));
  67. //auto listener = listenHTTP(settings, &hello);
  68. auto listener = listenHTTP(settings, router);
  69. scope (exit){
  70. listener.stopListening();
  71. }
  72. logInfo("Please open http://127.0.0.1:8080/ in your browser.");
  73. runApplication();
  74. }
  75. void ws_handle(scope WebSocket sock){
  76. // simple echo server + :)
  77. while(sock.connected){
  78. auto msg = sock.receiveText();
  79. sock.send(msg ~ " :)");
  80. }
  81. }
  82. /*
  83. void index(HTTPServerRequest req, HTTPServerResponse res){
  84. res.writeBody("Hello, World!");
  85. }
  86. */
  87. /*
  88. void hello(HTTPServerRequest req, HTTPServerResponse res){
  89. res.writeBody("Hello, World!");
  90. }
  91. */
  92. void test_pg_conn_driver(){
  93. client.pickConnection( (scope conn){
  94. immutable result = conn.execStatement(
  95. "SELECT 123 as first_num, 567 as second_num, 'abc'::text as third_text " ~
  96. "UNION ALL " ~
  97. "SELECT 890, 233, 'fgh'::text as third_text",
  98. ValueFormat.BINARY
  99. );
  100. assert(result[0]["second_num"].as!PGinteger == 567);
  101. assert(result[1]["third_text"].as!PGtext == "fgh");
  102. foreach (val; rangify(result[0])){
  103. writeln("Found entry: ", val.as!Bson.toJson);
  104. }
  105. } );
  106. }
  107. void test(HTTPServerRequest req, HTTPServerResponse res){
  108. auto cache = memcachedConnect("127.0.0.1:11211");
  109. /* test unproper arguments order */
  110. /* do not */
  111. /*
  112. test_key1 x = 5;
  113. test_key2 y = 2;
  114. //string r1 = test_args_types_mismash(x, y); // proper arguments order
  115. 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..
  116. writeln("r1 = ", r1);
  117. */
  118. /* do not */
  119. /*
  120. test_key5 x = 5;
  121. test_key6 y = 2;
  122. //string r3 = test_args_types_mismash(x, y); // proper arguments order
  123. 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..
  124. writeln("r3 = ", r3);
  125. */
  126. /* do like */
  127. test_key5 x = 5;
  128. test_key6 y = 2;
  129. string r3 = test_args_types_mismash(x, y); // proper arguments order
  130. //string r3 = test_args_types_mismash(y, x); // unproper - this not compiles
  131. writeln("r3 = ", r3);
  132. /* or - do like */
  133. test_key3 x2 = test_key3(5);
  134. test_key4 y2 = test_key4(2);
  135. string r2 = test_args_types_mismash2(x2, y2); // proper arguments order
  136. //string r2 = test_args_types_mismash2(y2, x2); // unproper - this not compiles
  137. writeln("r2 = ", r2);
  138. Language Lang = Language.uk;
  139. writeln("tr 1 = ", Tr(Lang, TKey.hello));
  140. writeln("tr 2 = ", Tr(Lang, TKey.welcome, ["username"], 0) );
  141. writeln("tr 3 = ", Tr(Lang, TKey.apples, [], 1) );
  142. writeln("tr 3 = ", Tr(Lang, TKey.apples, [], 2) ) ;
  143. writeln("tr 3 = ", Tr(Lang, TKey.apples, [], 5) );
  144. writeln("tr 4 = ", Tr(Lang, TKey.apples_n_oranges, ["6", "7"], 0) );
  145. writeln("get test1 = ", cache.get!string("test1"));
  146. string v1 = "value1 = 🔥🦀";
  147. if(cache.store("test1", v1) == RETURN_STATE.SUCCESS ){
  148. writeln("stored successfully");
  149. writeln("get stored: ", cache.get!string("test1") );
  150. }else{
  151. writeln("not stored");
  152. }
  153. string result = cache.get!string("test1");
  154. writeln("get test1 = ", result);
  155. writeln(cache.del("test1"));
  156. // https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
  157. // https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
  158. //client = new PostgresClient("host=localhost port=5432 dbname=mydb user=usename password=pass connect_timeout=5", 100);
  159. client = new PostgresClient("host=195.201.41.112 port=5432 dbname=t4x_parser_data user=admin4 password=Ftsn3rT5YA6ZDWEywfmXekKCS8a4cNxp connect_timeout=5", 100);
  160. test_pg_conn_driver();
  161. Mustache mustache2;
  162. auto context2 = new Mustache.Context;
  163. mustache2.path = "priv/folder2";
  164. mustache2.ext = "dtl";
  165. context2["param2"] = "blah blah blah ";
  166. Mustache mustache;
  167. auto context = new Mustache.Context;
  168. mustache.path = "priv";
  169. mustache.ext = "dtl";
  170. //context.useSection("boolean");
  171. //assert(mustache.renderString(" {{#boolean}}YES{{/boolean}}\n {{#boolean}}GOOD{{/boolean}}\n", context) == " YES\n GOOD\n");
  172. //{{#repo}}<b>{{name}}</b>{{/repo}}
  173. //{{^repo}}No repos :({{/repo}}
  174. // to
  175. //No repos :(
  176. context["lang"] = "en";
  177. context["number1"] = 42;
  178. context.useSection("maybe1");
  179. context["part1"] = mustache2.render("part1", context2);
  180. context["result1"] = "Hello, World!\n" ~ result;
  181. res.headers["Content-Type"] = "text/html; charset=utf-8";
  182. //res.writeBody("Hello, World!\n" ~ result);
  183. res.writeBody( mustache.render("main", context) );
  184. }