108 lines
4.5 KiB
C#
108 lines
4.5 KiB
C#
using System.Net;
|
|
using System.Reflection;
|
|
using System.Text.Json;
|
|
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 OpenTelemetry.Logs;
|
|
using OpenTelemetry.Metrics;
|
|
using OpenTelemetry.Resources;
|
|
using OpenTelemetry.Trace;
|
|
using Server.DB;
|
|
using Server.Services;
|
|
using Wonderking.Game.Mapping;
|
|
|
|
var builder = Host.CreateApplicationBuilder();
|
|
#if DEBUG
|
|
builder.Environment.EnvironmentName = "Development";
|
|
#endif
|
|
|
|
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/Server-{Date}.log", LogLevel.Trace);
|
|
loggingBuilder.AddFile("logs/Server-{Date}.json.log", LogLevel.Trace, isJson: true);
|
|
loggingBuilder.AddConsole();
|
|
});
|
|
|
|
var configuration = builder.Configuration;
|
|
|
|
Action<ResourceBuilder> resourceBuilderAction = r => r
|
|
.AddService("Continuity", serviceInstanceId: Environment.MachineName);
|
|
|
|
builder.Services.AddOpenTelemetry()
|
|
.ConfigureResource(resourceBuilderAction)
|
|
.WithTracing(tracing =>
|
|
{
|
|
tracing.AddSource(nameof(Server));
|
|
tracing.SetSampler(new AlwaysOnSampler());
|
|
tracing.AddMassTransitInstrumentation();
|
|
tracing.AddEntityFrameworkCoreInstrumentation(options => options.SetDbStatementForText = true);
|
|
tracing.AddHttpClientInstrumentation();
|
|
})
|
|
.WithMetrics(metrics =>
|
|
{
|
|
metrics.AddRuntimeInstrumentation();
|
|
metrics.AddHttpClientInstrumentation();
|
|
});
|
|
|
|
builder.Logging.AddOpenTelemetry(logging =>
|
|
{
|
|
var resourceBuilder = ResourceBuilder.CreateDefault();
|
|
resourceBuilderAction(resourceBuilder);
|
|
logging.SetResourceBuilder(resourceBuilder);
|
|
logging.IncludeFormattedMessage = true;
|
|
logging.IncludeScopes = true;
|
|
});
|
|
builder.Services.Configure<OpenTelemetryLoggerOptions>(logging =>
|
|
logging.AddOtlpExporter(options => options.Endpoint = new Uri(configuration["OTLP:Endpoint"] ?? string.Empty)));
|
|
builder.Services.ConfigureOpenTelemetryMeterProvider(metrics =>
|
|
metrics.AddOtlpExporter(options => options.Endpoint = new Uri(configuration["OTLP:Endpoint"] ?? string.Empty)));
|
|
builder.Services.ConfigureOpenTelemetryTracerProvider(tracing =>
|
|
tracing.AddOtlpExporter(options => options.Endpoint = new Uri(configuration["OTLP:Endpoint"] ?? string.Empty)));
|
|
|
|
builder.Services.AddHealthChecks()
|
|
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
|
|
builder.Services.AddSingleton<CharacterStatsMappingConfiguration>(
|
|
JsonSerializer.Deserialize<CharacterStatsMappingConfiguration>(
|
|
File.ReadAllText("config/character-stats.mapping.json")) ?? throw new InvalidOperationException());
|
|
|
|
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);
|
|
});
|
|
|
|
builder.Services.AddSingleton<ILoggerFactory>(loggerFactory);
|
|
builder.Services.AddSingleton<PacketDistributorService>();
|
|
builder.Services.AddSingleton<ItemObjectPoolService>();
|
|
builder.Services.AddHostedService(provider =>
|
|
provider.GetService<ItemObjectPoolService>() ?? throw new InvalidOperationException());
|
|
builder.Services.AddHostedService(provider =>
|
|
provider.GetService<PacketDistributorService>() ?? 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<ILogger<WonderkingAuthServer>>() ?? throw new InvalidOperationException(),
|
|
provider.GetService<IServiceProvider>() ?? throw new InvalidOperationException()));
|
|
|
|
using var host = builder.Build();
|
|
using (var scope = host.Services.CreateScope())
|
|
{
|
|
var db = scope.ServiceProvider.GetRequiredService<WonderkingContext>();
|
|
await db.Database.MigrateAsync().ConfigureAwait(true);
|
|
}
|
|
|
|
await host.RunAsync().ConfigureAwait(true);
|