Last active
August 29, 2015 14:11
-
-
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#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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