@@ -9,7 +9,45 @@ open System.Threading
9
9
10
10
module Program =
11
11
12
- let private configFromStartApp ( app : CommandLine.App )=
12
+ open Newtonsoft.Json .Linq
13
+
14
+ let jsonToConfig ( j : JObject ) =
15
+ let toJValue ( token : JToken ) = token :?> JValue
16
+
17
+ let toBool = Option.ofNull >> Option.map ( toJValue >> ( fun value -> value.ToObject< bool>()))
18
+ let toInt32 = Option.ofNull >> Option.map ( toJValue >> ( fun value -> value.ToObject< int32>()))
19
+ let toUint16 = Option.ofNull >> Option.map ( toJValue >> ( fun value -> value.ToObject< uint16>()))
20
+ let toString = Option.ofNull >> Option.map ( toJValue >> ( fun value -> value.ToObject< string>()))
21
+ let toTimeSpan = Option.ofNull >> Option.map ( toJValue >> ( fun value -> value.ToObject< TimeSpan>()))
22
+ let defaultConfig = Configuration.empty
23
+
24
+ { Configuration.KillSourceUri = j.[ " killSourceUri" ] |> toString |> Option.defaultValue defaultConfig.KillSourceUri;
25
+ NoCache = j.[ " noCache" ] |> toBool |> Option.defaultValue defaultConfig.NoCache;
26
+ MongoServer = j.[ " mongoServer" ] |> toString |> Option.defaultValue defaultConfig.MongoServer;
27
+ DbName = j.[ " dbName" ] |> toString |> Option.defaultValue defaultConfig.DbName;
28
+ KillsDbCollection = j.[ " killsDbCollection" ] |> toString |> Option.defaultValue defaultConfig.KillsDbCollection;
29
+ SessionsDbCollection = j.[ " sessionsDbCollection" ] |> toString |> Option.defaultValue defaultConfig.SessionsDbCollection;
30
+ MongoUserName = j.[ " mongoUserName" ] |> toString |> Option.defaultValue defaultConfig.MongoUserName;
31
+ MongoPassword = j.[ " mongoPassword" ] |> toString |> Option.defaultValue defaultConfig.MongoPassword;
32
+ WebServerPort = j.[ " webServerPort" ] |> toUint16 |> Option.defaultValue defaultConfig.WebServerPort;
33
+ BufferSize = j.[ " bufferSize" ] |> toInt32 |> Option.defaultValue defaultConfig.BufferSize;
34
+ SessionTimeout = j.[ " sessionTimeout" ] |> toTimeSpan |> Option.defaultValue defaultConfig.SessionTimeout;
35
+ }
36
+
37
+
38
+
39
+ let private configFromFile ( filePath : string ) =
40
+
41
+ if not <| System.IO.File.Exists( filePath) then
42
+ let msg = filePath |> sprintf " The configuration file %s was not found."
43
+ raise ( System.IO.FileNotFoundException( msg))
44
+
45
+ filePath
46
+ |> System.IO.File.ReadAllText
47
+ |> JObject.Parse
48
+ |> jsonToConfig
49
+
50
+ let private configFromCmdLine ( app : CommandLine.App )=
13
51
{ Configuration.empty with
14
52
KillSourceUri = CommandLine.getKillSourceUriValue app
15
53
|> Option.defaultValue ConfigurationDefaults.KillSourceUri
@@ -32,6 +70,12 @@ module Program=
32
70
NoCache = CommandLine.getNoCacheValue app;
33
71
SessionTimeout = CommandLine.getSessionTimeoutArg app;
34
72
}
73
+
74
+ let private configFromStartApp ( app : CommandLine.App )=
75
+ app
76
+ |> CommandLine.getConfigFileValue
77
+ |> Option.map configFromFile
78
+ |> Option.defaultWith ( fun () -> configFromCmdLine app)
35
79
36
80
let private validateConfig ( config : Configuration ) =
37
81
let validateString value name =
0 commit comments