68 lines
1.9 KiB
C#
68 lines
1.9 KiB
C#
namespace Bts.PubSubServer;
|
|
|
|
using Bts.PubSub;
|
|
|
|
using CommandLine;
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
public class PubSubServerConfig {
|
|
/// <summary>
|
|
/// The config server url for sparkd
|
|
/// </summary>
|
|
[JsonPropertyName("sparkd")]
|
|
[Option("sparkd", HelpText = "The sparkd config server")]
|
|
public string? Sparkd { get; set; }
|
|
|
|
[JsonPropertyName("simple_echo_server")]
|
|
[Option("simple-echo-server", HelpText = "Run a simple echo server (won't connect to sparkd)")]
|
|
public bool? SimpleEchoServer { get; set; }
|
|
|
|
[JsonPropertyName("pubsub")]
|
|
public PubSubConfig? PubSub { get; set; }
|
|
|
|
[JsonIgnore]
|
|
[Option(
|
|
"config",
|
|
MetaValue = "PATH",
|
|
HelpText = "Path to json file representing these options (PubSubServerConfig)")]
|
|
public string? ConfigFile { get; set; }
|
|
|
|
public static PubSubServerConfig Default() =>
|
|
new() {
|
|
Sparkd = "http://localhost:8000/config",
|
|
SimpleEchoServer = false,
|
|
PubSub = PubSubConfig.Default(),
|
|
ConfigFile = null,
|
|
};
|
|
|
|
public void MergeWith(PubSubServerConfig other) {
|
|
Sparkd ??= other.Sparkd;
|
|
SimpleEchoServer ??= other.SimpleEchoServer;
|
|
PubSub ??= other.PubSub;
|
|
ConfigFile ??= other.ConfigFile;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Merges options with those defined in `ConfigFile` (does not overwrite
|
|
/// non-null existing values).
|
|
/// </summary>
|
|
/// <exception cref="InvalidOperationException">
|
|
/// JsonSerializer.Deserialize returns null
|
|
/// </exception>
|
|
public void MergeWithConfigFile() {
|
|
if (ConfigFile is null) {
|
|
return;
|
|
}
|
|
var cfgString = File.ReadAllText(ConfigFile);
|
|
var parsedCfg = JsonSerializer.Deserialize<PubSubServerConfig>(cfgString) ??
|
|
throw new InvalidOperationException($"Failed to parse json from {ConfigFile}");
|
|
MergeWith(parsedCfg);
|
|
ConfigFile = null;
|
|
}
|
|
}
|
|
|