app.d 16 KB

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