2023-02-08 14:33:22 +01:00
|
|
|
using System.Reflection;
|
|
|
|
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;
|
2023-02-10 17:46:44 +01:00
|
|
|
using Microsoft.AspNetCore.StaticFiles;
|
2023-02-08 14:33:22 +01:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Serilog;
|
|
|
|
using Serilog.Extensions.Logging;
|
|
|
|
using Throw;
|
2023-02-21 15:45:51 +01:00
|
|
|
using Shared.SerializerContexts;
|
2023-02-08 14:33:22 +01:00
|
|
|
|
|
|
|
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));
|
2023-02-08 14:33:22 +01:00
|
|
|
|
|
|
|
var serverIp = builder.Configuration["ServerIp"] ?? "127.0.0.1";
|
|
|
|
var certificateManager = new CertificateService(serverIp, new SerilogLoggerFactory(Log.Logger).CreateLogger(""));
|
|
|
|
builder.WebHost.ConfigureKestrel(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.AddContext<SourceGenerationContext>());
|
2023-02-09 10:25:42 +01:00
|
|
|
|
2023-02-08 14:33:22 +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);
|
2023-02-13 19:47:08 +01:00
|
|
|
|
|
|
|
builder.Services.AddResponseCompression(options =>
|
|
|
|
{
|
|
|
|
options.EnableForHttps = true;
|
|
|
|
});
|
2023-02-08 14:33:22 +01:00
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
2023-02-13 19:47:08 +01:00
|
|
|
app.UseResponseCompression();
|
|
|
|
|
2023-02-08 14:33:22 +01:00
|
|
|
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
|
|
|
|
2023-02-08 14:33:22 +01:00
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
{
|
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI();
|
2023-02-21 15:45:51 +01:00
|
|
|
app.UseWebAssemblyDebugging();
|
2023-02-08 14:33:22 +01:00
|
|
|
}
|
|
|
|
|
2023-02-10 17:46:44 +01:00
|
|
|
// 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
|
2023-02-13 19:47:08 +01:00
|
|
|
var contentTypeProvider = new FileExtensionContentTypeProvider();
|
|
|
|
contentTypeProvider.Mappings[".cmp"] = "text/plain";
|
|
|
|
contentTypeProvider.Mappings[".evt"] = "text/plain";
|
|
|
|
|
2023-02-17 18:29:20 +01:00
|
|
|
app.UseBlazorFrameworkFiles();
|
2023-02-14 19:14:19 +01:00
|
|
|
app.UseStaticFiles(new StaticFileOptions
|
|
|
|
{
|
|
|
|
ContentTypeProvider = contentTypeProvider
|
|
|
|
});
|
2023-02-09 10:25:42 +01:00
|
|
|
|
2023-02-08 14:33:22 +01:00
|
|
|
app.MapControllers();
|
2023-02-17 18:29:20 +01:00
|
|
|
app.MapFallbackToFile("index.html");
|
2023-02-08 14:33:22 +01:00
|
|
|
|
|
|
|
app.Run();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2023-02-09 10:25:42 +01:00
|
|
|
Log.Fatal(ex, "Unhandled exception in startup");
|
2023-02-08 14:33:22 +01:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
Log.Information("Shut down complete");
|
|
|
|
Log.CloseAndFlush();
|
|
|
|
}
|