mist
Types
This type reflects whether the body has been read from the socket yet.
pub type Body =
HTTPBody
Functions
pub fn bit_builder_response(resp: Response(a), data: BitBuilder) -> HandlerResponse
The mist runtime only supports sending BitBuilder types, or files (see below). This method will erase any pre-existing response body.
pub fn chunked_response(resp: Response(a), iter: Iterator(
BitBuilder,
)) -> HandlerResponse
You can send chunks of responses from an iterator. The iterator must complete.
pub fn empty_response(resp: Response(a)) -> HandlerResponse
mist.serve
expects the mist Response type, rather than the gleam/http
Response. When returning a response with no body, this will convert the
type. Note that any previously set response body will be removed before
sending.
pub fn file_response(resp: Response(a), path: String, content_type: String) -> Result(
HandlerResponse,
FileError,
)
This is a more generally optimized method for returning files to a client.
It’s a light wrapper around Erlang’s file:sendfile/5
method. The error
can be matched on with mist/file.{FileError}
if custom behavior is desired
for various cases. The size of the file will be added to the
content-length
header field.
pub fn handler_func(handler: fn(Request(Body)) -> HandlerResponse) -> fn(
HandlerMessage,
LoopState(State),
) -> Next(LoopState(State))
Handles converting the mist Response
type into a gleam HTTP Response. Use
this when calling mist.serve
to start your application.
pub fn read_body(req: Request(Body)) -> Result(
Request(BitString),
DecodeError,
)
When using mist.serve
, the body is not automatically read. You can
inspect content headers to determine whether to read the body or not.
This function will pull the content from the sender. It gives back a
Request(BitString)
containing the body. This return value should be
treated as replacing the initial request. Do not attempt to call this
method multiple times on the same request.
pub fn run_service(port: Int, handler: fn(Request(BitString)) ->
Response(BitBuilder), max_body_limit max_body_limit: Int) -> Result(
Nil,
StartError,
)
Runs an HTTP Request->Response server at the given port, with your defined
handler. This will automatically read the full body contents up to the
specified max_body_limit
in bytes. If you’d prefer to have finer-grain
control over this behavior, consider using mist.serve
.
pub fn run_service_ssl(port port: Int, certfile certfile: String, keyfile keyfile: String, handler handler: fn(
Request(BitString),
) -> Response(BitBuilder), max_body_limit max_body_limit: Int) -> Result(
Nil,
StartError,
)
Similar setup and behavior to run_service
, but instead takes in the SSL
certificate/key and serves over HTTPS.
pub fn serve(port port: Int, handler handler: fn(
HandlerMessage,
LoopState(State),
) -> Next(LoopState(State))) -> Result(Nil, StartError)
Slightly more flexible alternative to run_service
. This allows hooking
into the mist.handler_func
method. Note that the request body
will not be automatically read. You will need to call mist.read_body
.
Ensure that this is only called once per request.
pub fn serve_ssl(port port: Int, certfile certfile: String, keyfile keyfile: String, handler handler: fn(
HandlerMessage,
LoopState(State),
) -> Next(LoopState(State))) -> Result(Nil, StartError)
Similar to the run_service
method, serve
also has a similar SSL method.