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
|
|
|
|
2023-08-12 21:02:59 +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;
|
2023-08-09 14:23:41 +00:00
|
|
|
using MassTransit;
|
2023-08-14 11:49:27 +00:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-08-14 19:45:00 +00:00
|
|
|
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;
|
2023-11-20 18:53:40 +00:00
|
|
|
using Npgsql;
|
2023-11-20 13:23:04 +00:00
|
|
|
using OpenTelemetry.Logs;
|
|
|
|
using OpenTelemetry.Metrics;
|
|
|
|
using OpenTelemetry.Resources;
|
|
|
|
using OpenTelemetry.Trace;
|
2023-08-10 08:47:35 +00:00
|
|
|
using Server.DB;
|
|
|
|
using Server.Services;
|
2023-11-25 11:26:28 +00:00
|
|
|
using Wonderking.Game.Mapping;
|
2022-12-31 13:31:42 +00:00
|
|
|
|
2023-08-09 14:23:41 +00:00
|
|
|
var builder = Host.CreateApplicationBuilder();
|
2023-08-14 19:45:00 +00:00
|
|
|
#if DEBUG
|
|
|
|
builder.Environment.EnvironmentName = "Development";
|
|
|
|
#endif
|
2023-11-16 20:01:55 +00:00
|
|
|
|
2023-11-22 05:28:01 +00:00
|
|
|
builder.Services.AddMetrics();
|
|
|
|
|
2023-08-14 19:45:00 +00:00
|
|
|
builder.Configuration.AddJsonFile("settings.json", true, true)
|
2023-08-14 20:54:22 +00:00
|
|
|
.AddJsonFile($"settings.{builder.Environment.EnvironmentName}.json", true)
|
2023-08-14 19:45:00 +00:00
|
|
|
.AddEnvironmentVariables().Build();
|
2023-11-16 20:01:55 +00:00
|
|
|
|
2023-08-10 08:47:35 +00:00
|
|
|
builder.Services.AddLogging();
|
2023-11-16 11:06:36 +00:00
|
|
|
var loggerFactory = LoggerFactory.Create(loggingBuilder =>
|
|
|
|
{
|
2023-11-16 17:49:43 +00:00
|
|
|
loggingBuilder.AddFile("logs/Server-{Date}.log", LogLevel.Trace);
|
|
|
|
loggingBuilder.AddFile("logs/Server-{Date}.json.log", LogLevel.Trace, isJson: true);
|
2023-11-16 11:06:36 +00:00
|
|
|
loggingBuilder.AddConsole();
|
|
|
|
});
|
2023-11-16 20:01:55 +00:00
|
|
|
|
2023-11-20 13:23:04 +00:00
|
|
|
var configuration = builder.Configuration;
|
2023-11-20 18:53:40 +00:00
|
|
|
if (configuration.GetValue<bool>("Tracing:Enabled"))
|
|
|
|
{
|
|
|
|
Action<ResourceBuilder> resourceBuilderAction = r => r
|
|
|
|
.AddService("Continuity", serviceInstanceId: Environment.MachineName);
|
2023-11-20 13:23:04 +00:00
|
|
|
|
2023-11-20 18:53:40 +00:00
|
|
|
builder.Services.AddOpenTelemetry()
|
|
|
|
.ConfigureResource(resourceBuilderAction)
|
|
|
|
.WithTracing(tracing =>
|
|
|
|
{
|
|
|
|
tracing.AddSource(nameof(Server));
|
2023-11-22 05:28:01 +00:00
|
|
|
//tracing.AddSource("MassTransit");
|
2023-11-20 18:53:40 +00:00
|
|
|
tracing.AddEntityFrameworkCoreInstrumentation(options => options.SetDbStatementForText = true);
|
|
|
|
tracing.AddNpgsql();
|
|
|
|
})
|
|
|
|
.WithMetrics(metrics =>
|
|
|
|
{
|
|
|
|
metrics.AddRuntimeInstrumentation();
|
2023-11-28 14:32:41 +00:00
|
|
|
metrics.AddProcessInstrumentation();
|
2023-11-20 18:53:40 +00:00
|
|
|
});
|
2023-11-20 13:23:04 +00:00
|
|
|
|
2023-11-20 18:53:40 +00:00
|
|
|
builder.Logging.AddOpenTelemetry(logging =>
|
2023-11-20 13:23:04 +00:00
|
|
|
{
|
2023-11-20 18:53:40 +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
|
|
|
{
|
2023-11-20 18:53:40 +00:00
|
|
|
logging.AddOtlpExporter(options =>
|
|
|
|
{
|
2023-11-28 14:32:41 +00:00
|
|
|
options.Endpoint = new Uri(configuration["OTLP:Logging:Endpoint"] ?? string.Empty);
|
2023-11-20 18:53:40 +00:00
|
|
|
});
|
2023-11-20 13:29:04 +00:00
|
|
|
});
|
2023-11-20 18:53:40 +00:00
|
|
|
builder.Services.ConfigureOpenTelemetryMeterProvider(metrics =>
|
|
|
|
{
|
|
|
|
metrics.AddOtlpExporter(options =>
|
2023-11-28 14:32:41 +00:00
|
|
|
options.Endpoint = new Uri(configuration["OTLP:Metrics:Endpoint"] ?? string.Empty));
|
2023-11-20 18:53:40 +00:00
|
|
|
});
|
|
|
|
builder.Services.ConfigureOpenTelemetryTracerProvider(tracing =>
|
|
|
|
{
|
|
|
|
tracing.AddZipkinExporter(options =>
|
|
|
|
options.Endpoint = new Uri(configuration["Zipkin:Endpoint"] ?? string.Empty));
|
2023-11-28 14:32:41 +00:00
|
|
|
tracing.AddOtlpExporter(options => options.Endpoint = new Uri(configuration["OTLP:Tracing:Endpoint"] ?? string.Empty));
|
2023-11-20 18:53:40 +00:00
|
|
|
});
|
|
|
|
}
|
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-16 20:01:55 +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);
|
2023-08-09 14:23:41 +00:00
|
|
|
builder.Services.AddSingleton<PacketDistributorService>();
|
2023-11-15 19:00:08 +00:00
|
|
|
builder.Services.AddSingleton<ItemObjectPoolService>();
|
2023-11-19 14:00:33 +00:00
|
|
|
builder.Services.AddHostedService(provider =>
|
|
|
|
provider.GetService<ItemObjectPoolService>() ?? throw new InvalidOperationException());
|
2023-08-14 19:30:32 +00:00
|
|
|
builder.Services.AddHostedService(provider =>
|
2023-08-09 14:23:41 +00:00
|
|
|
provider.GetService<PacketDistributorService>() ?? throw new InvalidOperationException());
|
|
|
|
builder.Services.AddMassTransit(x =>
|
2022-12-31 13:31:42 +00:00
|
|
|
{
|
2023-08-12 21:02:59 +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-14 19:30:32 +00:00
|
|
|
builder.Services.AddHostedService(provider => new WonderkingAuthServer(IPAddress.Any, 10001,
|
2023-08-09 14:23:41 +00:00
|
|
|
provider.GetService<ILogger<WonderkingAuthServer>>() ?? throw new InvalidOperationException(),
|
2023-08-10 08:47:35 +00:00
|
|
|
provider.GetService<IServiceProvider>() ?? throw new InvalidOperationException()));
|
2022-12-31 13:31:42 +00:00
|
|
|
|
2023-08-11 09:31:30 +00:00
|
|
|
using var host = builder.Build();
|
2023-10-27 17:47:17 +00:00
|
|
|
using (var scope = host.Services.CreateScope())
|
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();
|