Skip to content

Instantly share code, notes, and snippets.

@beyond-code-github
Last active August 29, 2015 14:11
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 beyond-code-github/97be45d299328c430a8c to your computer and use it in GitHub Desktop.
Save beyond-code-github/97be45d299328c430a8c to your computer and use it in GitHub Desktop.
Example of custom OWIN pipelines based on routing with Superscribe in F#
type RequireHttps(next: AppFunc) =
member this.Invoke(environment: IDictionary<string, obj>) : Task =
match environment.["owin.RequestScheme"].ToString() with
| "https" -> (next.Invoke(environment))
| other ->
environment.["owin.ResponseStatusCode"] <- 400 :> obj
environment.["owin.ResponseReasonPhrase"] <- "Connection was not secure" :> obj
Task.FromResult<obj>(null) :> Task
type RequireAuthentication(next: AppFunc) =
member this.Invoke(environment: IDictionary<string, obj>) : Task =
let requestHeaders = environment.["owin.RequestHeaders"] :?> Dictionary<string, string>
match requestHeaders.["Authentication"] with
| "ABC123" -> (next.Invoke(environment))
| other ->
environment.["owin.ResponseStatusCode"] <- 403 :> obj
environment.["owin.ResponseReasonPhrase"] <- "Authentication required" :> obj
Task.FromResult<obj>(null) :> Task
type Startup() =
member x.Configuration(app: Owin.IAppBuilder) =
let define = OwinRouteEngineFactory.Create();
app.UseSuperscribeRouter(define).UseSuperscribeHandler(define) |> ignore
define.Route("admin/token", fun o -> "{ token: ABC123 }" :> obj) |> ignore
define.Route("admin/users", fun o -> "List all users" :> obj) |> ignore
let users = define.Route("users")
define.Route(users / String "UserId", fun o -> "User details for " + o?Parameters?UserId :> obj) |> ignore
define.Pipeline("admin").Use<RequireHttps>() |> ignore
define.Pipeline("admin/users").Use<RequireAuthentication>() |> ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment