Skip to content

Instantly share code, notes, and snippets.

@Gastove
Created December 26, 2020 16:21
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 Gastove/f29b15be414815133730a709a4d920ba to your computer and use it in GitHub Desktop.
Save Gastove/f29b15be414815133730a709a4d920ba to your computer and use it in GitHub Desktop.
// This is what the Giraffe template produces
module giraffe.App
open System
open System.IO
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Cors.Infrastructure
open Microsoft.AspNetCore.Hosting
open Microsoft.Extensions.Hosting
open Microsoft.Extensions.Logging
open Microsoft.Extensions.DependencyInjection
open Giraffe
// ---------------------------------
// Models
// ---------------------------------
type Message =
{
Text : string
}
// ---------------------------------
// Views
// ---------------------------------
module Views =
open Giraffe.ViewEngine
let layout (content: XmlNode list) =
html [] [
head [] [
title [] [ encodedText "giraffe" ]
link [ _rel "stylesheet"
_type "text/css"
_href "/main.css" ]
]
body [] content
]
let partial () =
h1 [] [ encodedText "giraffe" ]
let index (model : Message) =
[
partial()
p [] [ encodedText model.Text ]
] |> layout
// ---------------------------------
// Web app
// ---------------------------------
let indexHandler (name : string) =
let greetings = sprintf "Hello %s, from Giraffe!" name
let model = { Text = greetings }
let view = Views.index model
htmlView view
let webApp =
choose [
GET >=>
choose [
route "/" >=> indexHandler "world"
routef "/hello/%s" indexHandler
]
setStatusCode 404 >=> text "Not Found" ]
// ---------------------------------
// Error handler
// ---------------------------------
let errorHandler (ex : Exception) (logger : ILogger) =
logger.LogError(ex, "An unhandled exception has occurred while executing the request.")
clearResponse >=> setStatusCode 500 >=> text ex.Message
// ---------------------------------
// Config and Main
// ---------------------------------
let configureCors (builder : CorsPolicyBuilder) =
builder
.WithOrigins(
"http://localhost:5000",
"https://localhost:5001")
.AllowAnyMethod()
.AllowAnyHeader()
|> ignore
let configureApp (app : IApplicationBuilder) =
let env = app.ApplicationServices.GetService<IWebHostEnvironment>()
(match env.IsDevelopment() with
| true ->
app.UseDeveloperExceptionPage()
| false ->
app .UseGiraffeErrorHandler(errorHandler)
.UseHttpsRedirection())
.UseCors(configureCors)
.UseStaticFiles()
.UseGiraffe(webApp)
let configureServices (services : IServiceCollection) =
services.AddCors() |> ignore
services.AddGiraffe() |> ignore
let configureLogging (builder : ILoggingBuilder) =
builder.AddConsole()
.AddDebug() |> ignore
[<EntryPoint>]
let main args =
let contentRoot = Directory.GetCurrentDirectory()
let webRoot = Path.Combine(contentRoot, "WebRoot")
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(
fun webHostBuilder ->
webHostBuilder
.UseContentRoot(contentRoot)
.UseWebRoot(webRoot)
.Configure(Action<IApplicationBuilder> configureApp)
.ConfigureServices(configureServices)
.ConfigureLogging(configureLogging)
|> ignore)
.Build()
.Run()
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment