app.d 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. alias uint8 = ubyte;
  2. //import vibe.vibe;
  3. // https://github.com/vibe-d/vibe.d/blob/master/source/vibe/vibe.d
  4. import vibe.core.core;
  5. import vibe.http.router;
  6. import vibe.http.server;
  7. import vibe.http.fileserver;
  8. import vibe.http.websockets;
  9. import vibe.core.log;
  10. import ws_bert_login : ws_bert_handle, login_test; // login - logged - logout -- via ws with bert ++ memcached + postgresql for sessions
  11. import memcached_test : memcached_test; // memcached example
  12. import mutex_test : init_mutex, ws_mutex_handle; // mutex example
  13. import std.string;
  14. import std.array;
  15. import std.algorithm;
  16. import std.variant : Variant;
  17. import std.datetime : SysTime, Clock;
  18. import std.concurrency : spawn;
  19. import vibe.core.concurrency;
  20. import vibe.core.core : Task, sleep;
  21. import core.time : Duration, dur;
  22. /+
  23. Tid user_state_pid; // with dlang message passing like in Erlang -- err here - do not tick every 30 sec
  24. struct UserState2{
  25. string client_id;
  26. SysTime last_online_at;
  27. }
  28. alias UserStateMap2 = UserState2[string];
  29. UserStateMap2 user_states;
  30. struct Msg1{
  31. uint8 command;
  32. }
  33. /*
  34. struct Msg2{
  35. Tid sender;
  36. string client_id;
  37. }
  38. */
  39. struct Msg21{
  40. uint8 command;
  41. string client_id;
  42. }
  43. struct Msg3{
  44. Tid sender;
  45. uint8 command;
  46. string client_id;
  47. }
  48. +/
  49. import std.conv : to;
  50. import tr;
  51. import mustache;
  52. alias MustacheEngine!(string) Mustache;
  53. import vibe.db.postgresql;
  54. import vibe.data.bson;
  55. //import vibe.data.json;
  56. // https://github.com/vibe-d/vibe.d/blob/master/source/vibe/vibe.d
  57. PostgresClient[] clients;
  58. import settings_toml; // get settings from settings.toml, settings keys, settings validation etc
  59. /* test unproper arguments order */
  60. /* do not */
  61. /*
  62. alias test_key1 = int;
  63. alias test_key2 = int;
  64. string test_args_types_mismash(test_key1 x, test_key2 y){
  65. return ( to!string(x) ~ " + " ~ to!string(y) ~ " = " ~ to!string( x + y ) );
  66. }
  67. */
  68. /* do not */
  69. /*
  70. import std.typecons : Typedef;
  71. alias test_key5 = Typedef!int;
  72. alias test_key6 = Typedef!int;
  73. string test_args_types_mismash(test_key5 x, test_key6 y){
  74. return ( to!string(x) ~ " + " ~ to!string(y) ~ " = " ~ to!string( x + y ) );
  75. }
  76. */
  77. /* do like */
  78. import std.typecons : Typedef;
  79. alias test_key5 = Typedef!(int, int.init, "key5");
  80. alias test_key6 = Typedef!(int, int.init, "key6");
  81. string test_args_types_mismash(test_key5 x, test_key6 y){
  82. return ( to!string(x) ~ " + " ~ to!string(y) ~ " = " ~ to!string( x + y ) );
  83. }
  84. /* or - do like */
  85. struct test_key3 { int v; }
  86. struct test_key4 { int v; }
  87. string test_args_types_mismash2(test_key3 x, test_key4 y){
  88. return ( to!string(x.v) ~ " + " ~ to!string(y.v) ~ " = " ~ to!string( x.v + y.v ) );
  89. }
  90. /+
  91. void startCleanupTask2(){ // with message passing (actors model like in Erlang); worker -- err here - do not tick every 30 sec
  92. writeln("startCleanupTask2();");
  93. while(true){
  94. /*
  95. auto msg = receiveOnly!(Tid, uint8, string)();
  96. writeln("int: ", msg[1]); // got int
  97. writeln("string: ", msg[2]); // got string
  98. msg[0].send(thisTid); // send message back
  99. */
  100. receive(
  101. (Msg1 msg){
  102. if(msg.command == 0){ // 0 for delete inactive clients
  103. writeln("221 ", user_states.keys); // show all keys
  104. auto now = Clock.currTime();
  105. auto toRemove = user_states.byKey
  106. .filter!(k => (now - user_states[k].last_online_at).total!"seconds" > 30) // clean every 30 seconds
  107. .array;
  108. foreach(client_id; toRemove){
  109. user_states.remove(client_id);
  110. }
  111. }
  112. return true;
  113. },
  114. /*
  115. (Msg2 msg){
  116. msg.sender
  117. msg.client_id
  118. },
  119. */
  120. (Msg21 msg){
  121. if(msg.command == 2){ // 2 for add_or_upd client
  122. //auto now = Clock.currTime();
  123. user_states[msg.client_id] = UserState2( client_id : msg.client_id, last_online_at : Clock.currTime() );
  124. //writeln("user_states.length = ", user_states.length);
  125. //writeln(user_states.keys); // show all keys
  126. //writeln(user_states.values); // show all values
  127. }else if(msg.command == 3){ // 3 for delete client
  128. user_states.remove(msg.client_id);
  129. }
  130. return true;
  131. },
  132. (Msg3 msg){
  133. if(msg.command == 1){ // 1 for check is new client
  134. //msg.sender.send( (msg.client_id in user_states) !is null ); // is client exists
  135. msg.sender.send( (msg.client_id in user_states) is null ); // is new client
  136. }
  137. //}else if(msg.command == 4){ // 4 for get length
  138. // msg.sender.send(user_states.length);
  139. //}
  140. return true;
  141. },
  142. (Variant v){
  143. writeln("got unexpected Variant v: ", v);
  144. return true;
  145. }
  146. );
  147. }
  148. }
  149. void startCleanupTask21(){ // clean daemon
  150. while(true){
  151. writeln("do clean!");
  152. writeln("273", user_states.keys); // show all keys
  153. user_state_pid.send(Msg1(0)); // 0 for delete inactive clients
  154. writeln("275", user_states.keys); // show all keys
  155. sleep(dur!"seconds"(30)); // clean every 30 seconds
  156. }
  157. }
  158. +/
  159. void main(){
  160. read_settings_toml(); // read settings, settings validation
  161. uint conn_num = cast(uint) toml_s[s_toml_db][s_toml_db_conn_num].integer();
  162. uint8 i = cast(uint8) conn_num;
  163. // https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
  164. // https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
  165. //client = new PostgresClient("host=localhost port=5432 dbname=mydb user=username password=pass connect_timeout=5", 100);
  166. while(i > 0){
  167. //writeln("i = ", i);
  168. clients ~= new PostgresClient( "host=" ~ toml_s[s_toml_db][s_toml_db_host].str() ~
  169. " port=" ~ toml_s[s_toml_db][s_toml_db_port].str() ~
  170. " dbname=" ~ toml_s[s_toml_db][s_toml_db_name].str() ~
  171. " user=" ~ toml_s[s_toml_db][s_toml_db_user].str() ~
  172. " password=" ~ toml_s[s_toml_db][s_toml_db_pass].str() ~
  173. " connect_timeout=" ~ toml_s[s_toml_db][s_toml_db_conn_timeout].str(),
  174. conn_num,
  175. (scope Connection conn){
  176. conn.prepareEx("get_city_by_id", "SELECT id, name, population FROM test WHERE id = $1");
  177. } );
  178. i--;
  179. }
  180. auto settings = new HTTPServerSettings;
  181. //settings.port = 8080;
  182. //settings.bindAddresses = ["::1", "127.0.0.1"];
  183. settings.port = cast(ushort)toml_s[s_toml_http][s_toml_http_port].integer();
  184. settings.bindAddresses = [ toml_s[s_toml_http][s_toml_http_host].str().idup ];
  185. //auto fsettings = new HTTPFileServerSettings;
  186. //fsettings.serverPathPrefix = "/static";
  187. auto router = new URLRouter;
  188. //router.get("/", &index);
  189. ////router.get("static/*", serverStaticFiles("public/", fsettings) );
  190. //router.get("/", staticTemplate!"index.html");
  191. router.get("/", serveStaticFile("public/index.html") ); // static html + ws echo example
  192. router.get("/ws", handleWebSockets(&ws_handle) ); // static html + ws echo example
  193. router.get("/ws_mutex", handleWebSockets(&ws_mutex_handle) ); // static html + ws echo example + mutex example
  194. router.get("/test", &test); // Mustache template + postgresql pool example
  195. router.get("/ws_login_test", handleWebSockets(&ws_bert_handle) ); // ws handler begins from "ws_" and next same http page path // login - logged - logout -- via ws with bert
  196. router.get("/login_test", &login_test); // login - logged - logout -- via ws with bert
  197. router.get("/memcached_test", &memcached_test); // memcached example
  198. router.get("*", serveStaticFiles("public/"));
  199. //auto listener = listenHTTP(settings, &hello);
  200. auto listener = listenHTTP(settings, router);
  201. scope (exit){
  202. listener.stopListening();
  203. }
  204. init_mutex(); // mutex example
  205. /*
  206. user_state_pid = spawn(&startCleanupTask2); // worker - clean inactive user state -- with message passing (actors model like in Erlang) -- err here - do not tick every 30 sec
  207. spawn(&startCleanupTask21); // daemon for worker
  208. //auto clean_daemon_pid = spawn(&startCleanupTask21, thisTid);
  209. sleep(dur!"seconds"(5));
  210. */
  211. logInfo("Please open http://127.0.0.1:8080/ in your browser.");
  212. runApplication();
  213. }
  214. void ws_handle(scope WebSocket sock){
  215. // simple echo server + :)
  216. //writeln("sock = ", sock); // vibe.http.websockets.WebSocket
  217. //writeln("sock.request = ", sock.request); // GET /ws?client_id=YaHoAnZo3JPYOwX7yn35 HTTP/1.1
  218. //writeln("sock.request.requestPath = ", sock.request.requestPath); // /ws
  219. //writeln("sock.request.queryString = ", sock.request.queryString); // client_id=YaHoAnZo3JPYOwX7yn35
  220. //writeln("sock.request.query = ", sock.request.query); // ["client_id": "YaHoAnZo3JPYOwX7yn35"]
  221. string client_id = "";
  222. if("client_id" in sock.request.query){
  223. client_id = sock.request.query["client_id"];
  224. //writeln("found client_id = ", client_id);
  225. }else{
  226. //writeln("client_id not found");
  227. throw new StringException("client_id not found");
  228. }
  229. /*
  230. user_state_pid.send(Msg3(thisTid, 1, client_id)); // 1 for check is client exists -- err here - do not tick every 30 sec
  231. //enforce(receiveOnly!Tid() == tid);
  232. //auto response = receiveOnly!(Tid, bool)(); // response[0] = Tid of worker; response[1] = bool result
  233. bool is_new_client = receiveOnly!bool();
  234. if(is_new_client){
  235. writeln("New client connected: ", client_id);
  236. }else{
  237. writeln("Reconnection from existing client: ", client_id);
  238. }
  239. user_state_pid.send(Msg21(2, client_id));
  240. */
  241. /*
  242. try{
  243. while(sock.connected){
  244. auto msg = sock.receiveText();
  245. sock.send(msg ~ " :)");
  246. }
  247. }catch(Exception ex){
  248. writeln("disconnected client_id = ", client_id);
  249. writeln("Error: ", ex.msg); // Error: Connection closed while reading message.
  250. }
  251. */
  252. while(sock.waitForData()){
  253. auto msg = sock.receiveText();
  254. sock.send(msg ~ " :)");
  255. }
  256. writeln("after disconnect"); // now shows
  257. }
  258. /*
  259. void index(HTTPServerRequest req, HTTPServerResponse res){
  260. res.writeBody("Hello, World!");
  261. }
  262. */
  263. /*
  264. void hello(HTTPServerRequest req, HTTPServerResponse res){
  265. res.writeBody("Hello, World!");
  266. }
  267. */
  268. /*
  269. void test_pg_conn_driver(){ // https://github.com/denizzzka/vibe.d.db.postgresql/blob/master/example/example.d#L13
  270. client.pickConnection( (scope conn){
  271. immutable result = conn.exec(
  272. "SELECT 123 as first_num, 567 as second_num, 'abc'::text as third_text " ~
  273. "UNION ALL " ~
  274. "SELECT 890, 233, 'fgh'::text as third_text",
  275. ValueFormat.BINARY
  276. );
  277. assert(result[0]["second_num"].as!PGinteger == 567);
  278. assert(result[1]["third_text"].as!PGtext == "fgh");
  279. foreach (val; rangify(result[0])){
  280. writeln("Found entry: ", val.as!Bson.toJson);
  281. }
  282. } );
  283. }
  284. */
  285. string get_all_cities(){
  286. return "SELECT id, name, population FROM test ORDER BY id";
  287. }
  288. void test_pg_conn_driver_queries(){
  289. /*
  290. client.pickConnection( (scope conn){
  291. QueryParams params; // https://github.com/denizzzka/dpq2/blob/master/src/dpq2/args.d#L15
  292. params.preparedStatementName = "get_city_by_id";
  293. 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
  294. conn.prepareEx(params.preparedStatementName, "SELECT id, name, population FROM test WHERE id = $1"); // get_city_by_id
  295. auto result1 = conn.execPrepared(params);
  296. writeln("id: ", result1[0]["id"].as!PGinteger);
  297. writeln("name: ", result1[0]["name"].as!PGtext);
  298. writeln("population: ", result1[0]["population"].as!PGinteger);
  299. //conn.prepareEx("q1", "UPDATE test SET name = $1, population = $2 WHERE id = $3"); // update_city_by_id
  300. //immutable result1 = conn.execPrepared("", ValueFormat.BINARY);
  301. destroy(conn);
  302. } );
  303. */
  304. //auto conn = clients[0].lockConnection(); // todo think is this correct to take index 0 here
  305. clients[0].pickConnection( (scope conn){ // todo think is this correct to take index 0 here
  306. QueryParams params; // https://github.com/denizzzka/dpq2/blob/master/src/dpq2/args.d#L15
  307. params.preparedStatementName = "get_city_by_id";
  308. 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
  309. //conn.prepareEx(params.preparedStatementName, "SELECT id, name, population FROM test WHERE id = $1"); // get_city_by_id
  310. auto result1 = conn.execPrepared(params);
  311. writeln("id: ", result1[0]["id"].as!PGinteger);
  312. writeln("name: ", result1[0]["name"].as!PGtext);
  313. writeln("population: ", result1[0]["population"].as!PGinteger);
  314. //conn.prepareEx("q1", "UPDATE test SET name = $1, population = $2 WHERE id = $3"); // update_city_by_id
  315. //immutable result1 = conn.execPrepared("", ValueFormat.BINARY);
  316. destroy(conn);
  317. } );
  318. }
  319. /*
  320. "SELECT id, name, population FROM test ORDER BY id"
  321. "UPDATE test SET name = $1, population = $2 WHERE id = $3", [City_Name, City_Pop, City_Id]
  322. "INSERT INTO test (name, population) VALUES ($1, $2)", [City_Name, City_Pop]
  323. "INSERT INTO test (name, population) VALUES ($1, $2) RETURNING id", [City_Name, City_Pop]
  324. "DELETE FROM test WHERE id = $1", [City_Id]
  325. */
  326. void test(HTTPServerRequest req, HTTPServerResponse res){
  327. /* test unproper arguments order */
  328. /* do not */
  329. /*
  330. test_key1 x = 5;
  331. test_key2 y = 2;
  332. //string r1 = test_args_types_mismash(x, y); // proper arguments order
  333. 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..
  334. writeln("r1 = ", r1);
  335. */
  336. /* do not */
  337. /*
  338. test_key5 x = 5;
  339. test_key6 y = 2;
  340. //string r3 = test_args_types_mismash(x, y); // proper arguments order
  341. 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..
  342. writeln("r3 = ", r3);
  343. */
  344. /* do like */
  345. test_key5 x = 5;
  346. test_key6 y = 2;
  347. string r3 = test_args_types_mismash(x, y); // proper arguments order
  348. //string r3 = test_args_types_mismash(y, x); // unproper - this not compiles
  349. writeln("r3 = ", r3);
  350. /* or - do like */
  351. test_key3 x2 = test_key3(5);
  352. test_key4 y2 = test_key4(2);
  353. string r2 = test_args_types_mismash2(x2, y2); // proper arguments order
  354. //string r2 = test_args_types_mismash2(y2, x2); // unproper - this not compiles
  355. writeln("r2 = ", r2);
  356. // i18n example
  357. Language Lang = Language.uk;
  358. writeln("tr 1 = ", Tr(Lang, TKey.hello));
  359. writeln("tr 2 = ", Tr(Lang, TKey.welcome, ["username"], 0) );
  360. writeln("tr 3 = ", Tr(Lang, TKey.apples, [], 1) );
  361. writeln("tr 3 = ", Tr(Lang, TKey.apples, [], 2) ) ;
  362. writeln("tr 3 = ", Tr(Lang, TKey.apples, [], 5) );
  363. writeln("tr 4 = ", Tr(Lang, TKey.apples_n_oranges, ["6", "7"], 0) );
  364. //test_pg_conn_driver();
  365. test_pg_conn_driver_queries();
  366. Mustache mustache2;
  367. auto context2 = new Mustache.Context;
  368. mustache2.path = "priv/folder2";
  369. mustache2.ext = "dtl";
  370. context2["param2"] = "blah blah blah ";
  371. Mustache mustache;
  372. auto context = new Mustache.Context;
  373. mustache.path = "priv";
  374. mustache.ext = "dtl";
  375. //context.useSection("boolean");
  376. //assert(mustache.renderString(" {{#boolean}}YES{{/boolean}}\n {{#boolean}}GOOD{{/boolean}}\n", context) == " YES\n GOOD\n");
  377. //{{escaped_html_tags}}
  378. //{{{not_escaped_html_tags}}}
  379. //{{#repo}}<b>{{name}}</b>{{/repo}}
  380. //{{^repo}}No repos :({{/repo}}
  381. // to
  382. //No repos :(
  383. context["lang"] = "en";
  384. context["number1"] = 42;
  385. context.useSection("maybe1");
  386. context["part1"] = mustache2.render("part1", context2);
  387. context["result1"] = "Hello, World!\n";
  388. res.headers["Content-Type"] = "text/html; charset=utf-8";
  389. //res.writeBody("Hello, World!\n" ~ result);
  390. res.writeBody( mustache.render("main", context) );
  391. }