1
0
mirror of synced 2024-11-24 06:20:12 +01:00
GC-local-server-rewrite/MainServer/Program.cs

137 lines
4.9 KiB
C#
Raw Normal View History

using System.Reflection;
using System.Security.Authentication;
using Application;
using Application.Interfaces;
using Domain.Config;
using Infrastructure;
using Infrastructure.Common;
using Infrastructure.Persistence;
2023-02-09 10:25:42 +01:00
using MainServer.Filters;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.EntityFrameworkCore;
using Serilog;
using Serilog.Extensions.Logging;
using Throw;
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();
var version = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?
.InformationalVersion;
Log.Information("GCLocalServer version {Version}", version);
Log.Information("Server starting up...");
try
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
const string configurationsDirectory = "Configurations";
builder.Configuration
.AddJsonFile($"{configurationsDirectory}/database.json", optional: false, reloadOnChange: false)
.AddJsonFile($"{configurationsDirectory}/game.json", optional: false, reloadOnChange: false)
.AddJsonFile($"{configurationsDirectory}/logging.json", optional: false, reloadOnChange: false)
.AddJsonFile($"{configurationsDirectory}/events.json", optional: true, reloadOnChange: false)
.AddJsonFile($"{configurationsDirectory}/matching.json", optional: true, reloadOnChange: false)
.AddJsonFile($"{configurationsDirectory}/server.json", optional: true, reloadOnChange: false);
builder.Services.Configure<EventConfig>(
builder.Configuration.GetSection(EventConfig.EVENT_SECTION));
builder.Services.Configure<RelayConfig>(
builder.Configuration.GetSection(RelayConfig.RELAY_SECTION));
2023-02-09 10:25:42 +01:00
builder.Services.Configure<GameConfig>(
builder.Configuration.GetSection(GameConfig.GAME_SECTION));
var serverIp = builder.Configuration["ServerIp"] ?? "127.0.0.1";
var certificateManager = new CertificateService(serverIp, new SerilogLoggerFactory(Log.Logger).CreateLogger(""));
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
builder.WebHost.UseKestrel(options =>
options.ConfigureHttpsDefaults(adapterOptions =>
{
adapterOptions.ServerCertificate = certificateManager.InitializeCertificate();
}));
}
builder.Host.UseSerilog((context, configuration) =>
{
configuration.WriteTo.Console().ReadFrom.Configuration(context.Configuration);
});
2023-02-09 10:25:42 +01:00
builder.Services.AddControllers(options =>
2023-02-21 15:45:51 +01:00
options.Filters.Add<ApiExceptionFilterService>())
.AddJsonOptions(options => options.JsonSerializerOptions.WriteIndented = true);
2023-02-09 10:25:42 +01:00
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddApplication();
builder.Services.AddInfrastructure(builder.Configuration);
builder.Services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
});
var app = builder.Build();
app.UseResponseCompression();
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<CardDbContext>();
db.Database.Migrate();
}
app.UseSerilogRequestLogging(options =>
{
options.MessageTemplate = "HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms, " +
"request host: {RequestHost}";
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
{
diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
};
});
var eventService = app.Services.GetService<IEventManagerService>();
eventService.ThrowIfNull();
eventService.InitializeEvents();
2023-02-09 10:25:42 +01:00
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
2023-02-21 15:45:51 +01:00
app.UseWebAssemblyDebugging();
}
// Add content type for .cmp and .evt files as static files with unknown file extensions return 404 by default
// See https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-7.0#fileextensioncontenttypeprovider
// ReSharper disable once UseObjectOrCollectionInitializer
var contentTypeProvider = new FileExtensionContentTypeProvider();
contentTypeProvider.Mappings[".cmp"] = "text/plain";
contentTypeProvider.Mappings[".evt"] = "text/plain";
2023-02-17 18:29:20 +01:00
app.UseBlazorFrameworkFiles();
app.UseStaticFiles(new StaticFileOptions
{
ContentTypeProvider = contentTypeProvider
});
2023-02-09 10:25:42 +01:00
app.MapControllers();
2023-02-17 18:29:20 +01:00
app.MapFallbackToFile("index.html");
app.Run();
}
catch (Exception ex)
{
2023-02-09 10:25:42 +01:00
Log.Fatal(ex, "Unhandled exception in startup");
}
finally
{
Log.Information("Shut down complete");
Log.CloseAndFlush();
}