Skip to content

Instantly share code, notes, and snippets.

@dustinmoris
Last active June 9, 2020 14:37
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 dustinmoris/3951273c3e5a3f525127ebb072f359fd to your computer and use it in GitHub Desktop.
Save dustinmoris/3951273c3e5a3f525127ebb072f359fd to your computer and use it in GitHub Desktop.
Isaac Problem
namespace GiraffeApp
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.Hosting
open Giraffe
open System.Threading.Tasks
module Program =
let earlyReturn : HttpFunc = Some >> Task.FromResult
let authCheck : HttpHandler =
fun next ctx ->
let isValidAuth =
ctx.Request.Query.ContainsKey("apiKey")
&& ctx.Request.Query.["apiKey"].ToString() = "test123"
match isValidAuth with
| true -> next ctx
| false -> setStatusCode 401 earlyReturn ctx
let app : HttpHandler =
choose [
authCheck >=> route "/api/foo" >=> text "foo"
authCheck >=> route "/api/bar" >=> text "bar"
]
let configureApp (appBuilder : IApplicationBuilder) =
appBuilder.UseGiraffe(app)
let StartWebServer args =
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(fun webBuilder ->
webBuilder.Configure(configureApp) |> ignore
)
.Build()
.Run()
0
[<EntryPoint>]
let main args = StartWebServer args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment