Skip to content

Instantly share code, notes, and snippets.

@ianrussellsoftwarepark
Created January 5, 2021 13:58
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 ianrussellsoftwarepark/8b02b1e07c65e956d921eac882d08f2b to your computer and use it in GitHub Desktop.
Save ianrussellsoftwarepark/8b02b1e07c65e956d921eac882d08f2b to your computer and use it in GitHub Desktop.
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
@kleo-53
Copy link

kleo-53 commented May 15, 2023

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!

  1. line 7: open Giraffe.ViewEngine (missing V)
  2. line 8: open FSharp.Control (VS Code says that namespace 'Tasks' is not defined, if I remove word Task, everything works correctly)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment