package wtr

  1. Overview
  2. Docs
Well Typed Router

Install

Dune Dependency

Authors

Maintainers

Sources

wtr-v2.0.0.tbz
sha256=6242f9bec932d49f41957a05b2ddd826f38ef05ed14d87737962585143cf02aa
sha512=960a8bc6dad6d40bb248f25ec1560993c14b52fb928542ed3b5dc72842413101492ca7e0a708448aa6f3207a7c201073782fe045964d6a398cf2a9051a865092

Description

Wtr - A typed router for OCaml web applications

Published: 06 Aug 2021

README

Wtr - Well Typed Router

Wtr - A typed router for OCaml web applications.

Wtr API          CoHttp Demo

Overview

Wtr is typed router for OCaml web applications.

  • A trie based router. Route matching is efficient and fast.

  • Route handlers are type-checked during compilation.

  • Supports matching and capturing URI path components, eg /home/about/:int.

  • Supports matching and capturing URI query parameters, eg /home/about?q=:int&q1=hello.

  • Supports matching HTTP methods, eg GET, POST, PUT, HEAD, DELETE etc.

  • Supports converting captured URI components to OCaml and custom user defined data types.

  • wtr.ppx is used to specify uri values. If you know how to type URI path in a browser location, then you already know how to use wtr.

  • Minimal learning overhead. Wtr is centered around 3 API calls, a ppx -%wtr - and your existing HTTP url/uri knowledge:

    • Wtr.create - creates a router - 'a Wtr.t - from a list of route values.

    • Wtr.match' - matches a given uri path in a router.

    • Wtr.create_decoder - allows you to create a user defined decoder. see Fruit module in the demo below.

Wtr Demo

# #require "wtr, wtr.ppx";;
module Fruit = struct
  type t = Apple | Orange | Pineapple

  let t : t Wtr.decoder =
    Wtr.create_decoder ~name:"fruit" ~decode:(function
      | "apple" -> Some Apple
      | "orange" -> Some Orange
      | "pineapple" -> Some Pineapple
      | _ -> None )
end

let about_page = "about page"
let prod_page i = "Int page. number : " ^ string_of_int i
let float_page f = "Float page. number : " ^ string_of_float f
let contact_page nm num = "Contact. Hi, " ^ nm ^ ". Num " ^ string_of_int num
let product1 name id q = Format.sprintf "Product1 %s. Id: %d. q = %b" name id q
let product2 name id = Format.sprintf "Product2 %s. Id: %d." name id
;;
let fruit_page = function
  | Fruit.Apple -> "Apples are juicy!"
  | Orange -> "Orange is a citrus fruit."
  | Pineapple -> "Pineapple has scaly skin"

let faq category_id =
  let category_name =
    match category_id with
    | 1 -> "products"
    | 2 -> "insurance"
    | 3 -> "returns"
    | _ -> "unknown"
  in
  "FAQ page for category : " ^ category_name

let router =
  Wtr.(
    create
      [ {%wtr| get,post,head,delete  ; /home/about/            |} about_page
      ; {%wtr| head,delete           ; /home/:int/             |} prod_page
      ; {%wtr| get,post              ; /home/:float/           |} float_page
      ; {%wtr| get; /contact/*/:int                            |} contact_page
      ; {%wtr| get; /product/:string?section=:int&q=:bool      |} product1
      ; {%wtr| get; /product/:string?section=:int&q1=yes       |} product2
      ; {%wtr| get; /fruit/:Fruit                              |} fruit_page
      ; {%wtr| GET; /faq/:int/**                               |} faq ])

let p () = 
  Printf.printf "\n====Router Match Results====\n" ;
  [ Wtr.match' `GET "/home/100001.1/" router
  ; Wtr.match' `DELETE "/home/100001/" router
  ; Wtr.match' `GET "/home/about/" router
  ; Wtr.match' `GET "/product/dyson350?section=233&q=true" router
  ; Wtr.match' `GET "/product/dyson350?section=2&q=false" router
  ; Wtr.match' `GET "/product/dyson350?section=2&q1=yes" router
  ; Wtr.match' `GET "/product/dyson350?section=2&q1=no" router
  ; Wtr.match' `GET "/fruit/apple" router
  ; Wtr.match' `GET "/fruit/orange" router
  ; Wtr.match' `GET "/fruit/pineapple" router
  ; Wtr.match' `GET "/fruit/guava" router
  ; Wtr.match' `GET "/faq/1/" router
  ; Wtr.match' `GET "/faq/1/whatever" router
  ; Wtr.match' `GET "/faq/2/whateasdfasdfasdf" router ]
  |> List.iteri (fun i -> function
       | Some s -> Printf.printf "%3d: %s\n" (i + 1) s
       | None -> Printf.printf "%3d: None\n" (i + 1) );;

Demo Output

# p () ;;
====Router Match Results====
  1: Float page. number : 100001.1
  2: Int page. number : 100001
  3: about page
  4: Product1 dyson350. Id: 233. q = true
  5: Product1 dyson350. Id: 2. q = false
  6: Product2 dyson350. Id: 2.
  7: None
  8: Apples are juicy!
  9: Orange is a citrus fruit.
 10: Pineapple has scaly skin
 11: None
 12: FAQ page for category : products
 13: FAQ page for category : products
 14: FAQ page for category : insurance
- : unit = ()

Dependencies (4)

  1. ppxlib >= "0.22.0"
  2. uri >= "4.2.0"
  3. ocaml >= "4.12.0"
  4. dune >= "2.8"

Dev Dependencies (3)

  1. odoc with-doc
  2. mdx with-test
  3. ppx_expect with-test

Used by

None

Conflicts

None

OCaml

Innovation. Community. Security.