package routes

  1. Overview
  2. Docs

Source file routes.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
module Routes_private = struct
  module Util = Util
end

module Map = Stdcompat.Map

module Method = struct
  type t =
    [ `GET
    | `HEAD
    | `POST
    | `PUT
    | `DELETE
    | `CONNECT
    | `OPTIONS
    | `TRACE
    | `Other of string
    ]

  let compare = compare
end

module MethodMap = Map.Make (Method)

type 'a t = 'a Parser.t
type 'a router = 'a t list MethodMap.t

let choice = Parser.choice

let with_method routes =
  List.fold_left
    (fun acc (meth, route) ->
      MethodMap.update
        meth
        (fun t ->
          match t with
          | None -> Some [ route ]
          | Some rs -> Some (route :: rs))
        acc)
    MethodMap.empty
    routes
;;

let match' routes target =
  if String.length target = 0
  then None
  else (
    let params = Util.split_path target in
    match Parser.parse routes params with
    | Some (r, []) -> Some r
    | _ -> None)
;;

let match_with_method routes ~target ~meth =
  if String.length target = 0
  then None
  else (
    let params = Util.split_path target in
    let rec route' = function
      | [] -> None
      | r :: rs ->
        (match Parser.parse r params with
        | Some (r, []) -> Some r
        | _ -> route' rs)
    in
    match MethodMap.find_opt meth routes with
    | None -> None
    | Some rs -> route' rs)
;;

let empty = Parser.empty
let str = Parser.str
let int = Parser.int
let int32 = Parser.int32
let int64 = Parser.int64
let bool = Parser.bool
let s = Parser.s
let apply = Parser.apply
let return = Parser.return

module Infix = struct
  include Parser.Infix
end
OCaml

Innovation. Community. Security.