moonbridge

view example_application.lua @ 0:f6d3b3f70dab

Initial commit
author jbe
date Sun Jan 04 19:30:28 2015 +0100 (2015-01-04)
parents
children 583e2ad140dc
line source
1 -- Moonbridge example application
2 -- invoke with ./moonbridge example_application.lua
4 local http = require "http"
6 local documents = {"example_webpage.html", "example_webpage.css"}
8 listen{
9 -- listen to a tcp version 4 socket
10 { proto = "tcp4", port = 8080, localhost = true },
12 -- listen to a tcp version 6 socket
13 { proto = "tcp6", port = 8080, localhost = true },
15 -- listen to a unix domain socket
16 --{ proto = "local", path = 'socket' },
18 -- execute the listener regularly (without incoming connection)
19 --{ proto = "interval", name = "myint", delay = 10, strict = false },
21 -- desired number of spare (idle) processes
22 pre_fork = 1, -- number of forks
24 -- minimum number of processes
25 min_fork = 2, -- number of forks
27 -- maximum number of processes (hard limit)
28 max_fork = 16, -- number of forks
30 -- delay between creation of spare processes
31 fork_delay = 1, -- seconds
33 -- delay before retry of failed process creation
34 fork_error_delay = 2, -- seconds
36 -- delay between destruction of excessive spare processes
37 exit_delay = 60, -- seconds
39 -- idle time after a fork gets terminated
40 idle_timeout = 0, -- seconds (0 for no timeout)
42 -- maximum memory consumption before process gets terminated
43 memory_limit = 1024*1024, -- bytes
45 -- preparation of process (executed before fork)
46 prepare = function()
47 for i, document in ipairs(documents) do
48 local file = assert(io.open(document))
49 documents[document] = file:read("*a")
50 file:close()
51 end
52 end,
54 -- connection handler
55 connect = http.generate_handler(
56 {
57 static_headers = {"Server: Moonbridge Example Server"},
58 request_body_size_limit = 16*1024*1024*1024 -- allow big file uploads
59 },
60 function(request)
62 if request.method == "GET" or request.method == "HEAD" then
64 if request.path == "/" then
65 request:send_status("303 See Other")
66 request:send_header("Location", "http://" .. request.headers_value.host .. "/example_webpage.html")
68 else
69 local document_name = string.match(request.path, "^/(.*)$")
70 local document_extension = string.match(document_name, "%.([^.])$")
71 local document = documents[string.match(request.path, "^/(.*)$")]
72 if document then
73 request:send_status("200 OK")
75 if document_extension == "html" then
76 request:send_header("Content-Type", "text/html; charset=UTF-8")
77 elseif document_extension == "css" then
78 request:send_header("Content-Type", "text/css; charset=UTF-8")
79 end
80 request:send_data(document)
81 else
82 request:send_status("404 Not Found")
83 request:send_header("Content-Type", "text/html; chatset=UTF-8")
84 request:send_data("<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1></body></html>")
85 end
87 end
89 elseif request.method == "POST" then
91 if request.path == "/post_example" then
92 local files = {}
93 do
94 local file
95 request:stream_post_param("files", function(chunk, field_name, meta)
96 if meta then
97 file = {
98 file_name = meta.file_name,
99 content_type = meta.content_type,
100 length = 0
101 }
102 end
103 if chunk then
104 file.length = file.length + #chunk
105 else
106 files[#files+1] = file
107 end
108 end)
109 end
111 request:send_status("200 OK")
112 request:send_header("Content-Type", "text/html; chatset=UTF-8")
113 request:send_data("<html>\n<head>\n")
114 request:send_data('<link href="example_webpage.css" rel="stylesheet" type="text/css">\n')
115 request:send_data("<title>Moonbridge Network Server for Lua Applications &ndash; Example Application</title>\n")
116 request:send_data("</head>\n<body>\n")
117 request:send_data("<h1>Moonbridge Network Server for Lua &ndash; Example Application</h1>\n")
118 request:send_data("<h2>POST request successful</h2>\n")
119 request:send_data('<table>\n<thead><th>File name</th><th>Content type</th><th class="numeric">Bytes received</th></thead>\n<tbody>\n')
120 for i, file in ipairs(files) do
121 request:send_data("<tr>")
122 request:send_data("<td>", http.encode_html(file.file_name or "(unknown)"), "</td>")
123 request:send_data("<td>", http.encode_html(file.content_type or "(unknown)"), "</td>")
124 request:send_data('<td class="numeric">', http.encode_html(tostring(file.length)), "</td>")
125 request:send_data("</tr>\n")
126 end
127 request:send_data("</tbody>\n</table>\n")
128 request:send_data("<p>Submitted comment: ", http.encode_html(request.post_params.comment), "</p>\n")
129 request:send_data("</body>\n</html>\n")
131 else
132 request:send_status("404 Not Found")
133 request:send_data("<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1></body></html>")
135 end
137 else
138 request:send_status("405 Method not allowed")
140 end
142 -- returning false causes termination of current process (and re-forking)
143 return true
144 end),
146 -- executed on process termination
147 finish = function()
148 end
149 }

Impressum / About Us