continuity/Continuity.AuthServer/Program.cs

144 lines
5.4 KiB
C#
Raw Normal View History

2023-11-21 20:37:50 +00:00
// Copyright (c) 2023 Timothy Schenk. Subject to 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;
using Rai.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();
2023-11-16 11:06:36 +00:00
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();
});
2023-11-20 13:23:04 +00:00
var configuration = builder.Configuration;
if (configuration.GetValue<bool>("Tracing:Enabled"))
{
Action<ResourceBuilder> resourceBuilderAction = r => r
.AddService("Continuity", serviceInstanceId: Environment.MachineName);
2023-11-20 13:23:04 +00:00
builder.Services.AddOpenTelemetry()
.ConfigureResource(resourceBuilderAction)
.WithTracing(tracing =>
{
tracing.AddSource(nameof(Server));
2024-02-05 17:20:57 +00:00
tracing.AddSource("MassTransit");
tracing.AddEntityFrameworkCoreInstrumentation(options => options.SetDbStatementForText = true);
tracing.AddNpgsql();
})
.WithMetrics(metrics =>
{
metrics.AddRuntimeInstrumentation();
metrics.AddProcessInstrumentation();
});
2023-11-20 13:23:04 +00:00
builder.Logging.AddOpenTelemetry(logging =>
2023-11-20 13:23:04 +00:00
{
var resourceBuilder = ResourceBuilder.CreateDefault();
resourceBuilderAction(resourceBuilder);
logging.SetResourceBuilder(resourceBuilder);
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});
builder.Services.Configure<OpenTelemetryLoggerOptions>(logging =>
2023-11-20 13:29:04 +00:00
{
logging.AddOtlpExporter(options =>
{
options.Endpoint = new Uri(configuration["OTLP:Logging:Endpoint"] ?? string.Empty);
});
2023-11-20 13:29:04 +00:00
});
builder.Services.ConfigureOpenTelemetryMeterProvider(metrics =>
{
metrics.AddOtlpExporter(options =>
options.Endpoint = new Uri(configuration["OTLP:Metrics:Endpoint"] ?? string.Empty));
});
builder.Services.ConfigureOpenTelemetryTracerProvider(tracing =>
{
tracing.AddZipkinExporter(options =>
options.Endpoint = new Uri(configuration["Zipkin:Endpoint"] ?? string.Empty));
tracing.AddOtlpExporter(options =>
options.Endpoint = new Uri(configuration["OTLP:Tracing:Endpoint"] ?? string.Empty));
});
}
2023-11-20 13:23:04 +00:00
builder.Services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
2023-11-16 11:06:36 +00:00
builder.Services.AddDbContextPool<WonderkingContext>(o =>
{
o.UseNpgsql(
$"Host={configuration["DB:Host"]};Username={configuration["DB:Username"]};Password={configuration["DB:Password"]};Database={configuration["DB:Database"]};Port={configuration["DB:Port"]}")
.EnableSensitiveDataLogging().UseLazyLoadingProxies().UseLoggerFactory(loggerFactory);
});
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
2023-08-09 14:23:41 +00:00
builder.Services.AddMassTransit(x =>
2022-12-31 13:31:42 +00:00
{
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();