app.d 9.3 KB

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