Created
January 5, 2021 13:58
-
-
Save ianrussellsoftwarepark/8b02b1e07c65e956d921eac882d08f2b to your computer and use it in GitHub Desktop.
This file contains 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
open Microsoft.AspNetCore.Builder | |
open Microsoft.AspNetCore.Hosting | |
open Microsoft.Extensions.Hosting | |
open Microsoft.Extensions.DependencyInjection | |
open Microsoft.AspNetCore.Http | |
open Giraffe | |
open Giraffe.iewEngine | |
open FSharp.Control.Tasks | |
type PingModel = { | |
Response: string | |
} | |
let indexView = | |
html [] [ | |
head [] [ | |
title [] [ str "Giraffe Sample" ] | |
] | |
body [] [ | |
h1 [] [ str "Welcome to F#" ] | |
p [ _class "some-css-class"; _id "someId" ] [ | |
str "Hello World" | |
] | |
] | |
] | |
let sayHelloNameHandler (name:string) = | |
fun (next : HttpFunc) (ctx : HttpContext) -> | |
task { | |
let msg = sprintf "Hello, %s" name | |
return! json { Response = msg } next ctx | |
} | |
let webApp = | |
choose [ | |
GET >=> choose [ | |
route "/" >=> htmlView indexView | |
subRoute "/api" | |
(choose [ | |
route "" >=> json { Response = "Hello world!!" } | |
routef "/%s" sayHelloNameHandler | |
]) | |
] | |
setStatusCode 404 >=> text "Not Found" | |
] | |
let configureApp (app : IApplicationBuilder) = | |
app.UseGiraffe webApp | |
let configureServices (services : IServiceCollection) = | |
services.AddGiraffe() |> ignore | |
[<EntryPoint>] | |
let main _ = | |
Host.CreateDefaultBuilder() | |
.ConfigureWebHostDefaults(fun webHost -> | |
webHost | |
.Configure(configureApp) | |
.ConfigureServices(configureServices) | |
|> ignore) | |
.Build() | |
.Run() | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello! I read your Introduction to Web Programming in F# with Giraffe (Part 2) and find typos in this program. Don't understand how to throw an issue so will write here. Hope it will be helpful!