new(
config,handle,app) ->server,err
Create a new HTTP server instance
Creates and configures an HTTP server. The handle function is called
for each request with (method, query, args, headers, body, context) and
must return content, status, response_headers. The args string is
the request's query string without the leading ?. The optional app
table is an opaque application context passed to the handler as
context.app — the place for process-lifetime state such as stores and
caches.
Several server instances (each with its own handler, TLS identities and
connection budget) can share one event loop via serve_all; a single
instance may also bind several addresses via listen.
The config table may override these defaults:
ip — bind address (default: "127.0.0.1")port — listen port (default: 8080)listen — optional array of { ip = ..., port = ... } bind specs; when
set it takes precedence over ip/portbacklog — listen backlog (default: 256)connection_limit — max concurrent connections (default: 64)requests_per_connection — requests before connection closes (default: 512)max_body_size — max request body in bytes (default: 5 MB)request_line_limit — max header/request line (default: 8 KB)keepalive_idle_timeout — keep-alive idle timeout (default: 15)request_header_timeout — header read timeout (default: 10)request_body_timeout — body read timeout (default: 30)tls_handshake_timeout — TLS handshake timeout (default: 10)ssl — TLS configuration (see below)compression — compression settings tablelog_level — log level (default: "access")log_headers — request headers to logon_request — optional callback invoked after each response is sent,
with one table argument: { host, method, query, status, elapsed, size }
(status is a number, elapsed in seconds, size the response body
size in bytes). It fires only on the normal response path — proxied
requests and premature protocol errors do not reach it. The callback
runs under pcall; a failure is logged and never affects the requestTLS configuration (ssl field):
{
default = { cert = "path/to/cert", key = "path/to/key" },
hosts = {
["domain.com"] = { cert = "...", key = "..." },
},
}
The server uses a coroutine-per-connection model: each accepted
connection is handled in a spawned coroutine within lev.run().
local server = require("http.server")
local srv, err = server.new(
{
ip = "0.0.0.0",
port = 8443,
ssl = {
default = {
cert = "/etc/tls/cert.pem",
key = "/etc/tls/key.pem",
},
},
},
function(method, query, args, headers, body, ctx)
return "Hello!", 200, { ["content-type"] = "text/plain" }
end
)
if not srv then error(err) end
srv:serve() -- blocks forever