Skip to content

Commit b02ee31

Browse files
committed
port old router to new router
Signed-off-by: Rudi Grinberg <[email protected]>
1 parent 8bb896d commit b02ee31

File tree

10 files changed

+46
-166
lines changed

10 files changed

+46
-166
lines changed

opium/src/app.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ open Import
22
module Server = Httpaf_lwt_unix.Server
33
module Reqd = Httpaf.Reqd
44
open Lwt.Syntax
5+
module Route = Router.Route
56

67
let err_invalid_host host =
78
Lwt.fail_invalid_arg ("Could not get host info for `" ^ host ^ "`")

opium/src/middlewares/middleware_router.ml

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,54 +10,48 @@ module Method_map = Map.Make (struct
1010
;;
1111
end)
1212

13-
type t = (Route.t * Rock.Handler.t) list Method_map.t
13+
type t = Rock.Handler.t Method_map.t Router.t
1414

15-
let empty = Method_map.empty
15+
let empty = Router.empty
1616

17-
let get t meth =
18-
match Method_map.find_opt meth t with
19-
| None -> []
20-
| Some xs -> List.rev xs
21-
;;
22-
23-
let add t ~route ~meth ~action =
24-
Method_map.update
25-
meth
26-
(function
27-
| None -> Some [ route, action ]
28-
| Some xs -> Some ((route, action) :: xs))
29-
t
17+
let add (t : t) ~route ~meth ~action =
18+
Router.update t route ~f:(function
19+
| None -> Method_map.singleton meth action
20+
| Some m -> Method_map.add meth action m)
3021
;;
3122

3223
(** finds matching endpoint and returns it with the parsed list of parameters *)
33-
let matching_endpoint endpoints meth uri =
34-
let endpoints = get endpoints meth in
35-
List.find_map endpoints ~f:(fun ep ->
36-
uri |> Route.match_url (fst ep) |> Option.map (fun p -> ep, p))
24+
let matching_endpoint (endpoints : t) meth uri =
25+
match Router.match_url endpoints uri with
26+
| None -> None
27+
| Some (a, params) ->
28+
(match Method_map.find_opt meth a with
29+
| None -> None
30+
| Some h -> Some (h, params))
3731
;;
3832

3933
module Env = struct
40-
let key : Route.matches Context.key =
41-
Context.Key.create ("path_params", Route.sexp_of_matches)
34+
let key : Router.Params.t Context.key =
35+
Context.Key.create ("path_params", Router.Params.sexp_of_t)
4236
;;
4337
end
4438

45-
let splat req = Context.find_exn Env.key req.Request.env |> fun route -> route.Route.splat
39+
let splat req = Context.find_exn Env.key req.Request.env |> Router.Params.unnamed
4640

4741
(* not param_exn since if the endpoint was selected it's likely that the parameter is
4842
already there *)
4943
let param req param =
50-
let { Route.params; _ } = Context.find_exn Env.key req.Request.env in
51-
List.assoc param params
44+
let params = Context.find_exn Env.key req.Request.env in
45+
Router.Params.named params param
5246
;;
5347

5448
let m endpoints =
5549
let filter default req =
5650
match matching_endpoint endpoints req.Request.meth req.Request.target with
5751
| None -> default req
58-
| Some (endpoint, params) ->
52+
| Some (handler, params) ->
5953
let env_with_params = Context.add Env.key params req.Request.env in
60-
(snd endpoint) { req with Request.env = env_with_params }
54+
handler { req with Request.env = env_with_params }
6155
in
6256
Rock.Middleware.create ~name:"Router" ~filter
6357
;;

opium/src/middlewares/middleware_router.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ type t
22

33
val m : t -> Rock.Middleware.t
44
val empty : t
5-
val add : t -> route:Route.t -> meth:Method.t -> action:Rock.Handler.t -> t
5+
val add : t -> route:Router.Route.t -> meth:Method.t -> action:Rock.Handler.t -> t
66
val param : Request.t -> string -> string
77
val splat : Request.t -> string list

