-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.fs
117 lines (97 loc) · 3.54 KB
/
main.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
#if INTERACTIVE
#load ".paket/load/main.group.fsx"
#else
module Main
#endif
open FSharp.Data.Sql
let [<Literal>] ResPath = __SOURCE_DIRECTORY__ + "/lib"
type Sql = SqlDataProvider<
DatabaseVendor = Common.DatabaseProviderTypes.MYSQL,
ConnectionString ="Server=localhost;Database=data;User=sqluser;Password=sqluserpwd",
ResolutionPath = ResPath,
IndividualsAmount=1000,
UseOptionTypes = true
>
Common.QueryEvents.SqlQueryEvent |> Event.add (printfn "Executing SQL: %O")
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Http
open Microsoft.Extensions.Logging
open Microsoft.Extensions.Configuration
open Giraffe
open FSharp.Control.Tasks.V2.ContextInsensitive
let connString =
System.Environment.GetEnvironmentVariable("MYSQL_CONNSTRING")
|> Option.ofObj
|> Option.defaultValue "Server=localhost;Database=data;User=root;Password=Hello"
type [<CLIMutable>] Person =
{
Name : string
}
module Person =
let create name = { Name=name }
module Sql =
module Person =
let add (person:Person) =
let ctx = Sql.GetDataContext(connString)
ctx.Data.Person.Create(Name=Some person.Name) |> ignore
do ctx.SubmitUpdates()
let list () =
let ctx = Sql.GetDataContext(connString)
ctx.Data.Person
|> Seq.choose(fun p -> p.Name |> Option.map Person.create)
|> Seq.toArray
let tryGetByName name =
let ctx = Sql.GetDataContext(connString)
ctx.Data.Person
|> Seq.tryFind (fun p -> p.Name = Some name)
|> Option.bind (fun p -> p.Name)
|> Option.map Person.create
module Handler =
module Person =
let add : HttpHandler =
fun (next : HttpFunc) (ctx : HttpContext) ->
task {
let! person = ctx.BindJsonAsync<Person>()
Sql.Person.add person
return! Successful.NO_CONTENT next ctx
}
let get name =
fun (next : HttpFunc) (ctx : HttpContext) ->
task {
match Sql.Person.tryGetByName name with
| Some p -> return! Successful.ok (json p) next ctx
| None -> return! RequestErrors.NOT_FOUND (sprintf "%s not found" name) next ctx
}
let list =
fun next ctx ->
task {
let l = Sql.Person.list ()
return! Successful.OK l next ctx
}
let webApp =
choose [
GET >=> route "/ping" >=> text "pong"
subRoute "/person" (choose [
GET >=> choose [
route "s" >=> Handler.Person.list
routef "/%s" Handler.Person.get
]
POST >=> Handler.Person.add
])
]
[<EntryPoint>]
let main args =
let config = ConfigurationBuilder().AddCommandLine(args).Build();
WebHostBuilder()
.UseKestrel()
.Configure(fun app ->
printfn "Add Giraffe to the ASP.NET Core pipeline"
app.UseGiraffe webApp)
.ConfigureServices(fun services ->
printfn "Register default Giraffe dependencies"
services.AddGiraffe() |> ignore)
.UseConfiguration(config)
.ConfigureLogging(fun logging -> logging.AddConsole() |> ignore)
.Build()
.Run()
0