Use Dependency Injection in Bts.DataStreamWebApp.Server

This commit is contained in:
John Colvin committed 2024-07-11 10:37:29 -04:00
1 parent 8347a7c126
commit 7739cc2f3d
2 files changed
+161 -64

No files matched your search

@@ -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<DataStreamWebAppServerConfig>(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<int> 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"));
}