87 lines
2.9 KiB
C#
87 lines
2.9 KiB
C#
using BCG;
|
|
|
|
using bts;
|
|
|
|
using Bts.DataStreamCore.Services;
|
|
using Bts.Extensions.Hosting;
|
|
using Bts.PubSub.Services;
|
|
|
|
using CommandLine;
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Spark.Api.Server;
|
|
using Spark.Api.MarketData;
|
|
using Spark.Api.Services;
|
|
using Spark.Core;
|
|
using Spark.Core.Services;
|
|
using Spark.WebSocket.Services;
|
|
|
|
using System;
|
|
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) {
|
|
const string appName = "spark_api_server";
|
|
var builder = WebApplication.CreateBuilder(new WebApplicationOptions {
|
|
Args = Environment.GetCommandLineArgs(),
|
|
ApplicationName = appName,
|
|
});
|
|
HostBuilderExtensions.ConfigureApp(builder.Configuration, builder.Environment.EnvironmentName, appName);
|
|
ConfigureServices(builder.Services, builder.Configuration);
|
|
var app = builder.Build();
|
|
var mdClient = app.Services.GetService<IMarketDataClient>();
|
|
if (mdClient is not null) {
|
|
PrimitiveManager.SetMarketDataClient(mdClient);
|
|
}
|
|
app.UseWebSockets(app.Configuration.GetValue<WebSocketOptions>("WebSocketOptions") ?? new() { KeepAliveInterval = TimeSpan.FromMinutes(2) });
|
|
var gateway = new Lazy<ISparkApiWebSocketServer>(app.Services.GetRequiredService<ISparkApiWebSocketServer>);
|
|
app.UseWebSockets();
|
|
|
|
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);
|
|
|
|
async Task HandleRequest(HttpContext context){
|
|
if (!context.WebSockets.IsWebSocketRequest)
|
|
{
|
|
context.Response.StatusCode = StatusCodes.Status400BadRequest;
|
|
await context.Response.WriteAsync("WebSocket endpoint");
|
|
}
|
|
|
|
await gateway.Value.HandleRequestAsync(context);
|
|
}
|
|
}
|
|
|
|
static void ConfigureServices(IServiceCollection services, IConfiguration config) {
|
|
services.Configure<WebSocketOptions>(config.GetSection("WebSocketOptions"))
|
|
.AddLogging(bd => HostBuilderExtensions.ConfigureLogging(bd, new SparkLoggerProvider()))
|
|
.AddSpark(config.GetSection("Spark"))
|
|
.AddSparkApi(config.GetSection("SparkApi"))
|
|
.AddPubSubClient(config.GetSection("PubSub"))
|
|
.AddViewStreamDatabase()
|
|
.AddMasterWorker(config.GetSection("MasterWorker"))
|
|
.AddWebSocketServer();
|
|
}
|
|
|
|
[Flags]
|
|
enum Errors {
|
|
None = 0,
|
|
Listen = 1,
|
|
// Add errors as needed
|
|
}
|