moonbridge

view example_application.lua @ 23:6889aa20b19f

Code cleanup in example_application.lua; Removed memory limit in example_application.lua
author jbe
date Thu Jan 29 23:40:38 2015 +0100 (2015-01-29)
parents 1fc0142c32e4
children 19a6d18bf34e
line source
1 -- Moonbridge example application
2 -- invoke with ./moonbridge example_application.lua
4 local http = require "moonbridge_http"
6 local documents = {}
7 for i, document_name in ipairs{"example_webpage.html", "example_webpage.css"} do
8 local file = assert(io.open(document_name))
9 documents[document_name] = file:read("*a")
10 file:close()
11 end
13 listen{
14 -- listen to a tcp version 4 socket
15 { proto = "tcp4", port = 8080, localhost = true },
17 -- listen to a tcp version 6 socket
18 { proto = "tcp6", port = 8080, localhost = true },
20 -- listen to a unix domain socket
21 --{ proto = "local", path = 'socket' },
23 -- execute the listener regularly (without incoming connection)
24 --{ proto = "interval", name = "myint", delay = 10, strict = false },
26 -- desired number of spare (idle) processes
27 pre_fork = 1, -- number of forks
29 -- minimum number of processes
30 min_fork = 2, -- number of forks
32 -- maximum number of processes (hard limit)
33 max_fork = 16, -- number of forks
35 -- delay between creation of spare processes
36 fork_delay = 1, -- seconds
38 -- delay before retry of failed process creation
39 fork_error_delay = 2, -- seconds
41 -- delay between destruction of excessive spare processes
42 exit_delay = 60, -- seconds
44 -- idle time after a fork gets terminated
45 idle_timeout = 0, -- seconds (0 for no timeout)
47 -- maximum memory consumption before process gets terminated
48 --memory_limit = 1024*1024, -- bytes
50 -- preparation of process (executed after fork)
51 prepare = function()
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 local function file_not_found()
63 request:send_status("404 Not Found")
64 request:send_header("Content-Type", "text/html; chatset=UTF-8")
65 request:send_data("<html>\n")
66 request:send_data("<head><title>404 Not Found</title></head>\n")
67 request:send_data("<body><h1>404 Not Found</h1></body>\n")
68 request:send_data("</html>\n")
69 end
71 if request.method == "GET" or request.method == "HEAD" then
73 if request.path == "" then
74 request:send_status("303 See Other")
75 request:send_header("Location", "http://" .. request.headers_value.host .. "/example_webpage.html")
77 else
78 local document_name = request.path
79 local document_extension = string.match(document_name, "%.([^.])$")
80 local document = documents[document_name]
81 if document then
82 request:send_status("200 OK")
84 if document_extension == "html" then
85 request:send_header("Content-Type", "text/html; charset=UTF-8")
86 elseif document_extension == "css" then
87 request:send_header("Content-Type", "text/css; charset=UTF-8")
88 end
89 request:send_data(document)
90 else
91 file_not_found()
92 end
94 end
96 elseif request.method == "POST" then
98 if request.path == "post_example" then
99 local files = {}
100 do
101 local file
102 request:stream_post_param("files", function(chunk, field_name, meta)
103 if meta then
104 file = {
105 file_name = meta.file_name,
106 content_type = meta.content_type,
107 length = 0
108 }
109 end
110 if chunk then
111 file.length = file.length + #chunk
112 else
113 files[#files+1] = file
114 end
115 end)
116 end
118 request:send_status("200 OK")
119 request:send_header("Content-Type", "text/html; chatset=UTF-8")
120 request:send_data("<html>\n<head>\n")
121 request:send_data('<link href="example_webpage.css" rel="stylesheet" type="text/css">\n')
122 request:send_data("<title>Moonbridge Network Server for Lua Applications &ndash; Example Application</title>\n")
123 request:send_data("</head>\n<body>\n")
124 request:send_data("<h1>Moonbridge Network Server for Lua &ndash; Example Application</h1>\n")
125 request:send_data("<h2>POST request successful</h2>\n")
126 request:send_data('<table>\n<thead><th>File name</th><th>Content type</th><th class="numeric">Bytes received</th></thead>\n<tbody>\n')
127 for i, file in ipairs(files) do
128 request:send_data("<tr>")
129 request:send_data("<td>", http.encode_html(file.file_name or "(unknown)"), "</td>")
130 request:send_data("<td>", http.encode_html(file.content_type or "(unknown)"), "</td>")
131 request:send_data('<td class="numeric">', http.encode_html(tostring(file.length)), "</td>")
132 request:send_data("</tr>\n")
133 end
134 request:send_data("</tbody>\n</table>\n")
135 request:send_data("<p>Submitted comment: ", http.encode_html(request.post_params.comment), "</p>\n")
136 request:send_data("</body>\n</html>\n")
138 else
139 file_not_found()
141 end
143 else
144 request:send_text_status_response("405 Method not allowed")
146 end
148 -- returning false causes termination of current process (and re-forking)
149 return true
150 end),
152 -- executed on process termination
153 finish = function()
154 end
155 }

Impressum / About Us