Changed line endings for Startup

This commit is contained in:
Tom 2024-10-29 12:14:55 +00:00
parent aac73563d0
commit 61151bef0c

View File

@ -1,161 +1,161 @@
using System.Net; using System.Net;
using System.Text.Json; using System.Text.Json;
using HermesSocketLibrary; using HermesSocketLibrary;
using HermesSocketLibrary.db; using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests; using HermesSocketLibrary.Requests;
using HermesSocketServer; using HermesSocketServer;
using HermesSocketServer.Requests; using HermesSocketServer.Requests;
using HermesSocketServer.Socket; using HermesSocketServer.Socket;
using HermesSocketServer.Socket.Handlers; using HermesSocketServer.Socket.Handlers;
using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.HttpOverrides;
using Serilog; using Serilog;
using Serilog.Events; using Serilog.Events;
using YamlDotNet.Serialization; using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions; using YamlDotNet.Serialization.NamingConventions;
using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.Connections;
using HermesSocketServer.Validators; using HermesSocketServer.Validators;
using HermesSocketServer.Store; using HermesSocketServer.Store;
using HermesSocketServer.Services; using HermesSocketServer.Services;
var yamlDeserializer = new DeserializerBuilder() var yamlDeserializer = new DeserializerBuilder()
.WithNamingConvention(HyphenatedNamingConvention.Instance) .WithNamingConvention(HyphenatedNamingConvention.Instance)
.Build(); .Build();
var configFileName = "server.config.yml"; var configFileName = "server.config.yml";
var environment = Environment.GetEnvironmentVariable("TTS_ENV")!.ToLower(); var environment = Environment.GetEnvironmentVariable("TTS_ENV")!.ToLower();
if (File.Exists("server.config." + environment + ".yml")) if (File.Exists("server.config." + environment + ".yml"))
configFileName = "server.config." + environment + ".yml"; configFileName = "server.config." + environment + ".yml";
var configContent = File.ReadAllText(configFileName); var configContent = File.ReadAllText(configFileName);
var configuration = yamlDeserializer.Deserialize<ServerConfiguration>(configContent); var configuration = yamlDeserializer.Deserialize<ServerConfiguration>(configContent);
if (configuration.Environment.ToUpper() != "QA" && configuration.Environment.ToUpper() != "PROD") if (configuration.Environment.ToUpper() != "QA" && configuration.Environment.ToUpper() != "PROD")
throw new Exception("Invalid environment set."); throw new Exception("Invalid environment set.");
var builder = WebApplication.CreateBuilder(); var builder = WebApplication.CreateBuilder();
builder.Logging.ClearProviders(); builder.Logging.ClearProviders();
builder.Services.Configure<ForwardedHeadersOptions>(options => builder.Services.Configure<ForwardedHeadersOptions>(options =>
{ {
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
}); });
builder.WebHost.UseUrls($"http://{configuration.WebsocketServer.Host}:{configuration.WebsocketServer.Port}"); builder.WebHost.UseUrls($"http://{configuration.WebsocketServer.Host}:{configuration.WebsocketServer.Port}");
var loggerConfiguration = new LoggerConfiguration(); var loggerConfiguration = new LoggerConfiguration();
if (configuration.Environment.ToUpper() == "QA") if (configuration.Environment.ToUpper() == "QA")
loggerConfiguration.MinimumLevel.Verbose(); loggerConfiguration.MinimumLevel.Verbose();
else else
loggerConfiguration.MinimumLevel.Debug(); loggerConfiguration.MinimumLevel.Debug();
loggerConfiguration.Enrich.FromLogContext() loggerConfiguration.Enrich.FromLogContext()
.WriteTo.File($"logs/{configuration.Environment.ToUpper()}/serverlog-.log", rollingInterval: RollingInterval.Day, retainedFileCountLimit: 7); .WriteTo.File($"logs/{configuration.Environment.ToUpper()}/serverlog-.log", rollingInterval: RollingInterval.Day, retainedFileCountLimit: 7);
if (configuration.Environment.ToUpper() == "QA") if (configuration.Environment.ToUpper() == "QA")
loggerConfiguration.WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Debug); loggerConfiguration.WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Debug);
else else
loggerConfiguration.WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Information); loggerConfiguration.WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Information);
var logger = loggerConfiguration.CreateLogger(); var logger = loggerConfiguration.CreateLogger();
builder.Host.UseSerilog(logger); builder.Host.UseSerilog(logger);
builder.Logging.AddSerilog(logger); builder.Logging.AddSerilog(logger);
var s = builder.Services; var s = builder.Services;
s.AddSerilog(logger); s.AddSerilog(logger);
s.AddSingleton<ServerConfiguration>(configuration); s.AddSingleton<ServerConfiguration>(configuration);
s.AddSingleton<Database>(); s.AddSingleton<Database>();
// Socket message handlers // Socket message handlers
s.AddSingleton<Serilog.ILogger>(logger); s.AddSingleton<Serilog.ILogger>(logger);
s.AddSingleton<ISocketHandler, HeartbeatHandler>(); s.AddSingleton<ISocketHandler, HeartbeatHandler>();
s.AddSingleton<ISocketHandler, HermesLoginHandler>(); s.AddSingleton<ISocketHandler, HermesLoginHandler>();
s.AddSingleton<ISocketHandler, RequestHandler>(); s.AddSingleton<ISocketHandler, RequestHandler>();
s.AddSingleton<ISocketHandler, LoggingHandler>(); s.AddSingleton<ISocketHandler, LoggingHandler>();
s.AddSingleton<ISocketHandler, ChatterHandler>(); s.AddSingleton<ISocketHandler, ChatterHandler>();
s.AddSingleton<ISocketHandler, EmoteDetailsHandler>(); s.AddSingleton<ISocketHandler, EmoteDetailsHandler>();
s.AddSingleton<ISocketHandler, EmoteUsageHandler>(); s.AddSingleton<ISocketHandler, EmoteUsageHandler>();
// Validators // Validators
s.AddSingleton<VoiceIdValidator>(); s.AddSingleton<VoiceIdValidator>();
s.AddSingleton<VoiceNameValidator>(); s.AddSingleton<VoiceNameValidator>();
// Stores // Stores
s.AddSingleton<VoiceStore>(); s.AddSingleton<VoiceStore>();
s.AddSingleton<UserStore>(); s.AddSingleton<UserStore>();
// Request handlers // Request handlers
s.AddSingleton<IRequest, CreatePolicy>(); s.AddSingleton<IRequest, CreatePolicy>();
s.AddSingleton<IRequest, CreateTTSUser>(); s.AddSingleton<IRequest, CreateTTSUser>();
s.AddSingleton<IRequest, CreateTTSVoice>(); s.AddSingleton<IRequest, CreateTTSVoice>();
s.AddSingleton<IRequest, DeletePolicy>(); s.AddSingleton<IRequest, DeletePolicy>();
s.AddSingleton<IRequest, DeleteTTSVoice>(); s.AddSingleton<IRequest, DeleteTTSVoice>();
s.AddSingleton<IRequest, GetChatterIds>(); s.AddSingleton<IRequest, GetChatterIds>();
s.AddSingleton<IRequest, GetConnections>(); s.AddSingleton<IRequest, GetConnections>();
s.AddSingleton<IRequest, GetDefaultTTSVoice>(); s.AddSingleton<IRequest, GetDefaultTTSVoice>();
s.AddSingleton<IRequest, GetEmotes>(); s.AddSingleton<IRequest, GetEmotes>();
s.AddSingleton<IRequest, GetEnabledTTSVoices>(); s.AddSingleton<IRequest, GetEnabledTTSVoices>();
s.AddSingleton<IRequest, GetPermissions>(); s.AddSingleton<IRequest, GetPermissions>();
s.AddSingleton<IRequest, GetRedemptions>(); s.AddSingleton<IRequest, GetRedemptions>();
s.AddSingleton<IRequest, GetRedeemableActions>(); s.AddSingleton<IRequest, GetRedeemableActions>();
s.AddSingleton<IRequest, GetPolicies>(); s.AddSingleton<IRequest, GetPolicies>();
s.AddSingleton<IRequest, GetTTSUsers>(); s.AddSingleton<IRequest, GetTTSUsers>();
s.AddSingleton<IRequest, GetTTSVoices>(); s.AddSingleton<IRequest, GetTTSVoices>();
s.AddSingleton<IRequest, GetTTSWordFilters>(); s.AddSingleton<IRequest, GetTTSWordFilters>();
s.AddSingleton<IRequest, UpdateTTSUser>(); s.AddSingleton<IRequest, UpdateTTSUser>();
s.AddSingleton<IRequest, UpdateTTSVoice>(); s.AddSingleton<IRequest, UpdateTTSVoice>();
s.AddSingleton<IRequest, UpdateDefaultTTSVoice>(); s.AddSingleton<IRequest, UpdateDefaultTTSVoice>();
s.AddSingleton<IRequest, UpdateTTSVoiceState>(); s.AddSingleton<IRequest, UpdateTTSVoiceState>();
s.AddSingleton<IRequest, UpdatePolicy>(); s.AddSingleton<IRequest, UpdatePolicy>();
// Managers // Managers
s.AddSingleton<ChannelManager>(); s.AddSingleton<ChannelManager>();
s.AddSingleton<HermesSocketManager>(); s.AddSingleton<HermesSocketManager>();
s.AddSingleton<SocketHandlerManager>(); s.AddSingleton<SocketHandlerManager>();
s.AddSingleton<IRequestManager, RequestManager>(); s.AddSingleton<IRequestManager, RequestManager>();
s.AddSingleton(new JsonSerializerOptions() s.AddSingleton(new JsonSerializerOptions()
{ {
PropertyNameCaseInsensitive = false, PropertyNameCaseInsensitive = false,
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
}); });
s.AddSingleton<Server>(); s.AddSingleton<Server>();
// Background services // Background services
s.AddHostedService<DatabaseService>(); s.AddHostedService<DatabaseService>();
var app = builder.Build(); var app = builder.Build();
app.UseForwardedHeaders(); app.UseForwardedHeaders();
app.UseSerilogRequestLogging(); app.UseSerilogRequestLogging();
var wsOptions = new WebSocketOptions() var wsOptions = new WebSocketOptions()
{ {
KeepAliveInterval = TimeSpan.FromSeconds(30) KeepAliveInterval = TimeSpan.FromSeconds(30)
}; };
// wsOptions.AllowedOrigins.Add("wss://tomtospeech.com"); // wsOptions.AllowedOrigins.Add("wss://tomtospeech.com");
//wsOptions.AllowedOrigins.Add("ws.tomtospeech.com"); //wsOptions.AllowedOrigins.Add("ws.tomtospeech.com");
//wsOptions.AllowedOrigins.Add("hermes-ws.goblincaves.com"); //wsOptions.AllowedOrigins.Add("hermes-ws.goblincaves.com");
app.UseWebSockets(wsOptions); app.UseWebSockets(wsOptions);
var options = app.Services.GetRequiredService<JsonSerializerOptions>(); var options = app.Services.GetRequiredService<JsonSerializerOptions>();
var server = app.Services.GetRequiredService<Server>(); var server = app.Services.GetRequiredService<Server>();
app.Use(async (HttpContext context, RequestDelegate next) => app.Use(async (HttpContext context, RequestDelegate next) =>
{ {
if (context.Request.Path != "/") if (context.Request.Path != "/")
{ {
context.Response.StatusCode = StatusCodes.Status401Unauthorized; context.Response.StatusCode = StatusCodes.Status401Unauthorized;
return; return;
} }
if (context.WebSockets.IsWebSocketRequest) if (context.WebSockets.IsWebSocketRequest)
{ {
using var webSocket = await context.WebSockets.AcceptWebSocketAsync(); using var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await server.Handle(new WebSocketUser(webSocket, IPAddress.Parse(context.Request.Headers["X-Forwarded-For"].ToString()), options, logger), context); await server.Handle(new WebSocketUser(webSocket, IPAddress.Parse(context.Request.Headers["X-Forwarded-For"].ToString()), options, logger), context);
} }
else else
{ {
context.Response.StatusCode = StatusCodes.Status400BadRequest; context.Response.StatusCode = StatusCodes.Status400BadRequest;
} }
await next(context); await next(context);
}); });
await app.RunAsync(); await app.RunAsync();