cowboy/cowboy

This is a simple wrapper over the erlang cowboy server. It is originall from gleam-lang/cowboy but has been modified to work with espresso.

original source: https://github.com/gleam-lang/cowboy/blob/83e2f20170e4a73e5499238149313f8329a2f41a/src/gleam/http/cowboy.gleam

Types

An incoming request that will be handled by the dispatch in gleam_cowboy_native.erl

pub external type CowboyRequest

The returned result of cowboy_router:compile

pub external type CowboyRouter

A Route can either be a service (a function that handles a request and response) or a router which is expanded into a list of services.

pub type Route(req, assigns, session, res) {
  ServiceRoute(Service(req, assigns, session, res))
  RouterRoute(Routes(req, assigns, session, res))
  StaticRoute(String, Static)
  WebsocketRoute(Websocket)
}

Constructors

  • ServiceRoute(Service(req, assigns, session, res))
  • RouterRoute(Routes(req, assigns, session, res))
  • StaticRoute(String, Static)
  • WebsocketRoute(Websocket)

Routes are the mapping between a path and the route. For example /hello -> hello_service

pub type Routes(req, assigns, session, res) =
  OrderedMap(String, Route(req, assigns, session, res))

Functions

pub fn router(routes: List(#(String, Route(a, b, c, d)))) -> CowboyRouter

Takes a list of route structures and compiles them into a cowboy router.

pub fn start(router: CowboyRouter, on_port number: Int) -> Result(
  Pid,
  Dynamic,
)

Start the cowboy server on the given port.

Search Document