Skip to content

Instantly share code, notes, and snippets.

@AngelMunoz
Created April 12, 2022 01:17
Show Gist options
  • Save AngelMunoz/461092c0a506881e38a3b047e9702d24 to your computer and use it in GitHub Desktop.
Save AngelMunoz/461092c0a506881e38a3b047e9702d24 to your computer and use it in GitHub Desktop.
F# Http Frameworks from zero
cd repos; dotnet new webapi -lang F# -o FalcoSample
cd FalcoSample; dotnet add package Falco
code -r .
open System
open Microsoft.AspNetCore.Http
open Falco
open Falco.Routing
open Falco.HostBuilder
open FalcoSample
let summaries =
[| "Freezing"
"Bracing"
"Chilly"
"Cool"
"Mild"
"Warm"
"Balmy"
"Hot"
"Sweltering"
"Scorching" |]
let helloHandler (ctx: HttpContext) =
ctx
|> Response.ofHtmlString "<h1>Hello, World!</h1>"
let weatherHandler (ctx: HttpContext) =
let rng = System.Random.Shared
let summaries =
[| for index in 0..4 ->
{ Date = DateTime.Now.AddDays(float index)
TemperatureC = rng.Next(-20, 55)
Summary = summaries.[rng.Next(summaries.Length)] } |]
ctx |> Response.ofJson summaries
webHost [||] {
endpoints [ get "/" helloHandler
get "/weather" weatherHandler ]
}
cd repos; dotnet new webapi -lang F# -o SaturnSample
cd SaturnSample; dotnet add package Saturn
code -r .
open System
open Microsoft.AspNetCore.Http
open Saturn
open Giraffe
open SaturnSample
let summaries =
[| "Freezing"
"Bracing"
"Chilly"
"Cool"
"Mild"
"Warm"
"Balmy"
"Hot"
"Sweltering"
"Scorching" |]
let helloHandler next (ctx: HttpContext) =
htmlString "<h1>Hello, World!</h1>" next ctx
let weatherHandler next (ctx: HttpContext) =
let rng = System.Random.Shared
let summaries =
[| for index in 0..4 ->
{ Date = DateTime.Now.AddDays(float index)
TemperatureC = rng.Next(-20, 55)
Summary = summaries.[rng.Next(summaries.Length)] } |]
json summaries next ctx
let appRouter =
router {
get "/" helloHandler
get "/weather" weatherHandler
}
let app = application { use_router appRouter }
run app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment