fyni/router

Types

Simple router based on Wisp.

Does not handle middlewares. If you want to have middlewares, see this example from the wisp repository.

pub type Router(user) {
  Router(
    routes: List(route.Route(user)),
    context: Result(user, Nil),
  )
}

Constructors

  • Router(
      routes: List(route.Route(user)),
      context: Result(user, Nil),
    )

Values

pub fn add(
  router: Router(user),
  method: http.Method,
  path: String,
  handler: fn(context.Context(user)) -> response.Response(
    wisp.Body,
  ),
) -> Router(user)

Appends a new route to the given router

This will be used later when matching a route

pub fn connect(
  router: Router(user),
  path: String,
  handler: fn(context.Context(user)) -> response.Response(
    wisp.Body,
  ),
) -> Router(user)

Appends a CONNECT route to the given router

This will be used later when matching a route

pub fn delete(
  router: Router(user),
  path: String,
  handler: fn(context.Context(user)) -> response.Response(
    wisp.Body,
  ),
) -> Router(user)

Appends a DELETE route to the given router

This will be used later when matching a route

pub fn from(routes: List(route.Route(user))) -> Router(user)

Creates a new router from given list of routes (without prefix)

pub fn from_with_prefix(
  routes: List(route.Route(user)),
  prefix prefix: String,
) -> Router(user)

Creates a new router from given list of routes with x prefix

pub fn get(
  router: Router(user),
  path: String,
  handler: fn(context.Context(user)) -> response.Response(
    wisp.Body,
  ),
) -> Router(user)

Appends a GET route to the given router

This will be used later when matching a route

pub fn handle(
  router: Router(user),
  request: request.Request(wisp.Connection),
  not_found: fn(context.Context(user)) -> response.Response(
    wisp.Body,
  ),
) -> response.Response(wisp.Body)
pub fn head(
  router: Router(user),
  path: String,
  handler: fn(context.Context(user)) -> response.Response(
    wisp.Body,
  ),
) -> Router(user)

Appends a HEAD route to the given router

This will be used later when matching a route

pub fn match(
  router: Router(user),
  request: request.Request(wisp.Connection),
) -> Result(route.Route(user), Nil)

Gets the first route matched by a given wisp.Request

The order of the given routes do matter, as this function will always return the first result.

pub fn new() -> Router(user)

Creates a new empty router.

pub fn openapi(
  router: Router(user),
  title title: String,
  version version: String,
) -> Router(user)

Creates a /openapi.json route

This route will create output following the OpenAPI Specification

pub fn options(
  router: Router(user),
  path: String,
  handler: fn(context.Context(user)) -> response.Response(
    wisp.Body,
  ),
) -> Router(user)

Appends a OPTIONS route to the given router

This will be used later when matching a route

pub fn patch(
  router: Router(user),
  path: String,
  handler: fn(context.Context(user)) -> response.Response(
    wisp.Body,
  ),
) -> Router(user)

Appends a PATCH route to the given router

This will be used later when matching a route

pub fn post(
  router: Router(user),
  path: String,
  handler: fn(context.Context(user)) -> response.Response(
    wisp.Body,
  ),
) -> Router(user)

Appends a POST route to the given router

This will be used later when matching a route

pub fn put(
  router: Router(user),
  path: String,
  handler: fn(context.Context(user)) -> response.Response(
    wisp.Body,
  ),
) -> Router(user)

Appends a PUT route to the given router

This will be used later when matching a route

pub fn trace(
  router: Router(user),
  path: String,
  handler: fn(context.Context(user)) -> response.Response(
    wisp.Body,
  ),
) -> Router(user)

Appends a TRACE route to the given router

This will be used later when matching a route

pub fn with_context(
  router: Router(user),
  context: user,
) -> Router(user)

Gives a user-made context to a router

Search Document