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