PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/liquidfeedback/moonbridge/example_application.lua

https://gitlab.com/fuzzynemesis/hajaannu
Lua | 166 lines | 108 code | 31 blank | 27 comment | 18 complexity | 1084107d69785b085e58d1fbdc9a953f MD5 | raw file
Possible License(s): Apache-2.0
  1. -- Moonbridge example application
  2. -- invoke with ./moonbridge example_application.lua
  3. --
  4. -- see helloworld.lua for a simpler example
  5. local http = require "moonbridge_http"
  6. -- preparation before forking:
  7. local documents = {}
  8. for i, document_name in ipairs{"example_webpage.html", "example_webpage.css"} do
  9. local file = assert(io.open(document_name))
  10. documents[document_name] = file:read("*a") -- store file contents in memory
  11. file:close()
  12. end
  13. listen{
  14. -- listen to a tcp version 4 socket
  15. --{ proto = "tcp", host = "0.0.0.0", port = 8080 },
  16. -- listen to a tcp version 6 socket
  17. { proto = "tcp", host = "::", port = 8080},
  18. -- listen to a unix domain socket
  19. --{ proto = "local", path = 'socket' },
  20. -- execute the listener regularly (without incoming connection)
  21. --{ proto = "interval", name = "myint", delay = 10, strict = false },
  22. -- desired number of spare (idle) processes
  23. pre_fork = 1, -- number of forks
  24. -- minimum number of processes
  25. min_fork = 4, -- number of forks
  26. -- maximum number of processes (hard limit)
  27. max_fork = 16, -- number of forks
  28. -- delay between creation of spare processes
  29. fork_delay = 0.25, -- seconds
  30. -- delay before retry of failed process creation
  31. fork_error_delay = 2, -- seconds
  32. -- delay between destruction of excessive spare processes
  33. exit_delay = 60, -- seconds
  34. -- idle time after a fork gets terminated
  35. idle_timeout = 0, -- seconds (0 for no timeout)
  36. -- maximum memory consumption before process gets terminated
  37. --memory_limit = 1024*1024, -- bytes
  38. -- preparation of process (executed after fork)
  39. prepare = function()
  40. -- e.g. open database connection
  41. end,
  42. -- connection handler
  43. connect = http.generate_handler(
  44. {
  45. static_headers = {"Server: Moonbridge Example Server"},
  46. request_header_size_limit = 1024*1024, -- maximum size of request headers
  47. request_body_size_limit = 16*1024*1024*1024, -- allow big file uploads
  48. idle_timeout = 65, -- maximum time until receiving the first byte of the request headera
  49. stall_timeout = 60, -- maximum time a client connection may be stalled
  50. request_header_timeout = 120, -- maximum time until receiving the remaining bytes of the request header
  51. response_timeout = 3600, -- time in which request body and response must be sent
  52. maximum_input_chunk_size = 16384, -- tweaks behavior of request-body parser
  53. minimum_output_chunk_size = 1024 -- chunk size for chunked-transfer-encoding
  54. },
  55. function(request)
  56. local function error_response(status)
  57. request:send_status(status)
  58. request:send_header("Content-Type", "text/html")
  59. request:send_data("<html>\n<head><title>", status, "</title></head>\n<body><h1>", status, "</h1></body>\n</html>\n")
  60. request:finish()
  61. end
  62. if request.method == "GET" or request.method == "HEAD" then
  63. if request.path == "" then
  64. request:send_status("303 See Other")
  65. request:send_header("Location", "http://" .. request.headers_value.host .. "/example_webpage.html")
  66. else
  67. local document_name = request.path
  68. local document_extension = string.match(document_name, "%.([^.])$")
  69. local document = documents[document_name] -- loads file contents from memory
  70. if document then
  71. request:send_status("200 OK")
  72. if document_extension == "html" then
  73. request:send_header("Content-Type", "text/html; charset=UTF-8")
  74. elseif document_extension == "css" then
  75. request:send_header("Content-Type", "text/css; charset=UTF-8")
  76. end
  77. request:send_data(document)
  78. else
  79. error_response("404 Not Found")
  80. end
  81. end
  82. elseif request.method == "POST" then
  83. if request.path == "post_example" then
  84. local files = {}
  85. do
  86. local file
  87. request:stream_post_param("files", function(chunk, field_name, meta)
  88. if meta then
  89. file = {
  90. file_name = meta.file_name,
  91. content_type = meta.content_type,
  92. length = 0
  93. }
  94. end
  95. if chunk then
  96. file.length = file.length + #chunk
  97. else
  98. files[#files+1] = file
  99. end
  100. end)
  101. end
  102. request:send_status("200 OK")
  103. request:send_header("Content-Type", "text/html; charset=UTF-8")
  104. request:send_data("<html>\n<head>\n")
  105. request:send_data('<link href="example_webpage.css" rel="stylesheet" type="text/css">\n')
  106. request:send_data("<title>Moonbridge Network Server for Lua Applications &ndash; Example Application</title>\n")
  107. request:send_data("</head>\n<body>\n")
  108. request:send_data("<h1>Moonbridge Network Server for Lua &ndash; Example Application</h1>\n")
  109. request:send_data("<h2>POST request successful</h2>\n")
  110. request:send_data('<table>\n<thead><th>File name</th><th>Content type</th><th class="numeric">Bytes received</th></thead>\n<tbody>\n')
  111. request:consume_input()
  112. for i, file in ipairs(files) do
  113. request:send_data("<tr>")
  114. request:send_data("<td>", http.encode_html(file.file_name or "(unknown)"), "</td>")
  115. request:send_data("<td>", http.encode_html(file.content_type or "(unknown)"), "</td>")
  116. request:send_data('<td class="numeric">', http.encode_html(tostring(file.length)), "</td>")
  117. request:send_data("</tr>\n")
  118. end
  119. request:send_data("</tbody>\n</table>\n")
  120. request:send_data("<p>Submitted comment: ", http.encode_html(request.post_params.comment), "</p>\n")
  121. request:send_data("</body>\n</html>\n")
  122. else
  123. error_response("404 Not Found")
  124. end
  125. else
  126. error_response("405 Method not allowed")
  127. end
  128. -- returning false causes termination of current process (and re-forking)
  129. return true
  130. end),
  131. -- executed on process termination
  132. finish = function()
  133. -- e.g. close database connection
  134. end
  135. }