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@194: 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@125: --{ proto = "tcp", host = "0.0.0.0", port = 8080 }, jbe@0: jbe@0: -- listen to a tcp version 6 socket jbe@125: { proto = "tcp", host = "::", port = 8080}, 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 bsw@121: min_fork = 4, -- 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@61: fork_delay = 0.25, -- 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@222: request_header_size_limit = 1024*1024, -- maximum size of request headers jbe@222: request_body_size_limit = 16*1024*1024*1024, -- allow big file uploads jbe@227: idle_timeout = 65, -- maximum time until receiving the first byte of the request headera jbe@227: stall_timeout = 60, -- maximum time a client connection may be stalled jbe@227: request_header_timeout = 120, -- maximum time until receiving the remaining bytes of the request header jbe@227: response_timeout = 3600, -- time in which request body and response must be sent jbe@227: maximum_input_chunk_size = 16384, -- tweaks behavior of request-body parser jbe@227: minimum_output_chunk_size = 1024 -- chunk size for chunked-transfer-encoding 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@192: request:consume_input() 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: