Skip to content

Commit e76b284

Browse files
NinoFlorisKrzysztof-Cieslak
authored andcommitted
Fixes other spots that are related to #143, DeleteAll and Index, brings consistency, all plugs are now only run after route check
1 parent a30bf9d commit e76b284

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

src/Saturn/Controller.fs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -141,26 +141,17 @@ module Controller =
141141
else
142142
actions |> List.fold (fun acc e -> addPlug acc e handler) state
143143

144-
member private __.AddHandler<'Output> state action (handler: HttpContext -> Task<'Output>) (path: string) =
145-
let route = route path
146-
147-
let handler =
148-
match typeof<'Output> with
149-
| k when k = typeof<HttpContext option> -> fun _ ctx -> handler ctx |> unbox<HttpFuncResult>
150-
| _ -> fun _ ctx -> handler ctx |> response<'Output> ctx
151-
152-
match state.Plugs.TryFind action with
153-
| Some acts -> (succeed |> List.foldBack (fun e acc -> acc >=> e) acts) >=> route >=> handler
154-
| None -> route >=> handler
155-
156144
member private __.AddHandlerWithRoute<'Output> state action (handler: HttpContext -> Task<'Output>) route =
157145
let handler =
158146
match typeof<'Output> with
159147
| k when k = typeof<HttpContext option> -> fun _ ctx -> handler ctx |> unbox<HttpFuncResult>
160148
| _ -> fun _ ctx -> handler ctx |> response<'Output> ctx
161149

162150
match state.Plugs.TryFind action with
163-
| Some acts -> (succeed |> List.foldBack (fun e acc -> acc >=> e) acts) >=> route >=> handler
151+
| Some acts ->
152+
// Apply route test before applying plugs
153+
let plugs = succeed |> List.foldBack (fun e acc -> acc >=> e) acts
154+
route >=> plugs >=> handler
164155
| None -> route >=> handler
165156

166157
member private __.AddKeyHandler<'Output> state action (handler: HttpContext -> 'Key -> Task<'Output>) path =
@@ -173,9 +164,8 @@ module Controller =
173164

174165
match state.Plugs.TryFind action with
175166
| Some acts ->
176-
let plugs: HttpHandler =
177-
(succeed |> List.foldBack (fun e acc -> acc >=> e) acts)
178-
// apply route test before applying plugs
167+
// Apply route test before applying plugs
168+
let plugs = succeed |> List.foldBack (fun e acc -> acc >=> e) acts
179169
route (fun key -> plugs >=> (handler key))
180170

181171
| None -> route handler
@@ -206,22 +196,24 @@ module Controller =
206196
let route = route "/"
207197
if ctx.Request.Path.Value.EndsWith("/") then
208198
route next ctx
209-
else
199+
else if (SubRouting.getNextPartOfPath ctx = "") then
200+
// TODO this could go away pending discussion about ctx.Request.Path modification.
201+
// Only change Path at the end of the road, otherwise we cannot have all plugs fire after route check.
210202
ctx.Request.Path <- PathString(ctx.Request.Path.Value + "/")
211203
route next ctx
204+
else
205+
route next ctx
212206
choose [
213207
yield GET >=> choose [
214208
let addToSiteMap = addToSiteMap "GET"
215209

216210
if state.Add.IsSome then
217211
let path = "/add"
218212
addToSiteMap path
219-
yield this.AddHandler state Add state.Add.Value path
213+
yield this.AddHandlerWithRoute state Add state.Add.Value (route path)
220214

221215
if state.Index.IsSome then
222-
let path = "/"
223-
let handler (ctx: HttpContext) = ctx.Request.Path <- PathString(ctx.Request.Path.ToString() + "/"); state.Index.Value(ctx)
224-
addToSiteMap path
216+
addToSiteMap "/"
225217
yield this.AddHandlerWithRoute state Index state.Index.Value trailingSlashHandler
226218

227219
if keyFormat.IsSome then
@@ -269,8 +261,6 @@ module Controller =
269261
let addToSiteMap = addToSiteMap "DELETE"
270262

271263
if state.DeleteAll.IsSome then
272-
let path = "/"
273-
addToSiteMap path
274264
yield this.AddHandlerWithRoute state DeleteAll state.DeleteAll.Value trailingSlashHandler
275265

276266
if keyFormat.IsSome then

0 commit comments

Comments
 (0)