roadrunner_gleam
Run Gleam handlers on roadrunner.
import gleam/erlang/process
import roadrunner_gleam
import roadrunner_gleam/request.{type Request}
import roadrunner_gleam/response.{type Response}
pub fn main() {
let assert Ok(_) =
roadrunner_gleam.new(handle)
|> roadrunner_gleam.port(8080)
|> roadrunner_gleam.start
process.sleep_forever()
}
fn handle(request: Request) -> Response {
case request.path_segments(request) {
[] -> response.text("hello, roadrunner!")
_ -> response.new(404)
}
}
Types
How to run a listener, built with new and the functions that follow it.
pub opaque type Builder
Values
pub fn bind(builder: Builder, ip: String) -> Builder
The address to listen on, such as "127.0.0.1" or "::1".
pub fn compress(builder: Builder) -> Builder
Compress responses with gzip or deflate, as the client’s
accept-encoding allows. Complete bodies of 860 bytes or more are
compressed, and chunked bodies are compressed as they go. Responses that
already have a content-encoding, and file, push and WebSocket
responses, are sent as they are.
pub fn drain(
listener: Listener,
timeout: Int,
) -> Result(Nil, Int)
Stop accepting connections, ask open ones to finish, and stop the
listener once they are gone or after timeout milliseconds, when the
rest are closed. Fails with how many were still open at the timeout.
A supervised listener exits after the drain, so its supervisor starts it again unless it is shut down too. Draining a listener already stopped succeeds at once.
pub fn http3(builder: Builder) -> Builder
Also serve HTTP/3, over QUIC. Needs tls. With a fixed port, HTTP/3 is
on the UDP port with the same number; with port 0 it gets a UDP port
of its own: read it with listener_quic_port.
pub fn listener_port(listener: Listener) -> Int
The TCP port the listener is bound to.
pub fn listener_quic_port(listener: Listener) -> Result(Int, Nil)
The UDP port the listener serves HTTP/3 on. Fails when it serves no HTTP/3.
pub fn name(builder: Builder, name: String) -> Builder
The name the listener is registered under. Defaults to one built from
the port. Give each listener its own name when you run several on
port 0.
pub fn new(
handler: fn(request.Request) -> response.Response(response.Body),
) -> Builder
A listener on port 4000 of every interface, over plain HTTP/1.1.
pub fn option(
builder: Builder,
name: String,
value: dynamic.Dynamic,
) -> Builder
Set any other roadrunner listener option, such as max_content_length
or keep_alive_timeout. See roadrunner_listener:opts() in
roadrunner’s docs for the full list. It overrides what the functions
above set, except routes, which carries the handler.
pub fn port(builder: Builder, port: Int) -> Builder
The port to listen on. 0 picks a free one; read it with
listener_port.
pub fn read_body_on_demand(builder: Builder) -> Builder
Leave request bodies unread until the handler reads them with
request.read_body or request.read_chunk, so large uploads can be
read in pieces. The handler runs as soon as the headers are in; a body
larger than max_content_length fails its read with BodyTooLarge.
Over HTTP/1.1, a client that sent expect: 100-continue with a declared
length over the limit gets a 413 without the handler running.
pub fn start(
builder: Builder,
) -> Result(actor.Started(Listener), actor.StartError)
Start the listener, linked to the calling process. A listener that cannot start, such as on a port already in use, returns an error and leaves the caller running.
pub fn stop(listener: Listener) -> Nil
Stop the listener and close its port, without waiting for open connections. Stopping a listener already stopped does nothing. A supervised listener is started again by its supervisor.
pub fn supervised(
builder: Builder,
) -> supervision.ChildSpecification(Listener)
A child specification to start the listener under a gleam/otp
supervisor.