@@ -2,7 +2,18 @@ type http_method = [ `DELETE | `GET | `POST | `PUT ]
2
2
3
3
module Witness = Ppx_deriving_router_witness
4
4
5
+ module type IO = sig
6
+ type 'a t
7
+
8
+ val return : 'a -> 'a t
9
+ val fail : exn -> 'a t
10
+ val bind : 'a t -> ('a -> 'b t ) -> 'b t
11
+ val catch : (unit -> 'a t ) -> ('a , exn ) result t
12
+ end
13
+
5
14
module type REQUEST = sig
15
+ module IO : IO
16
+
6
17
type t
7
18
8
19
val path : t -> string
@@ -11,14 +22,16 @@ module type REQUEST = sig
11
22
val queries : t -> (string * string ) list
12
23
(* request queries component, url decoded *)
13
24
14
- val body : t -> string Lwt .t
25
+ val body : t -> string IO .t
15
26
(* request body *)
16
27
17
28
val method_ : t -> http_method
18
29
(* request method *)
19
30
end
20
31
21
32
module type RESPONSE = sig
33
+ module IO : IO
34
+
22
35
type status
23
36
24
37
val status_ok : status
@@ -29,7 +42,7 @@ module type RESPONSE = sig
29
42
type t
30
43
31
44
val respond :
32
- status :status -> headers :(string * string ) list -> string -> t Lwt .t
45
+ status :status -> headers :(string * string ) list -> string -> t IO .t
33
46
end
34
47
35
48
module type RETURN = sig
@@ -42,13 +55,15 @@ module type RETURN = sig
42
55
end
43
56
44
57
module type S = sig
58
+ module IO : IO
59
+
45
60
type json = Yojson.Basic .t
46
61
47
- module Request : REQUEST
62
+ module Request : REQUEST with module IO = IO
48
63
49
64
type request = Request .t
50
65
51
- module Response : RESPONSE
66
+ module Response : RESPONSE with module IO = IO
52
67
53
68
type response = Response .t
54
69
@@ -70,7 +85,7 @@ module type S = sig
70
85
| Encode_raw : response encode
71
86
| Encode_json : ('a -> json ) -> 'a encode
72
87
73
- val encode : 'a encode -> 'a return -> response Lwt .t
88
+ val encode : 'a encode -> 'a return -> response IO .t
74
89
75
90
type 'v route =
76
91
| Route : ('a , 'v ) Routes .path * 'a * ('v -> 'w ) -> 'w route
@@ -82,13 +97,13 @@ module type S = sig
82
97
83
98
type 'a router
84
99
85
- val make : (request -> 'a Lwt .t ) Routes .router -> 'a router
100
+ val make : (request -> 'a IO .t ) Routes .router -> 'a router
86
101
87
102
val handle :
88
103
'a router ->
89
- ('a -> request -> response Lwt .t ) ->
104
+ ('a -> request -> response IO .t ) ->
90
105
request ->
91
- response Lwt .t
106
+ response IO .t
92
107
(* * handle request given a router and a dispatcher *)
93
108
94
109
val dispatch :
@@ -99,25 +114,28 @@ module type S = sig
99
114
| `Method_not_allowed
100
115
| `Not_found
101
116
| `Ok of 'a ]
102
- Lwt .t
117
+ IO .t
103
118
end
104
119
end
105
120
106
121
module Make
107
122
(Request : REQUEST )
108
- (Response : RESPONSE )
123
+ (Response : RESPONSE with module IO = Request.IO )
109
124
(Return : RETURN with type status = Response.status ) :
110
125
S
111
126
with type Request. t = Request. t
112
127
and type Response. t = Response. t
113
128
and type Response. status = Response. status
114
129
and type 'a Return. t = 'a Return. t
130
+ and type 'a IO. t = 'a Request.IO. t
131
+ and type 'a IO. t = 'a Response.IO. t
115
132
and module Witness = Witness = struct
116
133
type json = Yojson.Basic .t
117
134
type request = Request .t
118
135
type response = Response .t
119
136
type 'a return = 'a Return .t
120
137
138
+ module IO = Request .IO
121
139
module Request = Request
122
140
module Response = Response
123
141
module Return = Return
@@ -135,7 +153,7 @@ module Make
135
153
| Encode_raw : response encode
136
154
| Encode_json : ('a -> json ) -> 'a encode
137
155
138
- let encode : type a. a encode -> a Return.t -> response Lwt .t =
156
+ let encode : type a. a encode -> a Return.t -> response IO .t =
139
157
fun enc x ->
140
158
let status =
141
159
Option. value ~default: Response. status_ok (Return. status x)
@@ -145,7 +163,7 @@ module Make
145
163
| Encode_raw , x -> (
146
164
match Return. data x with
147
165
| None -> Response. respond ~status ~headers " "
148
- | Some x -> Lwt . return x)
166
+ | Some x -> IO . return x)
149
167
| Encode_json to_json , x -> (
150
168
match Return. data x with
151
169
| None -> Response. respond ~status ~headers " "
@@ -169,28 +187,28 @@ module Make
169
187
170
188
let to_route (Route (path , a , f )) = Routes. (map f (route path a))
171
189
172
- type 'a router = (Request .t -> 'a Lwt .t ) Routes .router
190
+ type 'a router = (Request .t -> 'a IO .t ) Routes .router
173
191
174
192
let make x = x
175
193
176
194
let dispatch (router : _ router ) req =
177
195
let target = Request. path req in
178
196
match Routes. match ' router ~target with
179
197
| Routes. FullMatch v | Routes. MatchWithTrailingSlash v ->
180
- Lwt . bind
181
- (Lwt_result . catch (fun () -> v req))
198
+ IO . bind
199
+ (IO . catch (fun () -> v req))
182
200
(function
183
- | Ok v -> Lwt . return (`Ok v)
201
+ | Ok v -> IO . return (`Ok v)
184
202
| Error (Invalid_query_parameter (x , y )) ->
185
- Lwt . return (`Invalid_query_parameter (x, y))
203
+ IO . return (`Invalid_query_parameter (x, y))
186
204
| Error (Invalid_body reason ) ->
187
- Lwt . return (`Invalid_body reason)
188
- | Error Method_not_allowed -> Lwt . return `Method_not_allowed
189
- | Error exn -> Lwt . fail exn )
190
- | Routes. NoMatch -> Lwt . return `Not_found
205
+ IO . return (`Invalid_body reason)
206
+ | Error Method_not_allowed -> IO . return `Method_not_allowed
207
+ | Error exn -> IO . fail exn )
208
+ | Routes. NoMatch -> IO . return `Not_found
191
209
192
210
let handle (router : _ router ) f req =
193
- Lwt . bind (dispatch router req) (function
211
+ IO . bind (dispatch router req) (function
194
212
| `Ok v -> f v req
195
213
| `Invalid_query_parameter (param , msg ) ->
196
214
Response. respond ~status: Response. status_bad_request
0 commit comments