106 lines
3.5 KiB
C#
106 lines
3.5 KiB
C#
using bts;
|
|
using bts.websocket_core;
|
|
|
|
using CommandLine;
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Spark.Api.Server;
|
|
using Spark.Api.Services;
|
|
using Spark.Core;
|
|
using Spark.Core.Services;
|
|
using Spark.WebSocket.Services;
|
|
|
|
using spark_api_server;
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Threading.Tasks;
|
|
|
|
var res = Parser.Default.ParseArguments<RunOptions, DumpSecuritiesOptions, DumpBooksOptions>(args);
|
|
await res.MapResult<RunOptions, DumpSecuritiesOptions, DumpBooksOptions, Task<int>>(
|
|
Run, SecurityDumper.run, BookDumper.run, errs => Task.FromResult(1));
|
|
|
|
static Task<int> Run(RunOptions opts) {
|
|
var builder = WebApplication.CreateBuilder(new WebApplicationOptions {
|
|
Args = Environment.GetCommandLineArgs(),
|
|
ApplicationName = "spark_api_server",
|
|
});
|
|
ConfigureApp(builder.Configuration, builder.Environment.EnvironmentName);
|
|
ConfigureServices(builder.Services, builder.Configuration);
|
|
var app = builder.Build();
|
|
app.UseWebSockets(app.Configuration.GetValue<WebSocketOptions>("WebSocketOptions") ?? new() { KeepAliveInterval = TimeSpan.FromMinutes(2) });
|
|
var gateway = new Lazy<IWebsocketConnectionGateway>(app.Services.GetRequiredService<IWebsocketConnectionGateway>);
|
|
app.Map("/", HandleRequest);
|
|
var errors = Errors.None;
|
|
Log.Api.info("Starting websocket server...");
|
|
try {
|
|
app.Run();
|
|
Log.Api.info("Websocket server stopped without error");
|
|
} catch (Exception ex) {
|
|
Log.Api.error($"Websocket server threw ex: {ex}");
|
|
errors |= Errors.Listen;
|
|
}
|
|
return Task.FromResult((int)errors);
|
|
|
|
Task HandleRequest(HttpContext context) =>
|
|
gateway.Value.HandleRequest(context);
|
|
}
|
|
|
|
static void ConfigureApp(ConfigurationManager configBuilder, string environment) {
|
|
// We prefix appsettings with the exe name because multiple exes will usually be in the build dir
|
|
// (this way, each exe can still load its own appsettings files from the current directory).
|
|
var exeName = Assembly.GetCallingAssembly().GetName().Name;
|
|
AddJsonFile($"{exeName}.appsettings.json");
|
|
AddJsonFile($"{exeName}.appsettings.{environment}.json");
|
|
|
|
var cfg = configBuilder.GetSection("config")?.Value;
|
|
if (!SetBasePath(cfg)) {
|
|
AddJsonFile(cfg);
|
|
}
|
|
|
|
bool SetBasePath(string? path) {
|
|
if (!Directory.Exists(path)) {
|
|
return false;
|
|
}
|
|
configBuilder.SetBasePath(path);
|
|
return true;
|
|
}
|
|
|
|
bool AddJsonFile(string? path) {
|
|
if (!File.Exists(path)) {
|
|
return false;
|
|
}
|
|
configBuilder.AddJsonFile(path);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
static void ConfigureServices(IServiceCollection services, IConfiguration config) {
|
|
services.Configure<WebSocketOptions>(config.GetSection("WebSocket"))
|
|
.AddLogging(ConfigureLogging)
|
|
.AddSpark(config.GetSection("Spark"))
|
|
.AddSparkApi(config.GetSection("SparkApi"))
|
|
.AddWebSocketServer();
|
|
|
|
static void ConfigureLogging(ILoggingBuilder loggingBuilder) =>
|
|
loggingBuilder.AddFilter("Microsoft", Microsoft.Extensions.Logging.LogLevel.Warning)
|
|
.AddFilter("System", Microsoft.Extensions.Logging.LogLevel.Warning)
|
|
.AddFilter("NToastNotify", Microsoft.Extensions.Logging.LogLevel.Warning)
|
|
.AddProvider(new SparkLoggerProvider())
|
|
.AddConsole()
|
|
.AddDebug();
|
|
}
|
|
|
|
[Flags]
|
|
enum Errors {
|
|
None = 0,
|
|
Listen = 1,
|
|
// Add errors as needed
|
|
}
|