moonbridge

view example_application.lua @ 29:41160dd94b11

Added tag v0.2.1 for changeset 0a9abf36e07b
author jbe
date Sat Jan 31 00:00:36 2015 +0100 (2015-01-31)
parents 19a6d18bf34e
children 59f485dc48ea
line source
1 -- Moonbridge example application
2 -- invoke with ./moonbridge example_application.lua
4 local http = require "moonbridge_http"
6 -- preparation before forking:
7 local documents = {}
8 for i, document_name in ipairs{"example_webpage.html", "example_webpage.css"} do
9 local file = assert(io.open(document_name))
10 documents[document_name] = file:read("*a") -- store file contents in memory
11 file:close()
12 end
14 listen{
15 -- listen to a tcp version 4 socket
16 { proto = "tcp4", port = 8080, localhost = true },
18 -- listen to a tcp version 6 socket
19 { proto = "tcp6", port = 8080, localhost = true },
21 -- listen to a unix domain socket
22 --{ proto = "local", path = 'socket' },
24 -- execute the listener regularly (without incoming connection)
25 --{ proto = "interval", name = "myint", delay = 10, strict = false },
27 -- desired number of spare (idle) processes
28 pre_fork = 1, -- number of forks
30 -- minimum number of processes
31 min_fork = 2, -- number of forks
33 -- maximum number of processes (hard limit)
34 max_fork = 16, -- number of forks
36 -- delay between creation of spare processes
37 fork_delay = 1, -- seconds
39 -- delay before retry of failed process creation
40 fork_error_delay = 2, -- seconds
42 -- delay between destruction of excessive spare processes
43 exit_delay = 60, -- seconds
45 -- idle time after a fork gets terminated
46 idle_timeout = 0, -- seconds (0 for no timeout)
48 -- maximum memory consumption before process gets terminated
49 --memory_limit = 1024*1024, -- bytes
51 -- preparation of process (executed after fork)
52 prepare = function()
53 -- e.g. open database connection
54 end,
56 -- connection handler
57 connect = http.generate_handler(
58 {
59 static_headers = {"Server: Moonbridge Example Server"},
60 request_body_size_limit = 16*1024*1024*1024 -- allow big file uploads
61 },
62 function(request)
64 local function file_not_found()
65 request:send_status("404 Not Found")
66 request:send_header("Content-Type", "text/html; chatset=UTF-8")
67 request:send_data("<html>\n")
68 request:send_data("<head><title>404 Not Found</title></head>\n")
69 request:send_data("<body><h1>404 Not Found</h1></body>\n")
70 request:send_data("</html>\n")
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 file_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 file_not_found()
142 end
144 else
145 request:send_text_status_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