continuity/Continuity.AuthServer/Program.cs

114 lines
4.7 KiB
C#
Raw Normal View History

2024-02-07 15:40:36 +00:00
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
2023-11-20 18:58:30 +00:00
using System.Net;
2023-08-09 14:23:41 +00:00
using System.Reflection;
2023-11-25 11:26:28 +00:00
using System.Text.Json;
2024-02-05 17:12:12 +00:00
using Continuity.AuthServer;
using Continuity.AuthServer.DB;
2024-02-05 17:12:12 +00:00
using Continuity.AuthServer.PacketHandlers;
using Continuity.AuthServer.Services;
2023-08-09 14:23:41 +00:00
using MassTransit;
2023-08-14 11:49:27 +00:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
2023-08-09 14:23:41 +00:00
using Microsoft.Extensions.DependencyInjection;
2023-11-20 13:23:04 +00:00
using Microsoft.Extensions.Diagnostics.HealthChecks;
2023-08-09 14:23:41 +00:00
using Microsoft.Extensions.Hosting;
2022-12-31 13:31:42 +00:00
using Microsoft.Extensions.Logging;
using Npgsql;
2023-11-20 13:23:04 +00:00
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
2024-04-04 14:57:42 +00:00
using RaiNote.PacketMediator;
2023-11-25 11:26:28 +00:00
using Wonderking.Game.Mapping;
using Wonderking.Packets;
2022-12-31 13:31:42 +00:00
2023-08-09 14:23:41 +00:00
var builder = Host.CreateApplicationBuilder();
#if DEBUG
builder.Environment.EnvironmentName = "Development";
#endif
2023-11-22 05:28:01 +00:00
builder.Services.AddMetrics();
builder.Configuration.AddJsonFile("settings.json", true, true)
2023-08-14 20:54:22 +00:00
.AddJsonFile($"settings.{builder.Environment.EnvironmentName}.json", true)
.AddEnvironmentVariables().Build();
2023-08-10 08:47:35 +00:00
builder.Services.AddLogging();
var loggerFactory = LoggerFactory.Create(loggingBuilder => {
loggingBuilder.AddFile("logs/Continuity.AuthServer-{Date}.log", LogLevel.Trace);
loggingBuilder.AddFile("logs/Continuity.AuthServer-{Date}.json.log", LogLevel.Trace, isJson: true);
2023-11-16 11:06:36 +00:00
loggingBuilder.AddConsole();
loggingBuilder.AddOpenTelemetry(logging => {
2024-09-16 19:36:50 +00:00
logging.AddOtlpExporter();
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});
2023-11-16 11:06:36 +00:00
});
2023-11-20 13:23:04 +00:00
var configuration = builder.Configuration;
builder.Services.AddOpenTelemetry()
.WithMetrics(metrics => {
metrics.AddRuntimeInstrumentation().AddProcessInstrumentation().AddMeter("Microsoft.AspNetCore.Hosting", nameof(Continuity.AuthServer))
.ConfigureResource(resourceBuilder => resourceBuilder.AddService("Continuity", serviceNamespace: "Wonderking", serviceVersion: "0.0.1"));
})
.WithTracing(tracing => {
if (builder.Environment.IsDevelopment()) {
tracing.SetSampler<AlwaysOnSampler>();
}
tracing.AddEntityFrameworkCoreInstrumentation(options => options.SetDbStatementForText = true).AddNpgsql();
2023-11-20 13:29:04 +00:00
});
builder.Services.AddMetrics();
builder.Services.Configure<OpenTelemetryLoggerOptions>(logging => logging.AddOtlpExporter());
builder.Services.ConfigureOpenTelemetryMeterProvider(meter => meter.AddOtlpExporter());
builder.Services.ConfigureOpenTelemetryTracerProvider(tracer => tracer.AddOtlpExporter());
2023-11-20 13:23:04 +00:00
builder.Services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
builder.Services.AddDbContextPool<WonderkingContext>(o => {
2023-11-16 11:06:36 +00:00
o.UseNpgsql(
$"Host={configuration["DB:Host"]};Username={configuration["DB:Username"]};Password={configuration["DB:Password"]};Database={configuration["DB:Database"]};Port={configuration["DB:Port"]}")
.EnableSensitiveDataLogging().UseLoggerFactory(loggerFactory);
2023-11-16 11:06:36 +00:00
});
2023-11-25 11:26:28 +00:00
builder.Services.AddSingleton<CharacterStatsMappingConfiguration>(
JsonSerializer.Deserialize<CharacterStatsMappingConfiguration>(
File.ReadAllText("config/character-stats.mapping.json")) ?? throw new InvalidOperationException());
2023-11-16 11:06:36 +00:00
builder.Services.AddSingleton<ILoggerFactory>(loggerFactory);
2024-02-05 17:12:12 +00:00
builder.Services.AddSingleton(provider =>
new PacketDistributorService<OperationCode, AuthSession>(
provider.GetRequiredService<IServiceProvider>(),
new List<Assembly> { Assembly.GetAssembly(typeof(OperationCode)) }.AsReadOnly(),
new List<Assembly> { Assembly.GetAssembly(typeof(LoginHandler)) }.AsReadOnly()
2024-02-05 17:12:12 +00:00
));
builder.Services.AddSingleton<ItemObjectPoolService>();
2024-02-05 17:12:12 +00:00
builder.Services.AddHostedService(provider => new WonderkingAuthServer(IPAddress.Any, 10001,
provider.GetService<ILogger<WonderkingAuthServer>>() ?? throw new InvalidOperationException(),
provider.GetService<IServiceProvider>() ?? throw new InvalidOperationException()));
builder.Services.AddHostedService(provider =>
provider.GetService<ItemObjectPoolService>() ?? throw new InvalidOperationException());
2024-02-05 17:12:12 +00:00
2023-08-14 19:30:32 +00:00
builder.Services.AddHostedService(provider =>
provider.GetService<PacketDistributorService<OperationCode, AuthSession>>() ??
throw new InvalidOperationException());
2024-02-05 17:12:12 +00:00
builder.Services.AddMassTransit(x => {
x.UsingInMemory((context, configurator) => configurator.ConfigureEndpoints(context));
x.AddMediator(cfg => cfg.AddConsumers(Assembly.GetExecutingAssembly()));
2022-12-31 13:31:42 +00:00
});
2023-08-11 09:31:30 +00:00
using var host = builder.Build();
2024-02-05 17:12:12 +00:00
await using (var scope = host.Services.CreateAsyncScope()) {
2023-08-14 11:49:27 +00:00
var db = scope.ServiceProvider.GetRequiredService<WonderkingContext>();
2023-11-21 20:36:05 +00:00
await db.Database.MigrateAsync();
2023-08-14 11:49:27 +00:00
}
2023-11-21 20:36:05 +00:00
await host.RunAsync();