Guide
Applications
Applications
whiplash.start(options, handler) serves one handler. An Application adds
WebSocket routes, streamed responses and upload routes:
module main
import pooka/http
import pooka/otp
import whiplash
import whiplash/body
handle(_: http.Request) -> http.Response {
http.text(404, "Not found")
}
streams(request: http.Request) -> Option(body.Stream) {
match request.path {
"/count" -> Some(body.chunks(200, [#("content-type", "text/plain")], [<<"1 ":utf8>>, <<"2 ":utf8>>, <<"3":utf8>>]))
_ -> None
}
}
main() {
let application = whiplash.Application(handle, fn(_) { None })
.with_streaming(streams)
let assert Ok(_) = whiplash.start_application(whiplash.options(8080), application)
otp.sleep_forever()
}
The second argument of Application chooses WebSocket routes (see
WebSockets); with_streaming and with_uploads add the
others. A WebSocket upgrade or extended CONNECT goes to the WebSocket routes.
A request with a body is offered to the upload routes as soon as its head has
arrived. Anything else goes to a streaming route if one claims it, and to the
handler otherwise.
Options and limits
whiplash.options(port) gives the defaults;
whiplash.Options(port, acceptors, max_connections, max_body, request_timeout)
sets them all:
| Field | Default | Meaning |
|---|---|---|
port |
0 asks the system for a free port; running.port tells which |
|
acceptors |
8 | Processes accepting connections |
max_connections |
1024 | Connections admitted at once |
max_body |
1 MiB | Request body limit, upload routes included (413 beyond it); also the WebSocket message limit |
request_timeout |
30000 ms | A request's whole deadline, and how long an idle connection stays |
options.with_address(address) changes where the server listens, which is
127.0.0.1 until you do (see Operations).
Deadlines are not extended for each byte that arrives. Header limits are the
HTTP/1 parser's defaults (http1.default_limits()) and HTTP/2's
SETTINGS_MAX_HEADER_LIST_SIZE.
What the server does for you
- Content-Length, Transfer-Encoding and Connection are the server's for
complete responses: they are derived from the body. A streamed response may
declare a
content-length, and must then write exactly that much. - HEAD, 204 and 304 responses carry no body.
- Every response has a
Dateunless the handler set one. - A plain CONNECT is answered 501: Whiplash opens no tunnels.
- Field names are lowercased on HTTP/2 and HTTP/3, and connection-specific fields are dropped there, as those protocols require.
When a handler fails
A handler that raises ends its own HTTP/1 connection, or resets only its own stream on HTTP/2 and HTTP/3 (a worker per request); OTP Logger reports it (see Logging). Nothing else is affected.
To answer with an error status instead, treat failures as values: let a route
return a Result, use ? inside it, and turn an Error into a response in
one place.
module main
import pooka/http
import pooka/int
double(request: http.Request) -> Result(http.Response, String) {
let text = match request.text() {
Ok(text) -> Ok(text)
Error(_) -> Error("the body is not text")
}?
let number = match int.parse(text) {
Ok(number) -> Ok(number)
Error(_) -> Error("not a number: " + text)
}?
Ok(http.text(200, int.to_string(number * 2)))
}
handle(request: http.Request) -> http.Response {
match double(request) {
Ok(response) -> response
Error(reason) -> http.text(400, reason)
}
}
Stopping
whiplash.stop(running) shuts down the whole tree: listener, acceptors and
every connection.