moonbridge

view example_application.lua @ 207:2dd0eea2f5f8

Removed two functions from moonbridge_io.h
author jbe
date Sun Jun 21 23:38:53 2015 +0200 (2015-06-21)
parents 822ccaeccccb
children bae067af56d4
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 = "tcp", host = "0.0.0.0", port = 8080 },
20 -- listen to a tcp version 6 socket
21 { proto = "tcp", host = "::", port = 8080},
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 = 4, -- 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_idle_timeout = 330, -- 5 minutes and 30 seconds after which an idle connection will be closed
64 request_header_timeout = 30, -- request headers must be sent within 30 seconds after first byte was received
65 timeout = 1800 -- request body and response must be sent within 30 minutes
66 },
67 function(request)
69 local function error_response(status)
70 request:send_status(status)
71 request:send_header("Content-Type", "text/html")
72 request:send_data("<html>\n<head><title>", status, "</title></head>\n<body><h1>", status, "</h1></body>\n</html>\n")
73 request:finish()
74 end
76 if request.method == "GET" or request.method == "HEAD" then
78 if request.path == "" then
79 request:send_status("303 See Other")
80 request:send_header("Location", "http://" .. request.headers_value.host .. "/example_webpage.html")
82 else
83 local document_name = request.path
84 local document_extension = string.match(document_name, "%.([^.])$")
85 local document = documents[document_name] -- loads file contents from memory
86 if document then
87 request:send_status("200 OK")
88 if document_extension == "html" then
89 request:send_header("Content-Type", "text/html; charset=UTF-8")
90 elseif document_extension == "css" then
91 request:send_header("Content-Type", "text/css; charset=UTF-8")
92 end
93 request:send_data(document)
94 else
95 error_response("404 Not Found")
96 end
98 end
100 elseif request.method == "POST" then
102 if request.path == "post_example" then
103 local files = {}
104 do
105 local file
106 request:stream_post_param("files", function(chunk, field_name, meta)
107 if meta then
108 file = {
109 file_name = meta.file_name,
110 content_type = meta.content_type,
111 length = 0
112 }
113 end
114 if chunk then
115 file.length = file.length + #chunk
116 else
117 files[#files+1] = file
118 end
119 end)
120 end
122 request:send_status("200 OK")
123 request:send_header("Content-Type", "text/html; charset=UTF-8")
124 request:send_data("<html>\n<head>\n")
125 request:send_data('<link href="example_webpage.css" rel="stylesheet" type="text/css">\n')
126 request:send_data("<title>Moonbridge Network Server for Lua Applications &ndash; Example Application</title>\n")
127 request:send_data("</head>\n<body>\n")
128 request:send_data("<h1>Moonbridge Network Server for Lua &ndash; Example Application</h1>\n")
129 request:send_data("<h2>POST request successful</h2>\n")
130 request:send_data('<table>\n<thead><th>File name</th><th>Content type</th><th class="numeric">Bytes received</th></thead>\n<tbody>\n')
131 request:consume_input()
132 for i, file in ipairs(files) do
133 request:send_data("<tr>")
134 request:send_data("<td>", http.encode_html(file.file_name or "(unknown)"), "</td>")
135 request:send_data("<td>", http.encode_html(file.content_type or "(unknown)"), "</td>")
136 request:send_data('<td class="numeric">', http.encode_html(tostring(file.length)), "</td>")
137 request:send_data("</tr>\n")
138 end
139 request:send_data("</tbody>\n</table>\n")
140 request:send_data("<p>Submitted comment: ", http.encode_html(request.post_params.comment), "</p>\n")
141 request:send_data("</body>\n</html>\n")
143 else
144 error_response("404 Not Found")
146 end
148 else
149 error_response("405 Method not allowed")
151 end
153 -- returning false causes termination of current process (and re-forking)
154 return true
155 end),
157 -- executed on process termination
158 finish = function()
159 -- e.g. close database connection
160 end
161 }

Impressum / About Us