sq-server.nut 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 2013 by Domingo Alvarez Duarte <[email protected]>
  3. *
  4. * Licensed under GPLv3, see http://www.gnu.org/licenses/gpl.html.
  5. */
  6. local globals = getroottable();
  7. if(!table_rawget(globals, "APP_CODE_FOLDER", false)) ::APP_CODE_FOLDER <- ".";
  8. WIN32 <- os.getenv("WINDIR") != null;
  9. /*
  10. local _old_import = import;
  11. ::__imported_files <- {};
  12. ::import = function(fn){
  13. }
  14. */
  15. AT_DEV_DBG <- true;
  16. function getUserCallbackSetup(fn){
  17. local fd = file(APP_CODE_FOLDER + "/" + fn, "r");
  18. local code = fd.read(fd.len());
  19. fd.close();
  20. local extra_code = format("APP_CODE_FOLDER <- \"%s\";\n", APP_CODE_FOLDER);
  21. local checkGlobal = function(gv)
  22. {
  23. if (table_rawin(globals, gv)){
  24. extra_code += format(gv + " <- \"%s\";\n", table_rawget(globals, gv));
  25. } else extra_code += gv + " <- false;\n";
  26. }
  27. checkGlobal("GLOBAL_PASSWORD_DOMAIN");
  28. checkGlobal("GLOBAL_MD5_PASSWORD");
  29. checkGlobal("VIEW_MD5_PASSWORD");
  30. checkGlobal("EDIT_MD5_PASSWORD");
  31. checkGlobal("EDIT_MD5_PASSWORD");
  32. if (table_rawget(globals, "AT_DEV_DBG", false)){
  33. extra_code += "AT_DEV_DBG <- true;\n";
  34. } else extra_code += "AT_DEV_DBG <- false;\n";
  35. code = extra_code + "\n" + code;
  36. if(table_rawin(globals, "addExtraCodeToUserCallbackSetup"))
  37. {
  38. code = addExtraCodeToUserCallbackSetup() + code;
  39. }
  40. return compilestring( code );
  41. }
  42. local mongoose_start_params = {
  43. error_log_file = "sq-mongoose.log",
  44. listening_ports = "8080",
  45. document_root = "./s",
  46. //num_threads = 50,
  47. //enable_keep_alive = "yes",
  48. //enable_tcp_nodelay = "yes",
  49. //request_timeout_ms = "30000",
  50. //cgi_extensions = "lua",
  51. //cgi_interpreter = "/usr/bin/lua",
  52. //cgi_interpreter = "C:\\Lua\\5.1\\lua.exe",
  53. ssl_certificate = "axTLS.x509_512.pem",
  54. //"ssl_certificate", "axTLS.x509_1024.pem",
  55. ssl_chain_file = "axTLS_x509_512.cer",
  56. extra_mime_types = ".xsl=application/xml,.json=application/json; charset=utf-8",
  57. master_plugin = function(){
  58. //companies_uk_db_mem_global <- SQLite3("file:companies_uk_db?mode=memory&cache=shared");
  59. //companies_uk_db_mem_global.restore("./companies-uk/companies-uk-2014-07.db");
  60. //ourbiz_db_mem_global <- SQLite3("file:ourbiz_db?mode=memory&cache=shared");
  61. //ourbiz_db_mem_global.restore("/home/mingo/dev/FrontAccountLua/ourbiz.db");
  62. debug_print("done master_plugin\n");
  63. },
  64. master_plugin_exit = function(){
  65. //companies_uk_db_mem_global.close();
  66. //ourbiz_db_mem_global.close();
  67. debug_print("done master_plugin_exit\n");
  68. },
  69. //functions to be used by each independent lua vm
  70. user_callback_setup = getUserCallbackSetup("sq-server-plugin.nut"),
  71. user_callback_exit = function(){
  72. debug_print("done user_callback_exit\n");
  73. },
  74. user_callback = function(event, request){
  75. //debug_print("\nevent :\n", event);
  76. if(event == "MG_NEW_REQUEST"){
  77. if(GLOBAL_MD5_PASSWORD)
  78. {
  79. if(request.info.uri == "/SQ/logout")
  80. {
  81. request.close_session();
  82. request.print(format("HTTP/1.1 302 Found\r\nLocation: http%s://%s\r\n\r\n",
  83. request.info.is_ssl ? "s" : "", request.info.http_headers.Host));
  84. return true;
  85. }
  86. /*
  87. if(!request.check_password(GLOBAL_MD5_PASSWORD)) {
  88. request.send_authorization_request(GLOBAL_PASSWORD_DOMAIN);
  89. return true;
  90. }*/
  91. }
  92. //debug_print("\n", request.get_option("num_threads"), request.get_conn_buf());
  93. if(AT_DEV_DBG || !table_get(this, "handle_request", false)) {
  94. //when developing we reload everything on each request
  95. dofile(APP_CODE_FOLDER + "/sq-server-plugin.nut");
  96. }
  97. local result;
  98. try {
  99. //debug_print("\nHttp :\n", request.info.uri);
  100. result = handle_request(request);
  101. }
  102. catch(exep){
  103. result = send_http_error_500(request, exep);
  104. }
  105. if(AT_DEV_DBG && table_get(this, "onDevCleanup", false)) {
  106. //when developing if we need to cleanup something here is the place
  107. onDevCleanup();
  108. }
  109. return result;
  110. }
  111. else if(event == "MG_EVENT_LOG"){
  112. //debug_print("\n", request.info.log_message);
  113. //return true;
  114. return false;
  115. }
  116. else if(event == "MG_HTTP_ERROR"
  117. || event == "MG_INIT_SSL"){
  118. return false;
  119. }
  120. },
  121. }
  122. local mg;
  123. function appServerStart(port, document_root){
  124. mg = Mongoose();
  125. //mongoose_start_params.num_threads <- "2";
  126. mongoose_start_params.listening_ports = port;
  127. mongoose_start_params.document_root = document_root;
  128. mg.show_errors_on_stdout(true);
  129. mg.start(mongoose_start_params);
  130. }
  131. //appServerStart(8087, "./s");
  132. //stdin.readn('c');
  133. function appServerStop(){
  134. if(mg) {
  135. mg.stop();
  136. mg = null;
  137. }
  138. }
  139. //print( lfs.currentdir())
  140. //
  141. // myio = io.open("luadump.txt", "wb")
  142. // myio:write(string.dump(start_params.user_callback))
  143. // myio:close()
  144. // run until the user presses enter
  145. //io.read()
  146. //appServerStop()