moonbridge

view example_application.lua @ 306:25f40c8d5082

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

Impressum / About Us