opium/src/opium.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module Body = Body
1212
module Request = Request
1313
module Response = Response
1414
module App = App
15-
module Route = Route
15+
module Route = Router.Route
1616
module Router = Middleware_router
1717

1818
module Handler = struct

opium/src/opium.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module Body = Body
1414
module Request = Request
1515
module Response = Response
1616
module App = App
17-
module Route = Route
17+
module Route = Router.Route
1818

1919
module Router : sig
2020
type t

opium/src/route.ml

Lines changed: 0 additions & 107 deletions
This file was deleted.

opium/src/route.mli

Lines changed: 0 additions & 21 deletions
This file was deleted.

opium/src/router.ml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ module Route = struct
1111

1212
let equal = ( = )
1313

14+
let to_string t =
15+
let rec loop acc = function
16+
| Nil -> acc
17+
| Full_splat -> "**" :: acc
18+
| Literal (s, rest) -> loop (s :: acc) rest
19+
| Param (s, rest) ->
20+
let s = Option.value s ~default:"*" in
21+
loop (s :: acc) rest
22+
in
23+
loop [] t |> List.rev |> String.concat ~sep:"/"
24+
;;
25+
1426
let rec sexp_of_t (t : t) : Sexp.t =
1527
match t with
1628
| Nil -> Atom "Nil"
@@ -55,15 +67,15 @@ module Route = struct
5567
else Literal (token, parse_tokens params tokens)
5668
;;
5769

58-
let of_string_exn s =
70+
let of_string s =
5971
let tokens = String.split_on_char ~sep:'/' s in
6072
match tokens with
6173
| "" :: tokens -> parse_tokens [] tokens
6274
| _ -> raise (E "route must start with /")
6375
;;
6476

65-
let of_string s =
66-
match of_string_exn s with
77+
let of_string_result s =
78+
match of_string s with
6779
| exception E s -> Error s
6880
| s -> Ok s
6981
;;

opium/src/router.mli

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ open Import
33
module Route : sig
44
type t
55

6-
val of_string : string -> (t, string) result
7-
val of_string_exn : string -> t
6+
val of_string_result : string -> (t, string) result
7+
val of_string : string -> t
88
val sexp_of_t : t -> Sexp.t
9+
val to_string : t -> string
910
end
1011

1112
module Params : sig

opium/test/opium_router_tests.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Router = Opium.Private.Router
33
open Router
44

55
let valid_route s =
6-
match Route.of_string s with
6+
match Route.of_string_result s with
77
| Error err -> print_endline ("[FAIL] invalid route " ^ err)
88
| Ok r -> Format.printf "[PASS] valid route:%a@." Sexp.pp_hum (Route.sexp_of_t r)
99
;;
@@ -71,15 +71,15 @@ let%expect_test "dummy router matches nothing" =
7171
7272
let%expect_test "we can add & match literal routes" =
7373
let url = "/foo/bar" in
74-
let route = Route.of_string_exn url in
74+
let route = Route.of_string url in
7575
let router = add empty route () in
7676
test_match_url router url;
7777
[%expect {|
7878
matched with params: ((named ()) (unnamed ())) |}]
7979
;;
8080
8181
let%expect_test "we can extract parameter after match" =
82-
let route = Route.of_string_exn "/foo/*/:bar" in
82+
let route = Route.of_string "/foo/*/:bar" in
8383
let router = add empty route () in
8484
test_match_url router "/foo/100/baz";
8585
test_match_url router "/foo/100";
@@ -93,7 +93,7 @@ let%expect_test "we can extract parameter after match" =
9393
9494
let of_routes routes =
9595
List.fold_left
96-
(fun router (route, data) -> add router (Route.of_string_exn route) data)
96+
(fun router (route, data) -> add router (Route.of_string route) data)
9797
empty
9898
routes
9999
;;

0 commit comments

Comments
 (0)