Skip to content

Instantly share code, notes, and snippets.

@gubser
Created October 11, 2018 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gubser/12e38d9efa7ea1f715d3819adbda3db5 to your computer and use it in GitHub Desktop.
Save gubser/12e38d9efa7ea1f715d3819adbda3db5 to your computer and use it in GitHub Desktop.
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