Skip to content

Commit

Permalink
Added aspire support.
Browse files Browse the repository at this point in the history
  • Loading branch information
DrNoLife committed Nov 29, 2024
1 parent df92e06 commit bfdc882
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Danbooru/Danbooru.AppHost/Danbooru.AppHost.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<Sdk Name="Aspire.AppHost.Sdk" Version="9.0.0" />

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
<UserSecretsId>1193f016-5317-4904-8300-2570091baffa</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Danbooru.UI\Danbooru.UI\Danbooru.UI.csproj" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions Danbooru/Danbooru.AppHost/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var builder = DistributedApplication.CreateBuilder(args);

builder.AddProject<Projects.Danbooru_UI>("danbooru-ui");

builder.Build().Run();
29 changes: 29 additions & 0 deletions Danbooru/Danbooru.AppHost/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
8 changes: 8 additions & 0 deletions Danbooru/Danbooru.AppHost/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions Danbooru/Danbooru.AppHost/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
}
}
22 changes: 22 additions & 0 deletions Danbooru/Danbooru.ServiceDefaults/Danbooru.ServiceDefaults.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireSharedProject>true</IsAspireSharedProject>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />

<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="9.0.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
</ItemGroup>

</Project>
119 changes: 119 additions & 0 deletions Danbooru/Danbooru.ServiceDefaults/Extensions.cs
Original file line number Diff line number Diff line change
@@ -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<TBuilder>(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<ServiceDiscoveryOptions>(options =>
// {
// options.AllowedSchemes = ["https"];
// });

return builder;
}

public static TBuilder ConfigureOpenTelemetry<TBuilder>(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<TBuilder>(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<TBuilder>(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;
}
}
1 change: 1 addition & 0 deletions Danbooru/Danbooru.UI/Danbooru.UI/Danbooru.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<ItemGroup>
<ProjectReference Include="..\..\Danbooru.ApiWrapper\Danbooru.ApiWrapper.csproj" />
<ProjectReference Include="..\..\Danbooru.ServiceDefaults\Danbooru.ServiceDefaults.csproj" />
<ProjectReference Include="..\Danbooru.UI.Client\Danbooru.UI.Client.csproj" />
<PackageReference Include="Blazored.LocalStorage" Version="4.5.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="9.0.0" />
Expand Down
4 changes: 4 additions & 0 deletions Danbooru/Danbooru.UI/Danbooru.UI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();

builder.Configuration.AddEnvironmentVariables();

// Add services to the container.
Expand All @@ -23,6 +25,8 @@

var app = builder.Build();

app.MapDefaultEndpoints();

app.UseExceptionHandler("/Error", createScopeForErrors: true);
app.UseHsts();

Expand Down
12 changes: 12 additions & 0 deletions Danbooru/Danbooru.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit bfdc882

Please sign in to comment.