app.d 14 KB

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