100 lines
2.7 KiB
C#
100 lines
2.7 KiB
C#
namespace Bts.DataStreamPublisher;
|
|
|
|
using Bts.PubSub;
|
|
|
|
using CommandLine;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
public class DataStreamPublisherConfig
|
|
{
|
|
/// <summary>
|
|
/// The login user for sparkd
|
|
/// </summary>
|
|
[JsonPropertyName("user")]
|
|
[Option("user", HelpText = "The sparkd login user")]
|
|
public string? User { get; set; }
|
|
|
|
/// <summary>
|
|
/// The login password for sparkd
|
|
/// </summary>
|
|
[JsonPropertyName("password")]
|
|
[Option("pass", HelpText = "The sparkd login password")]
|
|
public string? Password { get; set; }
|
|
|
|
/// <summary>
|
|
/// The config server url for sparkd
|
|
/// </summary>
|
|
[JsonPropertyName("sparkd")]
|
|
[Option("sparkd", HelpText = "The sparkd config server")]
|
|
public string? Sparkd { get; set; }
|
|
|
|
/// <summary>
|
|
/// The tables to publish on startup. Each key/value represents a table name/query to publish.
|
|
/// </summary>
|
|
[JsonPropertyName("publishers")]
|
|
public List<PublisherNode>? Publishers { get; set; }
|
|
|
|
[JsonPropertyName("mongodb_url")]
|
|
public string MONGO_DB_URL { get; set; }
|
|
|
|
[JsonPropertyName("mongodb_db_name")]
|
|
public string MONGO_DB_NAME { get; set; }
|
|
|
|
[JsonPropertyName("mongodb_db_collection")]
|
|
public string MONGO_DB_COLLECTION { get; set; }
|
|
|
|
[JsonPropertyName("benzinga_news_ttl")]
|
|
public string BENZINGA_NEWS_TTL { get; set; }
|
|
|
|
[JsonPropertyName("pubsub")]
|
|
public PubSubConfig? PubSub { get; set; }
|
|
|
|
[Option(
|
|
"config",
|
|
MetaValue = "PATH",
|
|
HelpText = "Path to json file representing these options (DataStreamPublisherConfig)")]
|
|
[JsonIgnore]
|
|
public string? ConfigFile { get; set; }
|
|
|
|
public static DataStreamPublisherConfig Default() =>
|
|
new()
|
|
{
|
|
PubSub = PubSubConfig.Default(),
|
|
};
|
|
|
|
public void MergeWith(DataStreamPublisherConfig other)
|
|
{
|
|
User ??= other.User;
|
|
Password ??= other.Password;
|
|
Sparkd ??= other.Sparkd;
|
|
Publishers ??= other.Publishers;
|
|
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<DataStreamPublisherConfig>(cfgString) ??
|
|
throw new InvalidOperationException($"Failed to parse json from {ConfigFile}");
|
|
MergeWith(parsedCfg);
|
|
ConfigFile = null;
|
|
}
|
|
}
|