-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.fs
More file actions
97 lines (77 loc) · 2.77 KB
/
Main.fs
File metadata and controls
97 lines (77 loc) · 2.77 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
module jook.Main
open Falco
open Falco.Routing
open Falco.HostBuilder
open jook.Data
open Microsoft.Extensions.Configuration
open Microsoft.Extensions.Logging
open System
open System.Linq
open System.Data
open Microsoft.Data.SqlClient
open System.Text.Json
open Microsoft.AspNetCore.Cors.Infrastructure
let env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
let config = configuration [||] {
required_json "appsettings.json"
optional_json $"appsettings.{env}.json"
}
let corsPolicyName = "local"
let corsPolicy (policyBuilder: CorsPolicyBuilder) =
// Note: This is a very lax setting, but a good fit for local development
policyBuilder
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
// Note: The URLs must not end with a /
.WithOrigins("https://localhost:5001",
"http://localhost:5173")
|> ignore
let corsOptions (options : CorsOptions) =
options.AddPolicy(corsPolicyName, corsPolicy)
let jsonOptions = JsonSerializerOptions()
jsonOptions.PropertyNamingPolicy <- JsonNamingPolicy.CamelCase
let connectionString = config.GetValue<string>("Settings:ConnectionString")
type ApplicationLogger = class end
let getDatabaseConnection () : IDbConnection =
new SqlConnection(connectionString) :> IDbConnection
let withServicesGet handler : HttpHandler = fun ctx ->
let logger = ctx.GetService<ILogger<ApplicationLogger>>()
let data = Request.getQuery ctx
handler logger getDatabaseConnection data ctx
let withServicesPost handler : HttpHandler = fun ctx ->
let logger = ctx.GetService<ILogger<ApplicationLogger>>()
task {
let! body = Request.getJsonOptions jsonOptions ctx
return handler logger getDatabaseConnection body ctx
}
let get route handler =
get route (withServicesGet handler)
let post route handler =
post route (withServicesPost handler)
[<EntryPoint>]
let main args =
webHost args {
use_default_files
use_static_files
use_cors corsPolicyName corsOptions
endpoints [
get "/tracks" (fun _ cf q ->
let title = q.TryGet ("title")
let artist = q.TryGet ("artist")
let album = q.TryGet ("album")
let genre = q.TryGet ("genre")
let tracks = trackList cf title artist album genre
let meta = {|
count = tracks.Count()
|}
let data = {|
meta = meta
tracks = tracks |}
Response.ofJsonOptions jsonOptions data)
post "/playlist" (fun _ cf body ->
// TODO: Persist the playlist tracks and title etc
Response.ofJsonOptions jsonOptions body)
]
}
0