moonbridge

view example_application.lua @ 71:4628be0a7b98

Updated reference to include xread, xread_nb, write_nb
author jbe
date Sat Apr 04 21:28:30 2015 +0200 (2015-04-04)
parents 54d488774467
children 7014436d88ea
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 = 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_body_size_limit = 16*1024*1024*1024, -- allow big file uploads
63 request_header_timeout = 360, -- request headers must be sent within 6 minutes (if nil, defaults to timeout below)
64 timeout = 1800 -- request body and response must be sent within 30 minutes
65 },
66 function(request)
68 local function error_response(status)
69 request:send_status(status)
70 request:send_header("Content-Type", "text/html")
71 request:send_data("<html>\n<head><title>", status, "</title></head>\n<body><h1>", status, "</h1></body>\n</html>\n")
72 request:finish()
73 end
75 if request.method == "GET" or request.method == "HEAD" then
77 if request.path == "" then
78 request:send_status("303 See Other")
79 request:send_header("Location", "http://" .. request.headers_value.host .. "/example_webpage.html")
81 else
82 local document_name = request.path
83 local document_extension = string.match(document_name, "%.([^.])$")
84 local document = documents[document_name] -- loads file contents from memory
85 if document then
86 request:send_status("200 OK")
87 if document_extension == "html" then
88 request:send_header("Content-Type", "text/html; charset=UTF-8")
89 elseif document_extension == "css" then
90 request:send_header("Content-Type", "text/css; charset=UTF-8")
91 end
92 request:send_data(document)
93 else
94 error_response("404 Not Found")
95 end
97 end
99 elseif request.method == "POST" then
101 if request.path == "post_example" then
102 local files = {}
103 do
104 local file
105 request:stream_post_param("files", function(chunk, field_name, meta)
106 if meta then
107 file = {
108 file_name = meta.file_name,
109 content_type = meta.content_type,
110 length = 0
111 }
112 end
113 if chunk then
114 file.length = file.length + #chunk
115 else
116 files[#files+1] = file
117 end
118 end)
119 end
121 request:send_status("200 OK")
122 request:send_header("Content-Type", "text/html; chatset=UTF-8")
123 request:send_data("<html>\n<head>\n")
124 request:send_data('<link href="example_webpage.css" rel="stylesheet" type="text/css">\n')
125 request:send_data("<title>Moonbridge Network Server for Lua Applications &ndash; Example Application</title>\n")
126 request:send_data("</head>\n<body>\n")
127 request:send_data("<h1>Moonbridge Network Server for Lua &ndash; Example Application</h1>\n")
128 request:send_data("<h2>POST request successful</h2>\n")
129 request:send_data('<table>\n<thead><th>File name</th><th>Content type</th><th class="numeric">Bytes received</th></thead>\n<tbody>\n')
130 for i, file in ipairs(files) do
131 request:send_data("<tr>")
132 request:send_data("<td>", http.encode_html(file.file_name or "(unknown)"), "</td>")
133 request:send_data("<td>", http.encode_html(file.content_type or "(unknown)"), "</td>")
134 request:send_data('<td class="numeric">', http.encode_html(tostring(file.length)), "</td>")
135 request:send_data("</tr>\n")
136 end
137 request:send_data("</tbody>\n</table>\n")
138 request:send_data("<p>Submitted comment: ", http.encode_html(request.post_params.comment), "</p>\n")
139 request:send_data("</body>\n</html>\n")
141 else
142 error_response("404 Not Found")
144 end
146 else
147 error_response("405 Method not allowed")
149 end
151 -- returning false causes termination of current process (and re-forking)
152 return true
153 end),
155 -- executed on process termination
156 finish = function()
157 -- e.g. close database connection
158 end
159 }

Impressum / About Us