From 7739cc2f3dfadbe22ff75de357def84aa871892f Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 11 Jul 2024 10:29:46 -0400 Subject: [PATCH] Use Dependency Injection in Bts.DataStreamWebApp.Server --- .../Configurations/DevServer.json | 108 ++++++++++++++++ .../Bts.DataStreamWebApp.Server/Program.cs | 117 ++++++++---------- 2 files changed, 161 insertions(+), 64 deletions(-) diff --git a/src/DataStreamSuite/Bts.DataStreamWebApp.Server/Configurations/DevServer.json b/src/DataStreamSuite/Bts.DataStreamWebApp.Server/Configurations/DevServer.json index 2c63c0851..a1ab80754 100644 --- a/src/DataStreamSuite/Bts.DataStreamWebApp.Server/Configurations/DevServer.json +++ b/src/DataStreamSuite/Bts.DataStreamWebApp.Server/Configurations/DevServer.json @@ -1,2 +1,110 @@ { + "MasterWorker": { + "Interval": "00:00:00.1" + }, + "ViewStreamDatabase": { + "Interval": "00:00:00.1" + }, + "Spark": { + "Config": { + "ConfigServer": "http://localhost:8000/config" + }, + "Credentials": { + "User": "bts", + "Password": "" + }, + "EnableAdmin": true, + "EnableOrders": true, + "EnablePosition": true + }, + "SparkCredentials": { + "User": "bts", + "Password": "" + }, + "PubSub": { + "MillisecondsBetweenSubInfoUpdates": 1000, + "MillisecondsBetweenTimeSliceUpdates": 5000, + "MillisecondsBetweenNoBufferSpaceRetries": 100, + "RetriesOnNoBufferSpace": 1, + "DoItAllSecondsBetweenLogs": 0, + "HeartbeatInterval": 15000, + "MissingHeartbeatsBeforeGc": 50, + "UseManagedSockets": true, + "StateUpdatePeriod": 5000, + "StatisticsUpdatePeriod": 5000, + "DisposedConnectionCheckPeriod": 1000, + "MessageProcessingWarningThreshold": 1000, + "ServerPort": 12345, + "Routing": { + "PublisherRoutes": [ + { + "Name": "default", + "ServerAddress": "bts-cloud://127.0.0.1:12345", + "TagRegExp": ".*" + } + ], + "SubscriberRoutes": [ + { + "Name": "default", + "ServerAddress": "bts-cloud://127.0.0.1:12345", + "TagRegExp": ".*" + } + ] + }, + "MaxPacketSize": 500000000, + "BlueCommMustDie": true, + "CrushableTags": [], + "TagTypeToAssemblyMap": { + "Entries": [ + { + "HandlerClass": "BCG.PubSubServerTagHandler", + "Assembly": "DataStreamCore.dll" + } + ] + }, + "ServerQueue": "PubSubQueue", + "PortRange": { + "First": 30400, + "Last": 31700 + }, + "BindToRandomPort": false, + "DebugTags": "^NOTAGS$", + "LatencyReportThresholdMs": 1500, + "UseSubstations": false, + "UseDynamicClients": false, + "IsSubstation": false, + "InternallyPublishSubInfo": true, + "UseManagedClientsInNativeCode": false, + "CompressedTags": "^NOTAGS$", + "TagsToReportCompressionRatio": "^NOTAGS$", + "DisableMsmq": false, + "UsePrepublishedServerState": false, + "IpTranslation": null, + "GcActivationThreshold": 0, + "WaitPeriodAfterGc": 1000, + "TagHandlerFactory": { + "Handlers": [ + { + "TagType": "DataStream", + "Assembly": "DataStreamCore", + "Class": "bts.PubsubServerStreamTagHandler" + }, + { + "TagType": "System_Sub", + "Assembly": "PubSub", + "Class": "BCG.SystemSubscriptionMapPublisher" + }, + { + "TagType": "System_Stats", + "Assembly": "PubSub", + "Class": "BCG.SystemStatsPublisher" + }, + { + "TagType": ".*", + "Assembly": "DataStreamCore", + "Class": "bts.PubsubServerGenericDataHandler" + } + ] + } + } } diff --git a/src/DataStreamSuite/Bts.DataStreamWebApp.Server/Program.cs b/src/DataStreamSuite/Bts.DataStreamWebApp.Server/Program.cs index 5157c51fa..26e6332ae 100644 --- a/src/DataStreamSuite/Bts.DataStreamWebApp.Server/Program.cs +++ b/src/DataStreamSuite/Bts.DataStreamWebApp.Server/Program.cs @@ -1,73 +1,62 @@ -using BCG; +using Bts.DataStreamCore.Services; +using Bts.PubSub.Services; -using bts.api.core; - -using Bts.DataStreamWebApp.Server; -using Bts.PubSub; - -using CommandLine; - -using mbts.config; +using Spark.Api.Services; +using Spark.Core.Services; using System.Reactive.Linq; -var parser = new Parser(ps => { - ps.CaseInsensitiveEnumValues = true; - ps.HelpWriter = Console.Out; +var builder = WebApplication.CreateBuilder(new WebApplicationOptions { + Args = args, + ApplicationName = "Bts.DataStreamWebApp.Server", }); +ConfigureApp(builder.Configuration); +ConfigureServices(builder.Services, builder.Configuration); -await parser.ParseArguments(args). - MapResult(Run, errors => Task.FromResult(1)); +var app = builder.Build(); +if (app.Environment.IsDevelopment()) { + app.UseDirectoryBrowser(); +} +var wsOpts = new WebSocketOptions { + KeepAliveInterval = TimeSpan.FromMinutes(2) +}; +app.UseWebSockets(wsOpts); +app.UseDefaultFiles(); +app.UseStaticFiles(); +if (app.Environment.IsDevelopment()) { + app.UseSwagger(); + app.UseSwaggerUI(); +} +app.UseHttpsRedirection(); +app.UseAuthorization(); +app.MapControllers(); +try { + Console.Write($"Starting Application"); + app.Run(); + Console.Write($"Application exited gracefully."); +} catch (Exception ex) { + Console.Error.Write($"Application crashed: {ex}"); + return 1; +} +return 0; -async Task Run(DataStreamWebAppServerConfig opts) { - Configuration.initialize(); - PubSubConfig.SetGlobalConfig(PubSubConfig.Default()); - using var context = ServiceContext.create(opts.Sparkd); - await context.login(opts.User ?? "bts", opts.Password ?? string.Empty); - var vsWorkLoop = Observable.Interval(TimeSpan.FromMilliseconds(200)).Subscribe(_ => { - ViewStreamDatabase.instance.doWork(); - MasterWorker.getInstance(false).doWork(); - }); - var builder = WebApplication.CreateBuilder(new WebApplicationOptions { - Args = args, - ApplicationName = "Bts.DataStreamWebApp.Server", - }); - if (opts.WebRootPath is not null) { - builder.WebHost.UseWebRoot(opts.WebRootPath); +static void ConfigureApp(ConfigurationManager configBuilder) { + configBuilder.AddCommandLine(Environment.GetCommandLineArgs()); + var cfgRoot = configBuilder.GetSection("config"); + if (cfgRoot?.Value is not null) { + configBuilder.AddJsonFile(cfgRoot.Value); } - if (opts.ContentRootPath is not null) { - builder.WebHost.UseContentRoot(opts.ContentRootPath); - } - if (opts.EnvironmentName is not null) { - builder.WebHost.UseEnvironment(opts.EnvironmentName); - } - builder.Services.AddDirectoryBrowser(); - builder.Services.AddControllers(); - builder.Services.AddEndpointsApiExplorer(); - builder.Services.AddSwaggerGen(); - var app = builder.Build(); - if (app.Environment.IsDevelopment()) { - app.UseDirectoryBrowser(); - } - var wsOpts = new WebSocketOptions { - KeepAliveInterval = TimeSpan.FromMinutes(2) - }; - app.UseWebSockets(wsOpts); - app.UseDefaultFiles(); - app.UseStaticFiles(); - if (app.Environment.IsDevelopment()) { - app.UseSwagger(); - app.UseSwaggerUI(); - } - app.UseHttpsRedirection(); - app.UseAuthorization(); - app.MapControllers(); - try { - app.Run(); - } catch (Exception ex) { - Console.WriteLine($"Exception on DataStreamWebsocketServer thread: {ex}"); - return 1; - } - Spark.Core.Disposables.Disposable.dispose(ref vsWorkLoop); - return 0; +} + +static void ConfigureServices(IServiceCollection services, IConfiguration config) { + services.AddDirectoryBrowser(); + services.AddControllers(); + services.AddEndpointsApiExplorer(); + services.AddSwaggerGen(); + services.AddMasterWorker(config.GetSection("MasterWorker")); + services.AddViewStreamDatabase(config.GetSection("ViewStreamDatabase")); + services.AddPubSubClient(config.GetSection("PubSub")); + // services.AddPrimitiveManager(); + services.AddSpark(config.GetSection("Spark")); + services.AddSparkd(config.GetSection("SparkdCredentials")); }