espresso/router

Types

pub type Handler(req, assigns, res) {
  ServiceHandler(OrderedMap(Method, Service(req, assigns, res)))
  RouterHandler(Router(req, assigns, res))
  StaticHandler(String, Static)
}

Constructors

  • ServiceHandler(OrderedMap(Method, Service(req, assigns, res)))
  • RouterHandler(Router(req, assigns, res))
  • StaticHandler(String, Static)
pub type Method {
  ALL
  GET
  POST
  PATCH
  PUT
  DELETE
  HEAD
  OPTIONS
}

Constructors

  • ALL
  • GET
  • POST
  • PATCH
  • PUT
  • DELETE
  • HEAD
  • OPTIONS
pub type Router(req, assigns, res) {
  Router(
    middleware: Middleware(req, assigns, res, req, res),
    handlers: OrderedMap(String, Handler(req, assigns, res)),
    not_found: Service(req, assigns, res),
  )
}

Constructors

  • Router(
      middleware: Middleware(req, assigns, res, req, res),
      handlers: OrderedMap(String, Handler(req, assigns, res)),
      not_found: Service(req, assigns, res),
    )

Functions

pub fn delete(router: Router(a, b, c), path: String, handler: fn(
    Request(a, b),
  ) -> Response(c)) -> Router(a, b, c)
pub fn expand(path: String, handlers: List(
    #(String, Handler(a, b, c)),
  ), router: Router(a, b, c)) -> List(#(String, Handler(a, b, c)))
pub fn get(router: Router(a, b, c), path: String, handler: fn(
    Request(a, b),
  ) -> Response(c)) -> Router(a, b, c)
pub fn handle(router: Router(a, b, c), routes: List(
    #(Method, fn(Request(a, b)) -> Response(c)),
  )) -> fn(Request(a, b)) -> Response(c)
pub fn middleware(router: Router(a, b, c), middleware: fn(
    fn(Request(a, b)) -> Response(c),
  ) -> fn(Request(a, b)) -> Response(c)) -> Router(a, b, c)

Sets the middleware for a router. This is a function that wraps all the router handlers under it. Currently it doesn’t support more than one but that may work in the future.

Examples

import espresso/router.{get, delete}
import espresso/request.{Request}
import espresso/service.{Service}

router.new()
|> router.router(
 "/api",
 router.new()
 |> router.middleware(fn(next: Service(BitString, BitBuilder)) {
   fn(req: Request(BitString)) {
     let auth = request.get_header(req, "authorization")
     case auth {
       Ok("Basic OnN1cGVyc2VjcmV0") -> next(req)
       _ -> send(401, "Unauthorized")
     }
   }
 })
 |> get("/things", fn(_req: Request(BitString)) { send(200, "Things") })
 |> delete("/things", fn(_req: Request(BitString)) { send(204, "") }),
)

pub fn new() -> Router(a, b, BitBuilder)

Instantiates a new router. This is usually the starting point for route definitions unless you want to override the types of the req, res, middleware or not_found handler.

Examples

import espresso/router

router.new()
|> get("/", fn(_req: Request(BitString)) { send(200, "Success") })
pub fn patch(router: Router(a, b, c), path: String, handler: fn(
    Request(a, b),
  ) -> Response(c)) -> Router(a, b, c)
pub fn post(router: Router(a, b, c), path: String, handler: fn(
    Request(a, b),
  ) -> Response(c)) -> Router(a, b, c)
pub fn put(router: Router(a, b, c), path: String, handler: fn(
    Request(a, b),
  ) -> Response(c)) -> Router(a, b, c)
pub fn router(router: Router(a, b, c), path: String, subrouter: Router(
    a,
    b,
    c,
  )) -> Router(a, b, c)
pub fn static(router: Router(a, b, c), path: String, config: Static) -> Router(
  a,
  b,
  c,
)

Handles a request for a given path and returns static files Currently only supports File and Directory

Examples

import espresso/router
import espreso/static

// Serves all files in the priv/public directory for any request starting with /public
router.new()
|> router.static("/public/[...]", static.Dir("priv/public"))

// Serves only the index.html file from the priv/public directory
router.new()
|> router.static("/", static.File("priv/public/index.html"))
pub fn to_routes(router: Router(a, b, c)) -> List(
  #(String, Route(a, b, c)),
)
Search Document