espresso/router

Types

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

Constructors

  • ServiceHandler(
      OrderedMap(Method, Service(req, assigns, session, res)),
    )
  • RouterHandler(Router(req, assigns, session, res))
  • StaticHandler(String, Static)
  • WebsocketHandler(Websocket)
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, session, res) {
  Router(
    middleware: Middleware(req, assigns, session, res, req, res),
    handlers: OrderedMap(
      String,
      Handler(req, assigns, session, res),
    ),
    not_found: Service(req, assigns, session, res),
  )
}

Constructors

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

Functions

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

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, c, 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, d), path: String, handler: fn(
    Request(a, b, c),
  ) -> Response(d)) -> Router(a, b, c, d)
pub fn post(router: Router(a, b, c, d), path: String, handler: fn(
    Request(a, b, c),
  ) -> Response(d)) -> Router(a, b, c, d)
pub fn put(router: Router(a, b, c, d), path: String, handler: fn(
    Request(a, b, c),
  ) -> Response(d)) -> Router(a, b, c, d)
pub fn router(router: Router(a, b, c, d), path: String, subrouter: Router(
    a,
    b,
    c,
    d,
  )) -> Router(a, b, c, d)
pub fn static(router: Router(a, b, c, d), path: String, config: Static) -> Router(
  a,
  b,
  c,
  d,
)

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, d)) -> List(
  #(String, Route(a, b, c, d)),
)
pub fn websocket(router: Router(a, b, c, d), path: String, handler: fn(
    String,
  ) -> Frame) -> Router(a, b, c, d)

Adds a websocket handler to a path

Example

import espresso/router.{websocket}
import espresso/websocket.{Websocket}

pub fn main() {
 let router =
   router.new()
   |> websocket(
     "/socket",
     fn(frame) {
       case frame {
         "chat:" <> _message -> websocket.Reply("Hello")
         "ping" -> websocket.Reply("pong")
         "actual_ping" -> websocket.Ping("")
         "pong" -> websocket.Pong("")
         _ -> websocket.Close("")
       }
     },
   )

 start(router)
}
Search Document