Module: PudimServer
Main module to create and run the server.
PudimServer:Create(config?)
Creates the main server instance with host, port, and advanced feature options.
Inputs: config? (optional table: ServiceName, Address, Port, Middlewares, Https, Concurrency, HotReload).
Output: configured server instance.
local server = PudimServer:Create{ Port = 8080 }
server:Routes(path, handler)
Registers a route and delegates request handling to the provided handler.
Inputs: path (string), handler (function (req, res)).
Output: no meaningful return value (internal route registration).
server:Routes("/health", function(req, res)
return res:response(200, "ok")
end)
server:EnableCors(config?)
Enables CORS and automatic OPTIONS preflight handling.
Inputs: config? (optional table with CORS policies).
Output: no meaningful return value (CORS enabled on server instance).
server:EnableCors{
AllowOrigins = {"https://app.example.com"},
AllowMethods = {"GET", "POST"}
}
server:UseHandler(entry)
Adds HTTP middleware to the pipeline before route execution.
Inputs: entry (table with name and Handler(req,res,next)).
Output: no meaningful return value (handler registered).
server:UseHandler{
name = "auth",
Handler = function(req, res, next)
if not req.headers["authorization"] then
return res:response(401, "Unauthorized")
end
return next()
end
}
server:RemoveHandler(name)
Removes HTTP middleware by name.
Inputs: name (string).
Output: boolean indicating whether removal succeeded.
local removed = server:RemoveHandler("auth")
print("handler removed?", removed)
server:SetMiddlewares(middleware)
Adds socket middleware to operate on the raw TCP client before HTTP parsing.
Inputs: middleware (table with name and Handler(client)).
Output: no meaningful return value (middleware registered).
server:SetMiddlewares{
name = "socket-log",
Handler = function(client)
print("new tcp connection")
return client
end
}
server:RemoveMiddlewares(name)
Removes socket middleware by name.
Inputs: name (string).
Output: boolean indicating whether removal succeeded.
local removed = server:RemoveMiddlewares("socket-log")
print("socket middleware removed?", removed)
server:EnableHotReload(config?)
Configures development hot reload by invalidating modules in package.loaded.
Inputs: config? (optional table with Enabled, Modules, Prefixes).
Output: no meaningful return value (hot reload configured).
server:EnableHotReload{
Enabled = true,
Modules = {"examples.hot_reload_message"},
Prefixes = {"app."}
}
server:Run()
Starts the main server loop, accepts connections, and processes requests.
Inputs: no parameters (uses server configuration already set).
Output: blocking server execution loop.
server:Run()
Request Object
Every route handler receives a req table built by http:ParseRequest. These are the available fields:
| Field | Type | Description |
|---|---|---|
| method | string | HTTP method (GET, POST, PUT, DELETE, etc.) |
| path | string | Request path, e.g. "/api/users" |
| version | string | HTTP version, e.g. "HTTP/1.1" |
| headers | table<string,string> | Request headers (keys are lowercase) |
| body | string | Raw request body |
| query | table<string, string|string[]>? | Parsed query string (populated when URL contains ?key=value) |
| params | table<string, string>? | Dynamic route parameters (e.g. :id becomes req.params.id) |
server:Routes("/users/:id", function(req, res)
print(req.method) -- "GET"
print(req.path) -- "/users/42"
print(req.params.id) -- "42"
print(req.headers["host"]) -- "localhost:8080"
return res:response(200, { id = req.params.id })
end)
Create Config
Full field reference for the config table passed to PudimServer:Create(config?).
| Field | Type | Default | Description |
|---|---|---|---|
| ServiceName | string? | "Pudim Server" | Service name shown in logs |
| Address | string? | "localhost" | Bind address ("localhost", "127.0.0.1", "0.0.0.0") |
| Port | number? | 8080 | Port number |
| Middlewares | table? | {} | Initial socket-level middlewares |
| Https | HttpsConfig? | nil | Native HTTPS configuration (see below) |
| Concurrency | ConcurrencyConfig? | nil | Cooperative concurrency configuration (see below) |
| HotReload | HotReloadConfig? | nil | Development hot reload configuration (see below) |
HttpsConfig
Enables native TLS via luasec. Requires the luasec rock installed.
| Field | Type | Default | Description |
|---|---|---|---|
| Enabled | boolean? | false | Enable native HTTPS |
| Mode | string? | "server" | SSL mode |
| Protocol | string? | "any" | SSL protocol |
| Key | string? | — | Path to the private key file |
| Certificate | string? | — | Path to the certificate file |
| Verify | string? | "none" | Peer verification policy |
| Options | string? | — | SSL options (e.g. "all") |
local server = PudimServer:Create{
Port = 8443,
Https = {
Enabled = true,
Key = "./certs/server.key",
Certificate = "./certs/server.crt"
}
}
ConcurrencyConfig
Enables cooperative concurrency using Lua coroutines to handle multiple connections without blocking.
| Field | Type | Default | Description |
|---|---|---|---|
| Enabled | boolean? | false | Enable cooperative concurrency |
| Sleep | number? | 0.001 | Idle loop sleep interval in seconds |
local server = PudimServer:Create{
Port = 8080,
Concurrency = { Enabled = true, Sleep = 0.001 }
}
HotReloadConfig
Development hot reload by clearing package.loaded entries before each request. No file watcher required.
| Field | Type | Default | Description |
|---|---|---|---|
| Enabled | boolean? | false | Enable hot reload |
| Modules | string[]? | {} | Exact module names to invalidate in package.loaded |
| Prefixes | string[]? | {} | Module prefixes to invalidate (e.g. "app." clears all app.*) |
local server = PudimServer:Create{
Port = 8080,
HotReload = {
Enabled = true,
Modules = {"examples.hot_reload_message"},
Prefixes = {"app."}
}
}
Request Lifecycle
Every incoming connection goes through these steps inside server:Run():
- TCP accept —
socket:accept()receives a raw client connection. - Socket middlewares — each middleware registered with
SetMiddlewaresruns sequentially on the raw TCP client (e.g. TLS wrapping). - Hot reload — if enabled, configured modules are cleared from
package.loadedso the nextrequirepicks up fresh code. - Receive —
client:receive()reads the raw HTTP data from the socket. - Parse —
http:ParseRequest(raw)transforms the raw string into aRequesttable (method,path,headers,body,query,params). - CORS preflight — if the method is
OPTIONSand CORS is enabled, a204preflight response is returned immediately. - Pipeline execute —
pipeline:execute(req, res, routeHandler)runs all handlers added viaUseHandler, each callingnext()or short-circuiting. - Route match — the final handler in the pipeline finds the matching route and calls its handler function.
- CORS headers — CORS response headers are injected into the final response string.
- Send —
client:send(response)writes the HTTP response back to the client and closes the connection.