Files
spark/src/DataStreamSuite/Bts.DataStreamWebApp.Server/Program.cs
T
John Colvin 1531a57bc5 feat: Add DataStream WebApp
Included in DataStreamSuite.sln, but not DataStreamQueryAnalyzer.sln
2024-07-09 15:14:29 -04:00

74 lines
2.2 KiB
C#

using BCG;
using bts.api.core;
using Bts.DataStreamWebApp.Server;
using Bts.PubSub;
using CommandLine;
using mbts.config;
using System.Reactive.Linq;
var parser = new Parser(ps => {
ps.CaseInsensitiveEnumValues = true;
ps.HelpWriter = Console.Out;
});
await parser.ParseArguments<DataStreamWebAppServerConfig>(args).
MapResult(Run, errors => Task.FromResult(1));
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);
}
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;
}