// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License. using System.Net; using System.Reflection; using System.Text.Json; using Continuity.AuthServer.DB; using Continuity.AuthServer.Services; using MassTransit; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Npgsql; using OpenTelemetry.Logs; using OpenTelemetry.Metrics; using OpenTelemetry.Resources; using OpenTelemetry.Trace; using Wonderking.Game.Mapping; var builder = Host.CreateApplicationBuilder(); #if DEBUG builder.Environment.EnvironmentName = "Development"; #endif builder.Services.AddMetrics(); builder.Configuration.AddJsonFile("settings.json", true, true) .AddJsonFile($"settings.{builder.Environment.EnvironmentName}.json", true) .AddEnvironmentVariables().Build(); 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); loggingBuilder.AddConsole(); }); var configuration = builder.Configuration; if (configuration.GetValue("Tracing:Enabled")) { Action resourceBuilderAction = r => r .AddService("Continuity", serviceInstanceId: Environment.MachineName); builder.Services.AddOpenTelemetry() .ConfigureResource(resourceBuilderAction) .WithTracing(tracing => { tracing.AddSource(nameof(Server)); //tracing.AddSource("MassTransit"); tracing.AddEntityFrameworkCoreInstrumentation(options => options.SetDbStatementForText = true); tracing.AddNpgsql(); }) .WithMetrics(metrics => { metrics.AddRuntimeInstrumentation(); metrics.AddProcessInstrumentation(); }); builder.Logging.AddOpenTelemetry(logging => { var resourceBuilder = ResourceBuilder.CreateDefault(); resourceBuilderAction(resourceBuilder); logging.SetResourceBuilder(resourceBuilder); logging.IncludeFormattedMessage = true; logging.IncludeScopes = true; }); builder.Services.Configure(logging => { logging.AddOtlpExporter(options => { options.Endpoint = new Uri(configuration["OTLP:Logging:Endpoint"] ?? string.Empty); }); }); 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)); }); } builder.Services.AddHealthChecks() .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); builder.Services.AddDbContextPool(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); }); builder.Services.AddSingleton( JsonSerializer.Deserialize( File.ReadAllText("config/character-stats.mapping.json")) ?? throw new InvalidOperationException()); builder.Services.AddSingleton(loggerFactory); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddHostedService(provider => provider.GetService() ?? throw new InvalidOperationException()); builder.Services.AddHostedService(provider => provider.GetService() ?? throw new InvalidOperationException()); builder.Services.AddMassTransit(x => { x.UsingInMemory((context, configurator) => configurator.ConfigureEndpoints(context)); x.AddMediator(cfg => cfg.AddConsumers(Assembly.GetExecutingAssembly())); }); builder.Services.AddHostedService(provider => new WonderkingAuthServer(IPAddress.Any, 10001, provider.GetService>() ?? throw new InvalidOperationException(), provider.GetService() ?? throw new InvalidOperationException())); using var host = builder.Build(); using (var scope = host.Services.CreateScope()) { var db = scope.ServiceProvider.GetRequiredService(); await db.Database.MigrateAsync(); } await host.RunAsync();