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_port
  • ewe.with_ipv6
  • ewe.with_tls
  • ewe.on_start
pub opaque type Builder

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)

Values

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

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

pub fn bind_all(builder: Builder) -> Builder

Binds server to all interfaces.

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

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

pub fn ip_address_to_string(address: IpAddress) -> String

Converts an IpAddress to a string for later printing.

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

Creates new server builder with handler provided.

Default configuration:

  • port: 8080
  • interface: 127.0.0.1
  • No ipv6 support
  • No TLS support
  • on_start: prints Listening on <scheme>://<ip_address>:<port>
pub fn on_start(
  builder: Builder,
  on_start: fn(http.Scheme, IpAddress, Int) -> Nil,
) -> Builder

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,
) -> Result(
  actor.Started(static_supervisor.Supervisor),
  actor.StartError,
)

Starts the server.

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

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

pub fn with_ipv6(builder: Builder) -> Builder

Enables IPv6 support.

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

Sets listening port for server.

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

Enables TLS support, requires certificate and key file.

Search Document