-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.fs
216 lines (184 loc) · 8.08 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
module giraffe_test_a.App
open System
open System.IO
open System.Diagnostics
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Cors.Infrastructure
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Http
open Microsoft.Extensions.Hosting
open Microsoft.Extensions.Logging
open Microsoft.Extensions.DependencyInjection
open Giraffe
// ---------------------------------
// Models
// ---------------------------------
type Message = { Text : string }
type ErrorViewModel = { RequestId : string; ShowRequestId : bool }
// ---------------------------------
// Views
// ---------------------------------
module Views =
open Giraffe.ViewEngine
let layout (title_data: string) (content: XmlNode list) =
html [] [
head [] [
title [] [
encodedText title_data
encodedText " - WebApplicationCs"
]
link [ _rel "stylesheet"; _type "text/css"; _href "/lib/bootstrap/css/bootstrap.min.css" ]
link [ _rel "stylesheet"; _type "text/css"; _href "/main.css" ]
]
body [] [
header [] [
nav [ _class "navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3" ] [
div [ _class "container" ] [
a [ _class "navbar-brand"; _href "/" ] [ encodedText "WebApplicationCs" ]
button [
_class "navbar-toggler"
_type "button"
attr "data-toggle" "collapse"
attr "data-target" ".navbar-collapse"
attr "aria-controls" "navbarSupportedContent"
attr "aria-expanded" "false"
attr "aria-label" "Toggle navigation"
] [
span [ _class "navbar-toggler-icon" ] []
]
div [ _class "navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse" ] [
ul [ _class "navbar-nav flex-grow-1" ] [
li [ _class "nav-link dark-text" ] [ a [ _href "/" ] [ encodedText "Home" ] ]
li [ _class "nav-link dark-text" ] [ a [ _href "/Home/Privacy" ] [ encodedText "Privacy"] ]
]
]
]
]
]
div [ _class "container" ] [ main [ attr "role" "main"; _class "pb-3" ] content ]
footer [ _class "border-top footer text-muted" ] [
div [ _class "container" ] [
rawText "© "
encodedText "2020 - WebApplicationCs - "
a [ _href "/Home/Privacy" ] [ encodedText "Privacy" ]
]
]
script [ _src "/lib/jquery/jquery.min.js" ] []
script [ _src "/lib/bootstrap/js/bootstrap.bundle.min.js" ] []
]
]
let index =
[
div [ _class "text-center" ] [
h1 [ _class "display-4" ] [ encodedText "Welcome" ]
p [] [
encodedText "Learn about "
a [ _href "https://docs.microsoft.com/aspnet/core" ] [
encodedText "building Web apps with ASP.NET Core"
]
]
]
] |> layout "Home Page"
let privacy =
let title = "Privacy Policy"
[
h1 [] [ encodedText title ]
p [] [ encodedText "Use this page to detail your site's privacy policy." ]
] |> layout title
let error (model : ErrorViewModel) =
[
h1 [ _class "text-danger" ] [ encodedText "Error." ]
h2 [ _class "text-danger" ] [ encodedText "An error occurred while processing your request." ]
if model.ShowRequestId then p [] [
strong [] [ encodedText "Request ID:"; ]
code [] [ encodedText model.RequestId ]
]
h3 [] [ encodedText "Development Mode" ]
p [] [
encodedText "Swapping to "
strong [] [ encodedText "Development" ]
encodedText " environment will display more detailed information about the error that occurred."
]
p [] [
rawText """
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
"""
]
] |> layout "Error"
// ---------------------------------
// Web app
// ---------------------------------
let indexHandler (name : string) =
htmlView Views.index
let error_handler =
fun (next : HttpFunc) (ctx : HttpContext) ->
htmlView (Views.error {
RequestId =
match Activity.Current with
| null -> ctx.TraceIdentifier
| activity_current -> activity_current.Id
ShowRequestId = true
}) next ctx
let webApp =
choose [
GET >=>
choose [
route "/" >=> htmlView Views.index
routef "/hello/%s" indexHandler
route "/Home/Privacy" >=> htmlView Views.privacy
route "/Home/Error" >=> error_handler
route "/error" >=> (fun _ _ -> failwith "234")
]
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 >=> error_handler
// ---------------------------------
// Config and Main
// ---------------------------------
let configureCors (builder : CorsPolicyBuilder) =
builder.WithOrigins("http://localhost:8080")
.AllowAnyMethod()
.AllowAnyHeader()
|> ignore
let configureApp (app : IApplicationBuilder) =
let env = app.ApplicationServices.GetService<IWebHostEnvironment>()
(match env.EnvironmentName with
| "Development" -> app.UseDeveloperExceptionPage()
| "Alt" -> app.UseExceptionHandler("/Home/Error")
| _ -> app.UseGiraffeErrorHandler(errorHandler))
.UseHttpsRedirection()
.UseCors(configureCors)
.UseStaticFiles()
.UseGiraffe(webApp)
let configureServices (services : IServiceCollection) =
services.AddCors() |> ignore
services.AddGiraffe() |> ignore
let configureLogging (builder : ILoggingBuilder) =
builder.AddFilter(fun l -> l.Equals LogLevel.Error)
.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