-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.fs
125 lines (106 loc) · 3.49 KB
/
Program.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
module FsStartup.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 "FsStartup" ]
link [ _rel "stylesheet"
_type "text/css"
_href "/main.css" ]
]
body [] content
]
let partial () =
h1 [] [ encodedText "FsStartup" ]
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