-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.fs
More file actions
255 lines (213 loc) · 8.38 KB
/
Program.fs
File metadata and controls
255 lines (213 loc) · 8.38 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
open System
open System.IO
open System.Security.Claims
open System.Collections.Generic
open System.Threading
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Http
open Microsoft.AspNetCore.Http.Features
open Microsoft.AspNetCore.Authentication
open Microsoft.AspNetCore.Authentication.Cookies
open Microsoft.Extensions.Logging
open Microsoft.Extensions.DependencyInjection
open Giraffe.Tasks
open Giraffe.HttpContextExtensions
open Giraffe.HttpHandlers
open Giraffe.Middleware
open Dapper
// open Giraffe.Razor.HttpHandlers
// open Giraffe.Razor.Middleware
// open SampleApp.Models
// open SampleApp.HtmlViews
open datab
type OptionHandler<'T>() =
inherit SqlMapper.TypeHandler<option<'T>>()
override __.SetValue(param, value) =
let valueOrNull =
match value with
| Some x -> box x
| None -> null
param.Value <- valueOrNull
override __.Parse value =
if isNull value || value = box DBNull.Value
then None
else Some (value :?> 'T)
// ---------------------------------
// Error handler
// ---------------------------------
let errorHandler (ex : Exception) (logger : ILogger) =
logger.LogError(EventId(), ex, "An unhandled exception has occurred while executing the request.")
clearResponse >=> setStatusCode 500 >=> text ex.Message
// ---------------------------------
// Web app
// ---------------------------------
let authScheme = CookieAuthenticationDefaults.AuthenticationScheme
let accessDenied = setStatusCode 401 >=> text "Access Denied"
let mustBeUser = requiresAuthentication accessDenied
let mustBeAdmin =
requiresAuthentication accessDenied
>=> requiresRole "Admin" accessDenied
let loginHandler =
fun (next : HttpFunc) (ctx : HttpContext) ->
task {
let issuer = "http://localhost:5000"
let claims =
[
Claim(ClaimTypes.Name, "John", ClaimValueTypes.String, issuer)
Claim(ClaimTypes.Surname, "Doe", ClaimValueTypes.String, issuer)
Claim(ClaimTypes.Role, "Admin", ClaimValueTypes.String, issuer)
]
let identity = ClaimsIdentity(claims, authScheme)
let user = ClaimsPrincipal(identity)
do! ctx.SignInAsync(authScheme, user)
return! text "Successfully logged in" next ctx
}
let userHandler =
fun (next : HttpFunc) (ctx : HttpContext) ->
text ctx.User.Identity.Name next ctx
let newTokenHandler =
fun (next : HttpFunc) (ctx : HttpContext) ->
let token = datab.JwtGenerator.getToken "Admin" "Admin"
text token next ctx
let calcHandler =
fun (next : HttpFunc) (ctx : HttpContext) ->
task {
// let! user = datab.RepoUser.getUser connection "mxManager"
// return! json user next ctx
let repo = datab.RepoUser.getUser
use connection = datab.RepoBase.getConnection()
do! connection.OpenAsync ()
use (tx: Npgsql.NpgsqlTransaction) = connection.BeginTransaction ()
let! user = datab.UserService.getUser repo connection "mxManager"
tx.Commit ()
return! json user next ctx
}
let showUserHandler id =
mustBeAdmin >=>
text (sprintf "User ID: %i" id)
let time() = System.DateTime.Now.ToString()
[<CLIMutable>]
type Car =
{
Name : string
Make : string
Wheels : int
Built : DateTime
}
let submitCar =
fun (next : HttpFunc) (ctx : HttpContext) ->
task {
let! car = ctx.BindModel<Car>()
return! json car next ctx
}
let smallFileUploadHandler =
fun (next : HttpFunc) (ctx : HttpContext) ->
task {
return!
(match ctx.Request.HasFormContentType with
| false -> setStatusCode 400 >=> text "Bad request"
| true ->
ctx.Request.Form.Files
|> Seq.fold (fun acc file -> sprintf "%s\n%s" acc file.FileName) ""
|> text) next ctx
}
let largeFileUploadHandler =
fun (next : HttpFunc) (ctx : HttpContext) ->
task {
let formFeature = ctx.Features.Get<IFormFeature>()
let! form = formFeature.ReadFormAsync CancellationToken.None
return!
(form.Files
|> Seq.fold (fun acc file -> sprintf "%s\n%s" acc file.FileName) ""
|> text) next ctx
}
let webApp =
choose [
GET >=>
choose [
route "/" >=> text "index"
route "/ping" >=> text "pong"
route "/error" >=> (fun _ _ -> failwith "Something went wrong!")
route "/login" >=> loginHandler
route "/logout" >=> signOff authScheme >=> text "Successfully logged out."
route "/user" >=> mustBeUser >=> userHandler
route "/calc" >=> calcHandler
route "/token" >=> newTokenHandler
// routef "/user/%i" showUserHandler
// route "/razor" >=> razorHtmlView "Person" { Name = "Razor" }
// route "/razorHello" >=> razorHtmlView "Hello" ""
// route "/fileupload" >=> razorHtmlView "FileUpload" ""
// route "/person" >=> (personView { Name = "Html Node" } |> renderHtml)
// route "/once" >=> (time() |> text)
// route "/everytime" >=> warbler (fun _ -> (time() |> text))
]
POST >=>
choose [
route "/small-upload" >=> smallFileUploadHandler
route "/large-upload" >=> largeFileUploadHandler ]
route "/car" >=> submitCar
setStatusCode 404 >=> text "Not Found" ]
// ---------------------------------
// Main
// ---------------------------------
let cookieAuth (o : CookieAuthenticationOptions) =
do
o.Cookie.HttpOnly <- true
o.Cookie.SecurePolicy <- CookieSecurePolicy.SameAsRequest
o.SlidingExpiration <- true
o.ExpireTimeSpan <- TimeSpan.FromDays 7.0
let configureApp (app : IApplicationBuilder) =
app.UseGiraffeErrorHandler errorHandler
app.UseStaticFiles() |> ignore
app.UseAuthentication() |> ignore
app.UseGiraffe webApp
let configureServices (services : IServiceCollection) =
let sp = services.BuildServiceProvider()
let env = sp.GetService<IHostingEnvironment>()
let viewsFolderPath = Path.Combine(env.ContentRootPath, "Views")
services
.AddAuthentication(authScheme)
.AddCookie(cookieAuth)
|> ignore
services.AddDataProtection() |> ignore
// services.AddRazorEngine viewsFolderPath |> ignore
let configureLogging (loggerBuilder : ILoggingBuilder) =
loggerBuilder.AddFilter(fun lvl -> lvl.Equals LogLevel.Error)
.AddConsole()
.AddDebug() |> ignore
[<EntryPoint>]
let main argv =
do datab.RepoAdoBase.getData
// SqlMapper.AddTypeHandler (OptionHandler<string>())
// SqlMapper.AddTypeHandler (OptionHandler<int>())
// let contentRoot = Directory.GetCurrentDirectory()
// let webRoot = Path.Combine(contentRoot, "WebRoot")
// WebHostBuilder()
// .UseKestrel()
// .UseContentRoot(contentRoot)
// .UseWebRoot(webRoot)
// .Configure(Action<IApplicationBuilder> configureApp)
// .ConfigureServices(configureServices)
// .ConfigureLogging(configureLogging)
// .Build()
// .Run()
0
// open System
// open datab
// [<EntryPoint>]
// let main argv =
// let userList = UserService.getUser RepoUser.getUser "mxManager" |> Async.RunSynchronously
// userList |> printfn "%A"
// let testUser : Models.User = {
// Id = 3;
// Username = "xxx";
// Password = "tttt";
// Active = true;
// Role = "r";
// UserFacilityIdLista = Some "lll"
// }
// UserService.getUser (fun x -> async { return Seq.ofList [testUser]}) "mxManager"
// |> Async.RunSynchronously
// |> printfn "%A"
// 0