From a04c20c85780bf339c09821c429d76ffac8b6ae4 Mon Sep 17 00:00:00 2001 From: Sergey Karmanov Date: Mon, 11 May 2026 00:20:39 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20.net=20Aspire?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/UniVerse.Api/Program.cs | 14 +- backend/UniVerse.Api/UniVerse.Api.csproj | 1 + backend/UniVerse.AppHost/AppHost.cs | 15 +++ .../Properties/launchSettings.json | 32 +++++ .../UniVerse.AppHost/UniVerse.AppHost.csproj | 19 +++ .../appsettings.Development.json | 8 ++ backend/UniVerse.AppHost/appsettings.json | 9 ++ backend/UniVerse.AppHost/aspire.config.json | 5 + .../UniVerse.ServiceDefaults/Extensions.cs | 126 ++++++++++++++++++ .../UniVerse.ServiceDefaults.csproj | 23 ++++ backend/UniVerse.sln | 12 ++ frontend/README.md | 18 +++ frontend/package.json | 1 + 13 files changed, 282 insertions(+), 1 deletion(-) create mode 100644 backend/UniVerse.AppHost/AppHost.cs create mode 100644 backend/UniVerse.AppHost/Properties/launchSettings.json create mode 100644 backend/UniVerse.AppHost/UniVerse.AppHost.csproj create mode 100644 backend/UniVerse.AppHost/appsettings.Development.json create mode 100644 backend/UniVerse.AppHost/appsettings.json create mode 100644 backend/UniVerse.AppHost/aspire.config.json create mode 100644 backend/UniVerse.ServiceDefaults/Extensions.cs create mode 100644 backend/UniVerse.ServiceDefaults/UniVerse.ServiceDefaults.csproj diff --git a/backend/UniVerse.Api/Program.cs b/backend/UniVerse.Api/Program.cs index 4da37e8..6204f5d 100644 --- a/backend/UniVerse.Api/Program.cs +++ b/backend/UniVerse.Api/Program.cs @@ -14,6 +14,13 @@ using UniVerse.Infrastructure.ExternalServices; var builder = WebApplication.CreateBuilder(args); +var useAspire = builder.Configuration.GetValue("Aspire:Enabled"); + +if (useAspire) +{ + builder.AddServiceDefaults(); +} + // --- Serilog --- Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(builder.Configuration) @@ -139,6 +146,11 @@ builder.Services.AddSwaggerGen(options => var app = builder.Build(); +if (useAspire) +{ + app.MapDefaultEndpoints(); +} + // --- Middleware Pipeline --- app.UseMiddleware(); app.UseMiddleware(); @@ -162,4 +174,4 @@ app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); -app.Run(); \ No newline at end of file +app.Run(); diff --git a/backend/UniVerse.Api/UniVerse.Api.csproj b/backend/UniVerse.Api/UniVerse.Api.csproj index 86f5e3f..69aaade 100644 --- a/backend/UniVerse.Api/UniVerse.Api.csproj +++ b/backend/UniVerse.Api/UniVerse.Api.csproj @@ -27,6 +27,7 @@ + diff --git a/backend/UniVerse.AppHost/AppHost.cs b/backend/UniVerse.AppHost/AppHost.cs new file mode 100644 index 0000000..36f7eb4 --- /dev/null +++ b/backend/UniVerse.AppHost/AppHost.cs @@ -0,0 +1,15 @@ +var builder = DistributedApplication.CreateBuilder(args); + +var api = builder + .AddProject("universe-api") + .WithEnvironment("Aspire__Enabled", "true"); + +// Запуск фронтенда (Vue + Vite) в dev-режиме вместе. +// Требования: установлен pnpm (или включён corepack), зависимости фронта установлены. +builder + .AddExecutable("universe-frontend", "pnpm", workingDirectory: "../../frontend") + .WithArgs("run", "dev:aspire") + // Используется в vite.config.ts для server.proxy['/api'].target + .WithEnvironment("VITE_API_PROXY_TARGET", api.GetEndpoint("http")); + +builder.Build().Run(); diff --git a/backend/UniVerse.AppHost/Properties/launchSettings.json b/backend/UniVerse.AppHost/Properties/launchSettings.json new file mode 100644 index 0000000..2a6dfce --- /dev/null +++ b/backend/UniVerse.AppHost/Properties/launchSettings.json @@ -0,0 +1,32 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17156;http://localhost:15060", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21010", + "ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "https://localhost:23046", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22274" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15060", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19138", + "ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "http://localhost:18238", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20274" + } + } + } +} diff --git a/backend/UniVerse.AppHost/UniVerse.AppHost.csproj b/backend/UniVerse.AppHost/UniVerse.AppHost.csproj new file mode 100644 index 0000000..2ccc522 --- /dev/null +++ b/backend/UniVerse.AppHost/UniVerse.AppHost.csproj @@ -0,0 +1,19 @@ + + + + Exe + net10.0 + enable + enable + fb90d29a-6c48-471b-b19f-d2f431a5ef38 + + + + + + + + + + + diff --git a/backend/UniVerse.AppHost/appsettings.Development.json b/backend/UniVerse.AppHost/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/backend/UniVerse.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/backend/UniVerse.AppHost/appsettings.json b/backend/UniVerse.AppHost/appsettings.json new file mode 100644 index 0000000..31c092a --- /dev/null +++ b/backend/UniVerse.AppHost/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + } +} diff --git a/backend/UniVerse.AppHost/aspire.config.json b/backend/UniVerse.AppHost/aspire.config.json new file mode 100644 index 0000000..a62b559 --- /dev/null +++ b/backend/UniVerse.AppHost/aspire.config.json @@ -0,0 +1,5 @@ +{ + "appHost": { + "path": "UniVerse.AppHost.csproj" + } +} diff --git a/backend/UniVerse.ServiceDefaults/Extensions.cs b/backend/UniVerse.ServiceDefaults/Extensions.cs new file mode 100644 index 0000000..224d3d9 --- /dev/null +++ b/backend/UniVerse.ServiceDefaults/Extensions.cs @@ -0,0 +1,126 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Logging; +using OpenTelemetry; +using OpenTelemetry.Metrics; +using OpenTelemetry.Trace; + +namespace Microsoft.Extensions.Hosting; + +// Adds common 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 +{ + private const string HealthEndpointPath = "/health"; + private const string AlivenessEndpointPath = "/alive"; + + 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(tracing => + // Exclude health check requests from tracing + tracing.Filter = context => + !context.Request.Path.StartsWithSegments(HealthEndpointPath) + && !context.Request.Path.StartsWithSegments(AlivenessEndpointPath) + ) + // 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(HealthEndpointPath); + + // Only health checks tagged with the "live" tag must pass for app to be considered alive + app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions + { + Predicate = r => r.Tags.Contains("live") + }); + } + + return app; + } +} diff --git a/backend/UniVerse.ServiceDefaults/UniVerse.ServiceDefaults.csproj b/backend/UniVerse.ServiceDefaults/UniVerse.ServiceDefaults.csproj new file mode 100644 index 0000000..a056dd4 --- /dev/null +++ b/backend/UniVerse.ServiceDefaults/UniVerse.ServiceDefaults.csproj @@ -0,0 +1,23 @@ + + + + net10.0 + enable + enable + true + true + + + + + + + + + + + + + + + diff --git a/backend/UniVerse.sln b/backend/UniVerse.sln index 5b76058..ad0e781 100644 --- a/backend/UniVerse.sln +++ b/backend/UniVerse.sln @@ -8,6 +8,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UniVerse.Application", "Uni EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UniVerse.Infrastructure", "UniVerse.Infrastructure\UniVerse.Infrastructure.csproj", "{A1B2C3D4-1111-2222-3333-444455558888}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UniVerse.AppHost", "UniVerse.AppHost\UniVerse.AppHost.csproj", "{CC38B044-852A-4E9C-AB35-EF7E35088490}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UniVerse.ServiceDefaults", "UniVerse.ServiceDefaults\UniVerse.ServiceDefaults.csproj", "{28475301-CBE1-4AE9-937F-8FD89E3EA6F0}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -30,5 +34,13 @@ Global {A1B2C3D4-1111-2222-3333-444455558888}.Debug|Any CPU.Build.0 = Debug|Any CPU {A1B2C3D4-1111-2222-3333-444455558888}.Release|Any CPU.ActiveCfg = Release|Any CPU {A1B2C3D4-1111-2222-3333-444455558888}.Release|Any CPU.Build.0 = Release|Any CPU + {CC38B044-852A-4E9C-AB35-EF7E35088490}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CC38B044-852A-4E9C-AB35-EF7E35088490}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CC38B044-852A-4E9C-AB35-EF7E35088490}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CC38B044-852A-4E9C-AB35-EF7E35088490}.Release|Any CPU.Build.0 = Release|Any CPU + {28475301-CBE1-4AE9-937F-8FD89E3EA6F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {28475301-CBE1-4AE9-937F-8FD89E3EA6F0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {28475301-CBE1-4AE9-937F-8FD89E3EA6F0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {28475301-CBE1-4AE9-937F-8FD89E3EA6F0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/frontend/README.md b/frontend/README.md index c7deeeb..f2916fd 100644 --- a/frontend/README.md +++ b/frontend/README.md @@ -29,6 +29,24 @@ See [Vite Configuration Reference](https://vite.dev/config/). pnpm install ``` +## Запуск вместе с backend (Aspire) + +Если запускать приложение через `backend/UniVerse.AppHost`, то фронтенд (Vite dev server) поднимается автоматически. + +1) Установить зависимости фронтенда: + +```sh +pnpm -C frontend install +``` + +2) Запустить Aspire AppHost: + +```sh +dotnet run --project backend/UniVerse.AppHost/UniVerse.AppHost.csproj +``` + +Обычно фронтенд слушает `http://localhost:5173` (если порт занят — Vite выберет следующий свободный). + ### Compile and Hot-Reload for Development ```sh diff --git a/frontend/package.json b/frontend/package.json index a76bd1d..38aad31 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -5,6 +5,7 @@ "type": "module", "scripts": { "dev": "vite", + "dev:aspire": "vite --host 0.0.0.0 --port 5173", "build": "run-p type-check \"build-only {@}\" --", "preview": "vite preview", "build-only": "vite build",