Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A variant of pathScan that also passes HttpContext to the handler
#r "<path/to/Suave.dll>"
open Suave
open Suave.Successful
open Suave.RequestErrors
/// <summary>
/// Same as pathScan but also passes the HttpContext to the handler h.
/// See pathScan: https://github.com/SuaveIO/suave/blob/0524552a876526ae6623ba6fdf4ca3a06feaf510/src/Suave/Combinators.fs#L413
/// </summary>
let pathScanContext (pf : PrintfFormat<_,_,_,_,'t>) (h : HttpContext -> 't -> WebPart) : WebPart =
let scan url =
try
let r = Sscanf.sscanf pf url
Some r
with _ -> None
let F (r: HttpContext) =
match scan r.request.path with
| Some p ->
let part = h r p
part r
| None ->
fail
F
// This lets us write the example ( https://suave.io/typed.html ) in a simpler form:
let handler ctx (a, b) =
// get something from ctx.request...
OK((a + b).ToString())
let testapp : WebPart =
choose
[ pathScanContext "/add/%d/%d" handler
NOT_FOUND "Found no handlers" ]
startWebServer defaultConfig testapp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.