app.d 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. /*
  93. void test_pg_conn_driver(){
  94. client.pickConnection( (scope conn){
  95. immutable result = conn.execStatement(
  96. "SELECT 123 as first_num, 567 as second_num, 'abc'::text as third_text " ~
  97. "UNION ALL " ~
  98. "SELECT 890, 233, 'fgh'::text as third_text",
  99. ValueFormat.BINARY
  100. );
  101. assert(result[0]["second_num"].as!PGinteger == 567);
  102. assert(result[1]["third_text"].as!PGtext == "fgh");
  103. foreach (val; rangify(result[0])){
  104. writeln("Found entry: ", val.as!Bson.toJson);
  105. }
  106. } );
  107. }
  108. */
  109. string get_all_cities(){
  110. return "SELECT id, name, population FROM test ORDER BY id";
  111. }
  112. void test_pg_conn_driver_queries(){
  113. client.pickConnection( (scope conn){
  114. conn.prepareStatement("get_city_by_id", "SELECT id, name, population FROM test WHERE id = $1"); // get_city_by_id
  115. QueryParams params;
  116. params.preparedStatementName = "get_city_by_id";
  117. params.argsVariadic(3);
  118. auto result1 = conn.execPreparedStatement(params);
  119. writeln("id: ", result1[0]["id"].as!PGinteger);
  120. writeln("name: ", result1[0]["name"].as!PGtext);
  121. writeln("population: ", result1[0]["population"].as!PGinteger);
  122. //conn.prepareStatement("q1", "UPDATE test SET name = $1, population = $2 WHERE id = $3"); // update_city_by_id
  123. //immutable result1 = conn.execPreparedStatement("", ValueFormat.BINARY);
  124. } );
  125. }
  126. void test(HTTPServerRequest req, HTTPServerResponse res){
  127. auto cache = memcachedConnect("127.0.0.1:11211");
  128. /* test unproper arguments order */
  129. /* do not */
  130. /*
  131. test_key1 x = 5;
  132. test_key2 y = 2;
  133. //string r1 = test_args_types_mismash(x, y); // proper arguments order
  134. 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..
  135. writeln("r1 = ", r1);
  136. */
  137. /* do not */
  138. /*
  139. test_key5 x = 5;
  140. test_key6 y = 2;
  141. //string r3 = test_args_types_mismash(x, y); // proper arguments order
  142. 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..
  143. writeln("r3 = ", r3);
  144. */
  145. /* do like */
  146. test_key5 x = 5;
  147. test_key6 y = 2;
  148. string r3 = test_args_types_mismash(x, y); // proper arguments order
  149. //string r3 = test_args_types_mismash(y, x); // unproper - this not compiles
  150. writeln("r3 = ", r3);
  151. /* or - do like */
  152. test_key3 x2 = test_key3(5);
  153. test_key4 y2 = test_key4(2);
  154. string r2 = test_args_types_mismash2(x2, y2); // proper arguments order
  155. //string r2 = test_args_types_mismash2(y2, x2); // unproper - this not compiles
  156. writeln("r2 = ", r2);
  157. Language Lang = Language.uk;
  158. writeln("tr 1 = ", Tr(Lang, TKey.hello));
  159. writeln("tr 2 = ", Tr(Lang, TKey.welcome, ["username"], 0) );
  160. writeln("tr 3 = ", Tr(Lang, TKey.apples, [], 1) );
  161. writeln("tr 3 = ", Tr(Lang, TKey.apples, [], 2) ) ;
  162. writeln("tr 3 = ", Tr(Lang, TKey.apples, [], 5) );
  163. writeln("tr 4 = ", Tr(Lang, TKey.apples_n_oranges, ["6", "7"], 0) );
  164. writeln("get test1 = ", cache.get!string("test1"));
  165. string v1 = "value1 = 🔥🦀";
  166. if(cache.store("test1", v1) == RETURN_STATE.SUCCESS ){
  167. writeln("stored successfully");
  168. writeln("get stored: ", cache.get!string("test1") );
  169. }else{
  170. writeln("not stored");
  171. }
  172. string result = cache.get!string("test1");
  173. writeln("get test1 = ", result);
  174. writeln(cache.del("test1"));
  175. // https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
  176. // https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
  177. //client = new PostgresClient("host=localhost port=5432 dbname=mydb user=usename password=pass connect_timeout=5", 100);
  178. client = new PostgresClient("host=195.201.41.112 port=5432 dbname=t4x_parser_data user=admin4 password=Ftsn3rT5YA6ZDWEywfmXekKCS8a4cNxp connect_timeout=5", 100);
  179. //test_pg_conn_driver();
  180. test_pg_conn_driver_queries();
  181. Mustache mustache2;
  182. auto context2 = new Mustache.Context;
  183. mustache2.path = "priv/folder2";
  184. mustache2.ext = "dtl";
  185. context2["param2"] = "blah blah blah ";
  186. Mustache mustache;
  187. auto context = new Mustache.Context;
  188. mustache.path = "priv";
  189. mustache.ext = "dtl";
  190. //context.useSection("boolean");
  191. //assert(mustache.renderString(" {{#boolean}}YES{{/boolean}}\n {{#boolean}}GOOD{{/boolean}}\n", context) == " YES\n GOOD\n");
  192. //{{#repo}}<b>{{name}}</b>{{/repo}}
  193. //{{^repo}}No repos :({{/repo}}
  194. // to
  195. //No repos :(
  196. context["lang"] = "en";
  197. context["number1"] = 42;
  198. context.useSection("maybe1");
  199. context["part1"] = mustache2.render("part1", context2);
  200. context["result1"] = "Hello, World!\n" ~ result;
  201. res.headers["Content-Type"] = "text/html; charset=utf-8";
  202. //res.writeBody("Hello, World!\n" ~ result);
  203. res.writeBody( mustache.render("main", context) );
  204. }