moonbridge

view example_application.lua @ 53:aaf67ef45c22

Timeout management
author jbe
date Sun Mar 22 13:19:54 2015 +0100 (2015-03-22)
parents 59f485dc48ea
children e3df61bf62f4
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 = "tcp4", port = 8080, localhost = true },
20 -- listen to a tcp version 6 socket
21 { proto = "tcp6", port = 8080, localhost = true },
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 = 2, -- 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 = 1, -- 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_body_size_limit = 16*1024*1024*1024 -- allow big file uploads
63 },
64 function(request)
66 local function error_response(status)
67 request:send_status(status)
68 request:send_header("Content-Type", "text/html")
69 request:send_data("<html>\n<head><title>", status, "</title></head>\n<body><h1>", status, "</h1></body>\n</html>\n")
70 request:finish()
71 end
73 if request.method == "GET" or request.method == "HEAD" then
75 if request.path == "" then
76 request:send_status("303 See Other")
77 request:send_header("Location", "http://" .. request.headers_value.host .. "/example_webpage.html")
79 else
80 local document_name = request.path
81 local document_extension = string.match(document_name, "%.([^.])$")
82 local document = documents[document_name] -- loads file contents from memory
83 if document then
84 request:send_status("200 OK")
85 if document_extension == "html" then
86 request:send_header("Content-Type", "text/html; charset=UTF-8")
87 elseif document_extension == "css" then
88 request:send_header("Content-Type", "text/css; charset=UTF-8")
89 end
90 request:send_data(document)
91 else
92 error_response("404 Not Found")
93 end
95 end
97 elseif request.method == "POST" then
99 if request.path == "post_example" then
100 local files = {}
101 do
102 local file
103 request:stream_post_param("files", function(chunk, field_name, meta)
104 if meta then
105 file = {
106 file_name = meta.file_name,
107 content_type = meta.content_type,
108 length = 0
109 }
110 end
111 if chunk then
112 file.length = file.length + #chunk
113 else
114 files[#files+1] = file
115 end
116 end)
117 end
119 request:send_status("200 OK")
120 request:send_header("Content-Type", "text/html; chatset=UTF-8")
121 request:send_data("<html>\n<head>\n")
122 request:send_data('<link href="example_webpage.css" rel="stylesheet" type="text/css">\n')
123 request:send_data("<title>Moonbridge Network Server for Lua Applications &ndash; Example Application</title>\n")
124 request:send_data("</head>\n<body>\n")
125 request:send_data("<h1>Moonbridge Network Server for Lua &ndash; Example Application</h1>\n")
126 request:send_data("<h2>POST request successful</h2>\n")
127 request:send_data('<table>\n<thead><th>File name</th><th>Content type</th><th class="numeric">Bytes received</th></thead>\n<tbody>\n')
128 for i, file in ipairs(files) do
129 request:send_data("<tr>")
130 request:send_data("<td>", http.encode_html(file.file_name or "(unknown)"), "</td>")
131 request:send_data("<td>", http.encode_html(file.content_type or "(unknown)"), "</td>")
132 request:send_data('<td class="numeric">', http.encode_html(tostring(file.length)), "</td>")
133 request:send_data("</tr>\n")
134 end
135 request:send_data("</tbody>\n</table>\n")
136 request:send_data("<p>Submitted comment: ", http.encode_html(request.post_params.comment), "</p>\n")
137 request:send_data("</body>\n</html>\n")
139 else
140 error_response("404 Not Found")
142 end
144 else
145 error_response("405 Method not allowed")
147 end
149 -- returning false causes termination of current process (and re-forking)
150 return true
151 end),
153 -- executed on process termination
154 finish = function()
155 -- e.g. close database connection
156 end
157 }

Impressum / About Us