Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] New router #215

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions opium/src/app.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ open Import
module Server = Httpaf_lwt_unix.Server
module Reqd = Httpaf.Reqd
open Lwt.Syntax
module Route = Router.Route

let err_invalid_host host =
Lwt.fail_invalid_arg ("Could not get host info for `" ^ host ^ "`")
Expand Down
46 changes: 20 additions & 26 deletions opium/src/middlewares/middleware_router.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,48 @@ module Method_map = Map.Make (struct
;;
end)

type 'a t = (Route.t * 'a) list Method_map.t
type t = Rock.Handler.t Method_map.t Router.t

let empty = Method_map.empty
let empty = Router.empty

let get t meth =
match Method_map.find_opt meth t with
| None -> []
| Some xs -> List.rev xs
;;

let add t ~route ~meth ~action =
Method_map.update
meth
(function
| None -> Some [ route, action ]
| Some xs -> Some ((route, action) :: xs))
t
let add (t : t) ~route ~meth ~action =
Router.update t route ~f:(function
| None -> Method_map.singleton meth action
| Some m -> Method_map.add meth action m)
;;

(** finds matching endpoint and returns it with the parsed list of parameters *)
let matching_endpoint endpoints meth uri =
let endpoints = get endpoints meth in
List.find_map endpoints ~f:(fun ep ->
uri |> Route.match_url (fst ep) |> Option.map (fun p -> ep, p))
let matching_endpoint (endpoints : t) meth uri =
match Router.match_url endpoints uri with
| None -> None
| Some (a, params) ->
(match Method_map.find_opt meth a with
| None -> None
| Some h -> Some (h, params))
;;

module Env = struct
let key : Route.matches Context.key =
Context.Key.create ("path_params", Route.sexp_of_matches)
let key : Router.Params.t Context.key =
Context.Key.create ("path_params", Router.Params.sexp_of_t)
;;
end

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

(* not param_exn since if the endpoint was selected it's likely that the parameter is
already there *)
let param req param =
let { Route.params; _ } = Context.find_exn Env.key req.Request.env in
List.assoc param params
let params = Context.find_exn Env.key req.Request.env in
Router.Params.named params param
;;

let m endpoints =
let filter default req =
match matching_endpoint endpoints req.Request.meth req.Request.target with
| None -> default req
| Some (endpoint, params) ->
| Some (handler, params) ->
let env_with_params = Context.add Env.key params req.Request.env in
(snd endpoint) { req with Request.env = env_with_params }
handler { req with Request.env = env_with_params }
in
Rock.Middleware.create ~name:"Router" ~filter
;;
8 changes: 4 additions & 4 deletions opium/src/middlewares/middleware_router.mli
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type 'a t
type t

val m : Rock.Handler.t t -> Rock.Middleware.t
val empty : 'action t
val add : 'a t -> route:Route.t -> meth:Method.t -> action:'a -> 'a t
val m : t -> Rock.Middleware.t
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have any guidelines on naming conventions for similar functions in Opium? I assume m is "middleware", but would be good to know if this should be used more generally.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's my original naming convention for a module that provides a middleware. It seemed a little long to name things like Middleware_foo.middleware. Not so sure if this convention is still beneficial.

val empty : t
val add : t -> route:Router.Route.t -> meth:Method.t -> action:Rock.Handler.t -> t
val param : Request.t -> string -> string
val splat : Request.t -> string list
6 changes: 5 additions & 1 deletion opium/src/opium.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module Private = struct
module Router = Router
end

module Context = Context
module Headers = Headers
module Cookie = Cookie
Expand All @@ -8,8 +12,8 @@ module Body = Body
module Request = Request
module Response = Response
module App = App
module Route = Route
module Auth = Auth
module Route = Router.Route
module Router = Middleware_router

module Handler = struct
Expand Down
16 changes: 11 additions & 5 deletions opium/src/opium.mli
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
module Private : sig
module Router : module type of struct
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, module type of Router wouldn't be the same thing? Or is this to allow adding other elements to the signature?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall that the simplest form omitted some type equalities for types inside Router.t. So if one used Router from outside this alias, the type Router.t wouldn't be equal. But I think this bug was fixed not too long ago. I'll see if the simpler form works as well.

include Router
end
end

module Context = Context
module Headers = Headers
module Cookie = Cookie
Expand All @@ -8,14 +14,14 @@ module Body = Body
module Request = Request
module Response = Response
module App = App
module Route = Route
module Auth = Auth
module Route = Router.Route

module Router : sig
type 'action t
type t

val empty : 'action t
val add : 'a t -> route:Route.t -> meth:Method.t -> action:'a -> 'a t
val empty : t
val add : t -> route:Route.t -> meth:Method.t -> action:Rock.Handler.t -> t
val param : Request.t -> string -> string
val splat : Request.t -> string list
end
Expand Down Expand Up @@ -88,7 +94,7 @@ module Middleware : sig

will redirect any URI containing two segments with the last segment containing
"hello" to the handler defined in [Handler.hello_world]. *)
val router : Rock.Handler.t Router.t -> Rock.Middleware.t
val router : Router.t -> Rock.Middleware.t

(** {3 [debugger]} *)

Expand Down
116 changes: 0 additions & 116 deletions opium/src/route.ml

This file was deleted.

21 changes: 0 additions & 21 deletions opium/src/route.mli

This file was deleted.

Loading