diff --git a/Danbooru/Danbooru.AppHost/Danbooru.AppHost.csproj b/Danbooru/Danbooru.AppHost/Danbooru.AppHost.csproj new file mode 100644 index 0000000..9f9beb6 --- /dev/null +++ b/Danbooru/Danbooru.AppHost/Danbooru.AppHost.csproj @@ -0,0 +1,22 @@ + + + + + + Exe + net9.0 + enable + enable + true + 1193f016-5317-4904-8300-2570091baffa + + + + + + + + + + + diff --git a/Danbooru/Danbooru.AppHost/Program.cs b/Danbooru/Danbooru.AppHost/Program.cs new file mode 100644 index 0000000..b908b96 --- /dev/null +++ b/Danbooru/Danbooru.AppHost/Program.cs @@ -0,0 +1,5 @@ +var builder = DistributedApplication.CreateBuilder(args); + +builder.AddProject("danbooru-ui"); + +builder.Build().Run(); diff --git a/Danbooru/Danbooru.AppHost/Properties/launchSettings.json b/Danbooru/Danbooru.AppHost/Properties/launchSettings.json new file mode 100644 index 0000000..9b3ef6c --- /dev/null +++ b/Danbooru/Danbooru.AppHost/Properties/launchSettings.json @@ -0,0 +1,29 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17026;http://localhost:15150", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21037", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22044" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15150", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19076", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20063" + } + } + } +} diff --git a/Danbooru/Danbooru.AppHost/appsettings.Development.json b/Danbooru/Danbooru.AppHost/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/Danbooru/Danbooru.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Danbooru/Danbooru.AppHost/appsettings.json b/Danbooru/Danbooru.AppHost/appsettings.json new file mode 100644 index 0000000..31c092a --- /dev/null +++ b/Danbooru/Danbooru.AppHost/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + } +} diff --git a/Danbooru/Danbooru.ServiceDefaults/Danbooru.ServiceDefaults.csproj b/Danbooru/Danbooru.ServiceDefaults/Danbooru.ServiceDefaults.csproj new file mode 100644 index 0000000..24b1b4f --- /dev/null +++ b/Danbooru/Danbooru.ServiceDefaults/Danbooru.ServiceDefaults.csproj @@ -0,0 +1,22 @@ + + + + net9.0 + enable + enable + true + + + + + + + + + + + + + + + diff --git a/Danbooru/Danbooru.ServiceDefaults/Extensions.cs b/Danbooru/Danbooru.ServiceDefaults/Extensions.cs new file mode 100644 index 0000000..13151bf --- /dev/null +++ b/Danbooru/Danbooru.ServiceDefaults/Extensions.cs @@ -0,0 +1,119 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.ServiceDiscovery; +using OpenTelemetry; +using OpenTelemetry.Metrics; +using OpenTelemetry.Trace; + +namespace Microsoft.Extensions.Hosting; + +// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry. +// This project should be referenced by each service project in your solution. +// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults +public static class Extensions +{ + public static TBuilder AddServiceDefaults(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.ConfigureOpenTelemetry(); + + builder.AddDefaultHealthChecks(); + + builder.Services.AddServiceDiscovery(); + + builder.Services.ConfigureHttpClientDefaults(http => + { + // Turn on resilience by default + http.AddStandardResilienceHandler(); + + // Turn on service discovery by default + http.AddServiceDiscovery(); + }); + + // Uncomment the following to restrict the allowed schemes for service discovery. + // builder.Services.Configure(options => + // { + // options.AllowedSchemes = ["https"]; + // }); + + return builder; + } + + public static TBuilder ConfigureOpenTelemetry(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Logging.AddOpenTelemetry(logging => + { + logging.IncludeFormattedMessage = true; + logging.IncludeScopes = true; + }); + + builder.Services.AddOpenTelemetry() + .WithMetrics(metrics => + { + metrics.AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddRuntimeInstrumentation(); + }) + .WithTracing(tracing => + { + tracing.AddSource(builder.Environment.ApplicationName) + .AddAspNetCoreInstrumentation() + // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) + //.AddGrpcClientInstrumentation() + .AddHttpClientInstrumentation(); + }); + + builder.AddOpenTelemetryExporters(); + + return builder; + } + + private static TBuilder AddOpenTelemetryExporters(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); + + if (useOtlpExporter) + { + builder.Services.AddOpenTelemetry().UseOtlpExporter(); + } + + // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package) + //if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) + //{ + // builder.Services.AddOpenTelemetry() + // .UseAzureMonitor(); + //} + + return builder; + } + + public static TBuilder AddDefaultHealthChecks(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Services.AddHealthChecks() + // Add a default liveness check to ensure app is responsive + .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); + + return builder; + } + + public static WebApplication MapDefaultEndpoints(this WebApplication app) + { + // Adding health checks endpoints to applications in non-development environments has security implications. + // See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments. + if (app.Environment.IsDevelopment()) + { + // All health checks must pass for app to be considered ready to accept traffic after starting + app.MapHealthChecks("/health"); + + // Only health checks tagged with the "live" tag must pass for app to be considered alive + app.MapHealthChecks("/alive", new HealthCheckOptions + { + Predicate = r => r.Tags.Contains("live") + }); + } + + return app; + } +} diff --git a/Danbooru/Danbooru.UI/Danbooru.UI/Danbooru.UI.csproj b/Danbooru/Danbooru.UI/Danbooru.UI/Danbooru.UI.csproj index 906fe1b..dbd9d3b 100644 --- a/Danbooru/Danbooru.UI/Danbooru.UI/Danbooru.UI.csproj +++ b/Danbooru/Danbooru.UI/Danbooru.UI/Danbooru.UI.csproj @@ -15,6 +15,7 @@ + diff --git a/Danbooru/Danbooru.UI/Danbooru.UI/Program.cs b/Danbooru/Danbooru.UI/Danbooru.UI/Program.cs index ef71e76..706befd 100644 --- a/Danbooru/Danbooru.UI/Danbooru.UI/Program.cs +++ b/Danbooru/Danbooru.UI/Danbooru.UI/Program.cs @@ -6,6 +6,8 @@ var builder = WebApplication.CreateBuilder(args); +builder.AddServiceDefaults(); + builder.Configuration.AddEnvironmentVariables(); // Add services to the container. @@ -23,6 +25,8 @@ var app = builder.Build(); +app.MapDefaultEndpoints(); + app.UseExceptionHandler("/Error", createScopeForErrors: true); app.UseHsts(); diff --git a/Danbooru/Danbooru.sln b/Danbooru/Danbooru.sln index e7c335c..f302914 100644 --- a/Danbooru/Danbooru.sln +++ b/Danbooru/Danbooru.sln @@ -11,6 +11,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Danbooru.ApiWrapper.Tests", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Danbooru.UI.Tests", "Danbooru.UI.Tests\Danbooru.UI.Tests.csproj", "{3AB1B83B-053A-4AF1-AD0A-EDE816F66144}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Danbooru.AppHost", "Danbooru.AppHost\Danbooru.AppHost.csproj", "{2AADA6A2-C3FD-4B0B-9F27-BCAA13514494}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Danbooru.ServiceDefaults", "Danbooru.ServiceDefaults\Danbooru.ServiceDefaults.csproj", "{C347B38E-6E0E-4387-FE0D-DD3AB8257C97}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +37,14 @@ Global {3AB1B83B-053A-4AF1-AD0A-EDE816F66144}.Debug|Any CPU.Build.0 = Debug|Any CPU {3AB1B83B-053A-4AF1-AD0A-EDE816F66144}.Release|Any CPU.ActiveCfg = Release|Any CPU {3AB1B83B-053A-4AF1-AD0A-EDE816F66144}.Release|Any CPU.Build.0 = Release|Any CPU + {2AADA6A2-C3FD-4B0B-9F27-BCAA13514494}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2AADA6A2-C3FD-4B0B-9F27-BCAA13514494}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2AADA6A2-C3FD-4B0B-9F27-BCAA13514494}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2AADA6A2-C3FD-4B0B-9F27-BCAA13514494}.Release|Any CPU.Build.0 = Release|Any CPU + {C347B38E-6E0E-4387-FE0D-DD3AB8257C97}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C347B38E-6E0E-4387-FE0D-DD3AB8257C97}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C347B38E-6E0E-4387-FE0D-DD3AB8257C97}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C347B38E-6E0E-4387-FE0D-DD3AB8257C97}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE