jbe@0: -- Moonbridge example application jbe@0: -- invoke with ./moonbridge example_application.lua jbe@33: -- jbe@33: -- see helloworld.lua for a simpler example jbe@0: jbe@4: local http = require "moonbridge_http" jbe@0: jbe@26: -- preparation before forking: jbe@13: local documents = {} jbe@13: for i, document_name in ipairs{"example_webpage.html", "example_webpage.css"} do jbe@15: local file = assert(io.open(document_name)) jbe@26: documents[document_name] = file:read("*a") -- store file contents in memory jbe@13: file:close() jbe@13: end jbe@0: jbe@0: listen{ jbe@0: -- listen to a tcp version 4 socket jbe@0: { proto = "tcp4", port = 8080, localhost = true }, jbe@0: jbe@0: -- listen to a tcp version 6 socket jbe@0: { proto = "tcp6", port = 8080, localhost = true }, jbe@0: jbe@0: -- listen to a unix domain socket jbe@0: --{ proto = "local", path = 'socket' }, jbe@0: jbe@0: -- execute the listener regularly (without incoming connection) jbe@0: --{ proto = "interval", name = "myint", delay = 10, strict = false }, jbe@0: jbe@0: -- desired number of spare (idle) processes jbe@0: pre_fork = 1, -- number of forks jbe@0: jbe@0: -- minimum number of processes jbe@0: min_fork = 2, -- number of forks jbe@0: jbe@0: -- maximum number of processes (hard limit) jbe@0: max_fork = 16, -- number of forks jbe@0: jbe@0: -- delay between creation of spare processes jbe@0: fork_delay = 1, -- seconds jbe@0: jbe@0: -- delay before retry of failed process creation jbe@0: fork_error_delay = 2, -- seconds jbe@0: jbe@0: -- delay between destruction of excessive spare processes jbe@0: exit_delay = 60, -- seconds jbe@0: jbe@0: -- idle time after a fork gets terminated jbe@0: idle_timeout = 0, -- seconds (0 for no timeout) jbe@0: jbe@0: -- maximum memory consumption before process gets terminated jbe@23: --memory_limit = 1024*1024, -- bytes jbe@0: jbe@11: -- preparation of process (executed after fork) jbe@0: prepare = function() jbe@26: -- e.g. open database connection jbe@0: end, jbe@0: jbe@0: -- connection handler jbe@0: connect = http.generate_handler( jbe@0: { jbe@0: static_headers = {"Server: Moonbridge Example Server"}, jbe@0: request_body_size_limit = 16*1024*1024*1024 -- allow big file uploads jbe@0: }, jbe@0: function(request) jbe@0: jbe@33: local function error_response(status) jbe@33: request:send_status(status) jbe@33: request:send_header("Content-Type", "text/html") jbe@33: request:send_data("\n
File name | Content type | Bytes received | \n\n') jbe@0: for i, file in ipairs(files) do jbe@0: request:send_data("
---|---|---|
", http.encode_html(file.file_name or "(unknown)"), " | ") jbe@0: request:send_data("", http.encode_html(file.content_type or "(unknown)"), " | ") jbe@0: request:send_data('', http.encode_html(tostring(file.length)), " | ") jbe@0: request:send_data("
Submitted comment: ", http.encode_html(request.post_params.comment), "
\n") jbe@0: request:send_data("\n\n") jbe@0: jbe@0: else jbe@33: error_response("404 Not Found") jbe@0: jbe@23: end jbe@0: jbe@0: else jbe@33: error_response("405 Method not allowed") jbe@0: jbe@0: end jbe@0: jbe@0: -- returning false causes termination of current process (and re-forking) jbe@0: return true jbe@0: end), jbe@0: jbe@0: -- executed on process termination jbe@0: finish = function() jbe@26: -- e.g. close database connection jbe@0: end jbe@0: } jbe@0: