Skip to content

Commit 79311eb

Browse files
authored
Merge pull request #4 from jameson2011/dev
Dev
2 parents 70cb885 + fc775b7 commit 79311eb

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

src/zKbProxy/CommandLine.fs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ module CommandLine=
2020
let bufferSizeArg = "buffer"
2121
let noCacheArg = "nocache"
2222
let sessionTimeoutArg = "sessiontimeout"
23+
let configArg = "config"
2324

2425
let private longestArg =
2526
[ killSourceUriArg; dbServerArg; dbNameArg; dbKillsCollectionArg; dbSessionCollectionArg;
2627
dbUserArg; dbPasswordArg; webPortArg; bufferSizeArg; noCacheArg;
27-
sessionTimeoutArg; ]
28+
sessionTimeoutArg; configArg; ]
2829
|> Seq.map String.length
2930
|> Seq.max
3031

@@ -84,6 +85,9 @@ module CommandLine=
8485
app.Description <- "A proxy for zKb's RedisQ service"
8586
app
8687

88+
let addConfigFileArg = addSingleOption configArg configArg "The configuration file"
89+
let getConfigFileValue app = getStringOption configArg app
90+
8791
let addKillSourceUriArg = addSingleOption killSourceUriArg "killsource" "The URI providing zKB kills. By default this is zKB RedisQ"
8892
let getKillSourceUriValue app = getStringOption killSourceUriArg app
8993

@@ -148,6 +152,7 @@ module CommandLine=
148152
>> addLiveBufferSizeArg
149153
>> addNoCacheArg
150154
>> addSessionTimeoutArg
155+
>> addConfigFileArg
151156
>> setAction cmd
152157
app.Command("run", (composeAppPipe f)) |> ignore
153158
app

src/zKbProxy/Program.fs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,45 @@ open System.Threading
99

1010
module Program=
1111

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)=
1351
{ Configuration.empty with
1452
KillSourceUri = CommandLine.getKillSourceUriValue app
1553
|> Option.defaultValue ConfigurationDefaults.KillSourceUri
@@ -32,6 +70,12 @@ module Program=
3270
NoCache = CommandLine.getNoCacheValue app;
3371
SessionTimeout = CommandLine.getSessionTimeoutArg app;
3472
}
73+
74+
let private configFromStartApp(app: CommandLine.App)=
75+
app
76+
|> CommandLine.getConfigFileValue
77+
|> Option.map configFromFile
78+
|> Option.defaultWith (fun () -> configFromCmdLine app)
3579

3680
let private validateConfig (config: Configuration) =
3781
let validateString value name =

src/zKbProxy/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"zKbProxy": {
44
"commandName": "Project",
5-
"commandLineArgs": "run --server localhost -db zKbRedisqProxy -killscol zkbKills --buffer 200000"
5+
"commandLineArgs": "run --config ..\\..\\..\\..\\..\\..\\..\\..\\Projects\\Docs\\zkbproxy\\zkbproxy.json"
66
}
77
}
88
}

0 commit comments

Comments
 (0)