Skip to content

Instantly share code, notes, and snippets.

@TheAngryByrd
Last active April 14, 2022 04:29
Show Gist options
  • Save TheAngryByrd/8d65b1ab1ea4da60ac7ed8c1449f60ff to your computer and use it in GitHub Desktop.
Save TheAngryByrd/8d65b1ab1ea4da60ac7ed8c1449f60ff to your computer and use it in GitHub Desktop.
F# MapGet helper
open System
open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.Hosting
open Microsoft.AspNetCore.Http
open System.Threading.Tasks
let mapGet (pattern : string) handler (app : WebApplication) =
app.MapGet(pattern, (Func<_>(handler))) |> ignore
let mapGetAsync (pattern : string) (handler : HttpContext -> Task) (app : WebApplication) =
app.MapGet(pattern, RequestDelegate(handler)) |> ignore
let builder = WebApplication.CreateBuilder()
let app = builder.Build()
app |> mapGet "/" (fun () -> "hello world!") |> ignore
app |> mapGetAsync "/hello" (fun ctx -> task { do! ctx.Response.WriteAsync("hello", ctx.RequestAborted)}) |> ignore
app.Run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment