continuity/Continuity.AuthServer/Program.cs

128 lines
5 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;
using Continuity.AuthServer.DB;
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;
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();
#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));
2023-11-22 05:28:01 +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);
2023-08-09 14:23:41 +00:00
builder.Services.AddSingleton<PacketDistributorService>();
builder.Services.AddSingleton<ItemObjectPoolService>();
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
{
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();
chore: .net8 support and extended support for pipeline jobs fix: incorrect command fix: incorrect command fix: incorrect command fix: export tools path chore: test dir fix: test build script fix: missing semicolon fix: missing semicolon fix: missing buildscript fix: missing buildscript chore: update build to .net7 ci: set root dir ci: set root dir revert: .net 8 support for benchmarks ci: disable docker for ci ci: disable docker for ci ci: disable docker for ci ci: trigger ci: i stg ci: let's hope its the correct one ci: i stg ci: further sonarscanner setup ci: add tools ci: rearrange ci: verbose output ci: hardcoded project key ci: test env ci: test more env ci: env test again ci: test env ci: shit ci: final setup ci: final setup ci: final setup ci: adjust buildscript ci: nuke ci: install java ci: install java refactor: loggermessages to own namespace chore: switch to dotnet foundation editorconfig preset ci: switch to basic gitea ci ci: steps ci: add missing runs-on ci: remove unnecessary actions ci: test attempt? ci: add missing name for step ci: fix missing project name reference ci: lets try again ci: again ci: again.. ci: idk at this point ci: append path prematurely ci: add path to bash cli: I really don't know ci: again.... ci: idk ci: again.... ci: another one ci: fix incorrect path add-path ci: add dependency track support ci: fix upload ci: forgot to adjust data for action ci: incorrect path? ci: add version tag ci: idk ci: fix incorrect path for bom ci: fix version tag ci: disable github license resolution for now ci: does this work ci: another one bites the dust chore: .net 8 and extended pipeline support ci: again chore: dockerignore added chore(deps): update dependency benchmarkdotnet to v0.13.10 Signed-off-by: noreply@rainote.dev ci: dependency track can run on any branch ci: first attempt docker image ci: again ci: some fixes ci: idk ci: does this help ci: idk ci: another one ci: forgot ci: downgrade qemu setup ci: downgrade.. ci: v1 includes docker ci: rearrange ci: idk what to do with this ci: let's try cat ci: alt ci: depressing ci: yikes ci: ah come on ci: let's try new version again ci: again ci: another one ci: another one ci: confusion ci: try single ci: aaaa ci: one more time ci: again main (#69) Reviewed-on: https://forge.rainote.dev/wonderking/continuity/pulls/69 Co-authored-by: Timothy Schenk <admin@rainote.dev> Co-committed-by: Timothy Schenk <admin@rainote.dev> chore: ci jobs expanded & .net 8 support ci: branch name sanitization for docker ci: another attempt ci: forgot actor chore: remove nuke remnants chore(deps): update dependency dotnet-sdk to v7.0.403 Signed-off-by: noreply@rainote.dev refactor: shared data into library refactor: rewrite for packethandler/id map chore: logging chore: Sonar Warnings chore: upgrade to .net8 image chore: fetch all required information for characters chore: upgrade to .net 8 ci: install .net 7.0 alongside 8.0 for sbom fix: incorrect job ci: let's try arm64 again ci: adjust project for .net 8 support chore: adjustments to composefile chore: analyzer setup chore: analyzer and project settings updated chore: ci jobs expanded & .net 8 support
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();