ewe

Types

Possible errors that can occur when reading a body.

pub type BodyError {
  BodyTooLarge
  InvalidBody
}

Constructors

  • BodyTooLarge
  • InvalidBody

Ewe’s server builder. Contains all server’s configuration. Can be adjusted with the following functions:

  • ewe.bind
  • ewe.bind_all
  • ewe.with_read_body
  • ewe.with_port
  • ewe.with_random_port
  • ewe.with_ipv6
  • ewe.with_tls
  • ewe.with_name
  • ewe.on_start
  • ewe.on_crash
pub opaque type Builder(body)

Represents a connection between a client and a server, stored inside a Request. Can be converted to a BitArray using ewe.read_body.

pub type Connection =
  @internal Connection

Represents an IP address. Appears when accessing client’s information (ewe.client_stats) or on_start handler (ewe.on_start).

pub type IpAddress {
  IpV4(Int, Int, Int, Int)
  IpV6(Int, Int, Int, Int, Int, Int, Int, Int)
}

Constructors

  • IpV4(Int, Int, Int, Int)
  • IpV6(Int, Int, Int, Int, Int, Int, Int, Int)

Represents started server’s information. Can be retrieved using ewe.get_server_info.

pub type ServerInfo {
  ServerInfo(
    scheme: http.Scheme,
    ip_address: IpAddress,
    port: Int,
  )
}

Constructors

Values

pub fn bind(
  builder: Builder(body),
  interface: String,
) -> Builder(body)

Binds server to a specific interface. Crashes program if interface is invalid.

pub fn bind_all(builder: Builder(body)) -> Builder(body)

Binds server to all interfaces.

pub fn bytes(
  response: response.Response(a),
  bytes: bytes_tree.BytesTree,
) -> response.Response(bytes_tree.BytesTree)

Sets response body to a bytes, sets content-length header. Doesn’t set content-type header.

pub fn get_client_info(
  connection: @internal Connection,
) -> Result(#(IpAddress, Int), Nil)

Performs an attempt to get the client’s IP address and port.

pub fn get_server_info(
  name: process.Name(@internal Message(ServerInfo)),
) -> Result(ServerInfo, Nil)

Retrieves server’s information. Requires the same name as the one used in ewe.with_name and server to be started. Otherwise, will crash the program.

pub fn ip_address_to_string(address: IpAddress) -> String

Converts an IpAddress to a string for later printing.

pub fn json(
  response: response.Response(a),
  json: string_tree.StringTree,
) -> response.Response(bytes_tree.BytesTree)

Sets response body to a JSON (use gleam_json package and encode using json.to_string_tree), sets content-type to application/json; charset=utf-8 and content-length headers.

pub fn new(
  handler: fn(request.Request(body)) -> response.Response(
    bytes_tree.BytesTree,
  ),
) -> Builder(body)

Creates new server builder with handler provided.

Default configuration:

  • port: 8080
  • interface: 127.0.0.1
  • No ipv6 support
  • No TLS support
  • Default process name for server information retrieval
  • on_start: prints Listening on <scheme>://<ip_address>:<port>
  • on_crash: empty 500 response
pub fn on_crash(
  builder: Builder(body),
  on_crash: response.Response(bytes_tree.BytesTree),
) -> Builder(body)

Sets a custom response that will be sent when server crashes.

pub fn on_start(
  builder: Builder(body),
  on_start: fn(ServerInfo) -> Nil,
) -> Builder(body)

Sets a custom handler that will be called after server starts.

pub fn read_body(
  req: request.Request(@internal Connection),
  size_limit size_limit: Int,
) -> Result(request.Request(BitArray), BodyError)

Reads body from a request. If request body is malformed, InvalidBody error is returned. On success, returns a request with body converted to BitArray.

  • When transfer-encoding header set as chunked, BodyTooLarge error is returned if accumulated body is larger than size_limit.
  • Ensures that content-length is in size_limit scope.
pub fn start(
  builder: Builder(@internal Connection),
) -> Result(
  actor.Started(static_supervisor.Supervisor),
  actor.StartError,
)

Starts the server.

pub fn supervised(
  builder: Builder(@internal Connection),
) -> supervision.ChildSpecification(static_supervisor.Supervisor)

Creates a supervisor that can be appended to a supervision tree.

pub fn text(
  response: response.Response(a),
  text: String,
) -> response.Response(bytes_tree.BytesTree)

Sets response body to a text, sets content-type to text/plain; charset=utf-8 and content-length headers.

pub fn with_ipv6(builder: Builder(body)) -> Builder(body)

Enables IPv6 support.

pub fn with_name(
  builder: Builder(body),
  name: process.Name(@internal Message(ServerInfo)),
) -> Builder(body)

Sets a custom process name for server information retrieval, allowing to use ewe.get_server_info after server starts.

pub fn with_port(
  builder: Builder(body),
  port: Int,
) -> Builder(body)

Sets listening port for server.

pub fn with_random_port(builder: Builder(body)) -> Builder(body)

Sets listening port for server to a random port. Useful for testing.

pub fn with_read_body(
  builder: Builder(BitArray),
  size_limit: Int,
  on_failure: fn(BodyError) -> response.Response(
    bytes_tree.BytesTree,
  ),
) -> Builder(@internal Connection)

With this option, body is read before handler is called, making request body available in handler (Request(BitArray)). If body is invalid or too large, on_failure function is called.

pub fn with_tls(
  builder: Builder(body),
  certificate: String,
  keyfile: String,
) -> Builder(body)

Enables TLS support, requires certificate and key file.

Search Document