espresso/router

Types

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

Constructors

  • ServiceHandler(OrderedMap(Method, Service(req, res)))
  • RouterHandler(Router(req, 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, res) {
  Router(
    middleware: Middleware(req, res, BitString, BitBuilder),
    handlers: OrderedMap(String, Handler(req, res)),
  )
}

Constructors

  • Router(
      middleware: Middleware(req, res, BitString, BitBuilder),
      handlers: OrderedMap(String, Handler(req, res)),
    )

Functions

pub fn delete(router: Router(a, b), path: String, handler: fn(
    Request(a),
  ) -> Response(b)) -> Router(a, b)
pub fn expand(path: String, handlers: List(
    #(String, Handler(a, b)),
  ), router: Router(a, b)) -> List(#(String, Handler(a, b)))
pub fn get(router: Router(a, b), path: String, handler: fn(
    Request(a),
  ) -> Response(b)) -> Router(a, b)
pub fn handle(router: Router(a, b), routes: List(
    #(Method, fn(Request(a)) -> Response(b)),
  )) -> fn(Request(BitString)) -> Response(BitBuilder)
pub fn new() -> Router(BitString, BitBuilder)
pub fn patch(router: Router(a, b), path: String, handler: fn(
    Request(a),
  ) -> Response(b)) -> Router(a, b)
pub fn post(router: Router(a, b), path: String, handler: fn(
    Request(a),
  ) -> Response(b)) -> Router(a, b)
pub fn put(router: Router(a, b), path: String, handler: fn(
    Request(a),
  ) -> Response(b)) -> Router(a, b)
pub fn router(router: Router(a, b), path: String, subrouter: Router(
    a,
    b,
  )) -> Router(a, b)
pub fn static(router: Router(a, b), path: String, config: Static) -> Router(
  a,
  b,
)

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)) -> List(#(String, Route))
Search Document