From f96de8936dd0ca095876f9c34fe2ea6312530112 Mon Sep 17 00:00:00 2001 From: masiton Date: Sat, 5 Jul 2025 08:46:30 +0200 Subject: [PATCH] Configuration for Broker. --- .../MessengerBroker.Configuration.csproj | 13 +++++ .../Model/BrokerConfiguration.cs | 51 +++++++++++++++++++ .../Model/Servers/Base/Server.cs | 9 ++++ .../Model/Servers/MasterServer.cs | 15 ++++++ .../Model/Servers/SlaveServer.cs | 11 ++++ ...vironmentPersistenceConfigurationParser.cs | 27 ++++++++++ .../Parsers/MasterServerParser.cs | 27 ++++++++++ .../Parsers/SlaveServerParser.cs | 26 ++++++++++ .../Constants.EnvironmentVariables.cs | 16 ++++++ 9 files changed, 195 insertions(+) create mode 100644 code/MessengerBroker.Configuration/MessengerBroker.Configuration.csproj create mode 100644 code/MessengerBroker.Configuration/Model/BrokerConfiguration.cs create mode 100644 code/MessengerBroker.Configuration/Model/Servers/Base/Server.cs create mode 100644 code/MessengerBroker.Configuration/Model/Servers/MasterServer.cs create mode 100644 code/MessengerBroker.Configuration/Model/Servers/SlaveServer.cs create mode 100644 code/MessengerBroker.Configuration/Parsers/ApiEnvironmentPersistenceConfigurationParser.cs create mode 100644 code/MessengerBroker.Configuration/Parsers/MasterServerParser.cs create mode 100644 code/MessengerBroker.Configuration/Parsers/SlaveServerParser.cs create mode 100644 code/MessengerBroker.Configuration/Sources/Environment/Constants.EnvironmentVariables.cs diff --git a/code/MessengerBroker.Configuration/MessengerBroker.Configuration.csproj b/code/MessengerBroker.Configuration/MessengerBroker.Configuration.csproj new file mode 100644 index 0000000..bbcaa09 --- /dev/null +++ b/code/MessengerBroker.Configuration/MessengerBroker.Configuration.csproj @@ -0,0 +1,13 @@ + + + + net9.0 + enable + disable + + + + + + + diff --git a/code/MessengerBroker.Configuration/Model/BrokerConfiguration.cs b/code/MessengerBroker.Configuration/Model/BrokerConfiguration.cs new file mode 100644 index 0000000..62c3f18 --- /dev/null +++ b/code/MessengerBroker.Configuration/Model/BrokerConfiguration.cs @@ -0,0 +1,51 @@ +using MessengerApi.Configuration.Model.Persistence.Base; +using MessengerApi.Configuration.Parsers; +using MessengerApi.Configuration.Sources.Environment; +using MessengerBroker.Configuration.Model.Servers; +using MessengerBroker.Configuration.Parsers; +using MessengerBroker.Configuration.Sources.Environment; + +namespace MessengerBroker.Configuration.Model +{ + public class BrokerConfiguration + { + public PersistenceConfiguration ApiPersistenceConfiguration { get; set; } + + public PersistenceConfiguration BrokerPersistenceConfiguration { get; set; } + + /// + /// List of proxies that are trusted to provide forwarding headers. + /// + public string[] Proxies { get; set; } + + public Guid BrokerId { get; set; } + + public MasterServer[] MasterServers { get; set; } + + public SlaveServer[] SlaveServers { get; set; } + + public BrokerConfiguration( + PersistenceConfiguration apiPersistenceConfiguration, + PersistenceConfiguration broPersistenceConfiguration, + Guid brokerId, + MasterServer[] masters, + SlaveServer[] slaves) + { + this.ApiPersistenceConfiguration = apiPersistenceConfiguration; + this.BrokerPersistenceConfiguration = broPersistenceConfiguration; + this.BrokerId = brokerId; + this.MasterServers = masters; + this.SlaveServers = slaves; + this.Proxies = []; + } + + public BrokerConfiguration(IEnvironmentConfigurationSource config):this( + ApiEnvironmentPersistenceConfigurationParser.Parse(config), + EnvironmentPersistenceConfigurationParser.Parse(config), + config.GetValue(Constants.EnvironmentVariables.BROKER_ID), + MasterServerParser.Parse(config.GetValue(Constants.EnvironmentVariables.MASTER_SERVERS)), + SlaveServerParser.Parse(config.GetValue(Constants.EnvironmentVariables.SLAVE_SERVERS))) + { + } + } +} \ No newline at end of file diff --git a/code/MessengerBroker.Configuration/Model/Servers/Base/Server.cs b/code/MessengerBroker.Configuration/Model/Servers/Base/Server.cs new file mode 100644 index 0000000..900a369 --- /dev/null +++ b/code/MessengerBroker.Configuration/Model/Servers/Base/Server.cs @@ -0,0 +1,9 @@ +namespace MessengerBroker.Configuration.Model.Servers.Base +{ + public class Server + { + public Guid BrokerId { get; set; } + + public string Name { get; set; } + } +} diff --git a/code/MessengerBroker.Configuration/Model/Servers/MasterServer.cs b/code/MessengerBroker.Configuration/Model/Servers/MasterServer.cs new file mode 100644 index 0000000..3cc650a --- /dev/null +++ b/code/MessengerBroker.Configuration/Model/Servers/MasterServer.cs @@ -0,0 +1,15 @@ +using MessengerBroker.Configuration.Model.Servers.Base; + +namespace MessengerBroker.Configuration.Model.Servers +{ + /// + /// Server that we have to pull data from and provide alternate service for. + /// + public class MasterServer : Server + { + /// + /// Url of the Broker API we use to download the data. + /// + public string BrokerUrl { get; set; } + } +} \ No newline at end of file diff --git a/code/MessengerBroker.Configuration/Model/Servers/SlaveServer.cs b/code/MessengerBroker.Configuration/Model/Servers/SlaveServer.cs new file mode 100644 index 0000000..4a80029 --- /dev/null +++ b/code/MessengerBroker.Configuration/Model/Servers/SlaveServer.cs @@ -0,0 +1,11 @@ +using MessengerBroker.Configuration.Model.Servers.Base; + +namespace MessengerBroker.Configuration.Model.Servers +{ + /// + /// Server that is pulling data from us and has to provide alternate service for us. + /// + public class SlaveServer : Server + { + } +} \ No newline at end of file diff --git a/code/MessengerBroker.Configuration/Parsers/ApiEnvironmentPersistenceConfigurationParser.cs b/code/MessengerBroker.Configuration/Parsers/ApiEnvironmentPersistenceConfigurationParser.cs new file mode 100644 index 0000000..95dcff6 --- /dev/null +++ b/code/MessengerBroker.Configuration/Parsers/ApiEnvironmentPersistenceConfigurationParser.cs @@ -0,0 +1,27 @@ +using MessengerApi.Configuration.Model.Persistence; +using MessengerApi.Configuration.Model.Persistence.Base; +using MessengerApi.Configuration.Parsers; +using MessengerApi.Configuration.Sources.Environment; +using MessengerBroker.Configuration.Sources.Environment; + +namespace MessengerBroker.Configuration.Parsers +{ + public class ApiEnvironmentPersistenceConfigurationParser + { + public static PersistenceConfiguration Parse(IEnvironmentConfigurationSource config) + { + var type = PersistenceTypeParser.Parse(config.GetValue(Constants.EnvironmentVariables.API_PERSISTENCE_TYPE)); + + if (type == MessengerApi.Configuration.Enums.PersistenceTypes.Sql) + { + return new SqlPersistenceConfiguration(config); + } + else if (type == MessengerApi.Configuration.Enums.PersistenceTypes.PostgreSql) + { + return new NpgPersistenceConfiguration(config); + } + + throw new InvalidOperationException("Unrecognized persistence type."); + } + } +} \ No newline at end of file diff --git a/code/MessengerBroker.Configuration/Parsers/MasterServerParser.cs b/code/MessengerBroker.Configuration/Parsers/MasterServerParser.cs new file mode 100644 index 0000000..d8044ca --- /dev/null +++ b/code/MessengerBroker.Configuration/Parsers/MasterServerParser.cs @@ -0,0 +1,27 @@ +using MessengerBroker.Configuration.Model.Servers; + +namespace MessengerBroker.Configuration.Parsers +{ + public class MasterServerParser + { + public static MasterServer[] Parse(string value) + { + var servers = new List(); + var values = value.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + + foreach(var v in values) + { + var props = v.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + + servers.Add(new MasterServer + { + BrokerId = Guid.Parse(props[0]), + BrokerUrl = props[1], + Name = props[2] + }); + } + + return servers.ToArray(); + } + } +} diff --git a/code/MessengerBroker.Configuration/Parsers/SlaveServerParser.cs b/code/MessengerBroker.Configuration/Parsers/SlaveServerParser.cs new file mode 100644 index 0000000..23761ba --- /dev/null +++ b/code/MessengerBroker.Configuration/Parsers/SlaveServerParser.cs @@ -0,0 +1,26 @@ +using MessengerBroker.Configuration.Model.Servers; + +namespace MessengerBroker.Configuration.Parsers +{ + public static class SlaveServerParser + { + public static SlaveServer[] Parse(string value) + { + var servers = new List(); + var values = value.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + + foreach (var v in values) + { + var props = v.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + + servers.Add(new SlaveServer + { + BrokerId = Guid.Parse(props[0]), + Name = props[1] + }); + } + + return servers.ToArray(); + } + } +} diff --git a/code/MessengerBroker.Configuration/Sources/Environment/Constants.EnvironmentVariables.cs b/code/MessengerBroker.Configuration/Sources/Environment/Constants.EnvironmentVariables.cs new file mode 100644 index 0000000..6d76f51 --- /dev/null +++ b/code/MessengerBroker.Configuration/Sources/Environment/Constants.EnvironmentVariables.cs @@ -0,0 +1,16 @@ + +namespace MessengerBroker.Configuration.Sources.Environment +{ + public class Constants + { + public static class EnvironmentVariables + { + public const string API_PERSISTENCE_TYPE = nameof(API_PERSISTENCE_TYPE); + public const string API_SQL_CONNECTIONSTRING = nameof(API_SQL_CONNECTIONSTRING); + public const string API_NPG_CONNECTIONSTRING = nameof(API_NPG_CONNECTIONSTRING); + public const string BROKER_ID = nameof(BROKER_ID); + public const string MASTER_SERVERS = nameof(MASTER_SERVERS); + public const string SLAVE_SERVERS = nameof(SLAVE_SERVERS); + } + } +}