Skip to content

Commit

Permalink
Routers are now handlers rather than middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
aantron committed Feb 11, 2022
1 parent 7320f87 commit a365a17
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 22 deletions.
11 changes: 4 additions & 7 deletions src/dream.mli
Original file line number Diff line number Diff line change
Expand Up @@ -1439,11 +1439,10 @@ val set_server_stream : request -> stream -> unit

(** {1 Routing} *)

val router : route list -> middleware
(** Creates a router. Besides interpreting routes, a router is a middleware
which calls its next handler if none of its routes match the request. Route
components starting with [:] are parameters, which can be retrieved with
{!Dream.param}. See example
val router : route list -> handler
(** Creates a router. If none of the routes match the request, the router
returns {!Dream.not_found}. Route components starting with [:] are
parameters, which can be retrieved with {!Dream.param}. See example
{{:https://github.com/aantron/dream/tree/master/example/3-router#files}
[3-router]} \[{{:http://dream.as/3-router} playground}\].
Expand All @@ -1454,7 +1453,6 @@ val router : route list -> middleware
Dream.get "/echo/:word" @@ fun request ->
Dream.html (Dream.param "word" request);
]
@@ Dream.not_found
]}
{!Dream.scope} is the main form of site composition. However, Dream also
Expand All @@ -1466,7 +1464,6 @@ val router : route list -> middleware
@@ Dream.router [
Dream.get "/static/**" @@ Dream.static "www/static";
]
@@ Dream.not_found
]}
[**] causes the request's path to be trimmed by the route prefix, and the
Expand Down
4 changes: 2 additions & 2 deletions src/server/router.ml
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ let param request name =
let router routes =
let routes = List.flatten routes in

fun next_handler request ->
fun request ->

(* TODO Probably unnecessary (because it's better to just convert this to a
trie), but the method can be checked before descending down the route. *)
Expand Down Expand Up @@ -297,4 +297,4 @@ let router routes =
try_routes
params prefix path routes
(fun handler request -> handler request)
(fun () -> next_handler request)
(fun () -> Helpers.not_found request)
2 changes: 1 addition & 1 deletion src/server/router.mli
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ val scope : string -> Message.middleware list -> route list -> route
(* The middleware and the path parameter retriever. With respect to path
parameters, the middleware is the setter, and the retriever is, of course,
the getter. *)
val router : route list -> Message.middleware
val router : route list -> Message.handler
val param : Message.request -> string -> string

(* Variables used by the router. *)
Expand Down
20 changes: 8 additions & 12 deletions test/expect/server/router.ml
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ let%expect_test _ =
let show ?(prefix = "/") ?(method_ = `GET) target router =
try
Dream.request ~method_ ~target ""
|> Dream.test ~prefix
(router @@ fun _ -> Dream.respond ~status:`Not_Found "")
|> Dream.test ~prefix router
|> fun response ->
let body =
Dream.client_stream response
Expand Down Expand Up @@ -349,9 +348,9 @@ let%expect_test _ =
Dream.param: missing path parameter "x" |}]

let%expect_test _ =
show "/" @@ (fun next_handler request ->
show "/" @@ (fun request ->
ignore (Dream.param request "x");
next_handler request);
Dream.empty `Not_Found);
[%expect {| Dream.param: missing path parameter "x" |}]

(* Router respects site prefix. *)
Expand Down Expand Up @@ -525,7 +524,7 @@ let%expect_test _ =

(* Router sequence works. *)

let%expect_test _ =
(* let%expect_test _ =
show "/abc/def" @@ Dream.pipeline [
Dream.router [
Dream.get "/abc/ghi" (fun _ -> Dream.respond "first");
Expand All @@ -536,7 +535,7 @@ let%expect_test _ =
];
[%expect {|
Response: 200 OK
second |}]
second |}] *)

(* Wildcard routes. *)

Expand Down Expand Up @@ -701,8 +700,7 @@ let%expect_test _ =
|> Dream.router [
Dream.get "/def" (fun request ->
Dream.respond (Dream.prefix request ^ " " ^ path request));
]
@@ (fun _ -> Dream.respond ~status:`Not_Found ""))
])
];
[%expect {|
Response: 200 OK
Expand All @@ -719,8 +717,7 @@ let%expect_test _ =
(Dream.param request "x")
(Dream.param request "y")
(path request));
]
@@ (fun _ -> Dream.respond ~status:`Not_Found ""))
])
];
[%expect {|
Response: 200 OK
Expand All @@ -736,8 +733,7 @@ let%expect_test _ =
(Dream.prefix request)
(Dream.param request "x")
(path request));
]
@@ (fun _ -> Dream.respond ~status:`Not_Found ""))
])
];
[%expect {|
Response: 200 OK
Expand Down

0 comments on commit a365a17

Please sign in to comment.