From 769aff3d4fd6b7869e1983ab2c5adcc8724af7c7 Mon Sep 17 00:00:00 2001 From: xiaolipro <2357729423@qq.com> Date: Mon, 26 Feb 2024 18:09:19 +0800 Subject: [PATCH] init --- .dockerignore | 25 ++++ .gitignore | 8 ++ InterfaceForward.sln | 49 ++++++++ NuGet.Config | 6 + common.props | 7 ++ src/InterfaceForward.Api/Dockerfile | 23 ++++ .../InterfaceForward.Api.csproj | 24 ++++ .../InterfaceForwardApiModule.cs | 26 ++++ src/InterfaceForward.Api/Program.cs | 12 ++ .../Properties/launchSettings.json | 38 ++++++ .../appsettings.Development.json | 8 ++ src/InterfaceForward.Api/appsettings.json | 9 ++ .../InterfaceForward.AppHost.csproj | 19 +++ src/InterfaceForward.AppHost/Program.cs | 5 + .../Properties/launchSettings.json | 16 +++ .../appsettings.Development.json | 8 ++ src/InterfaceForward.AppHost/appsettings.json | 9 ++ .../InterfaceAggregate/InterfaceEntity.cs | 39 ++++++ .../Enumerations/ParameterType.cs | 12 ++ .../Enumerations/RequestContentType.cs | 8 ++ .../Enumerations/ResponseContentType.cs | 6 + .../Enumerations/ServiceType.cs | 7 ++ src/InterfaceForward.Domain/GlobalUsings.cs | 4 + .../InterfaceForward.Domain.csproj | 13 ++ .../InterfaceForwardDomainModule.cs | 8 ++ .../Extensions.cs | 119 ++++++++++++++++++ .../InterfaceForward.ServiceDefaults.csproj | 24 ++++ src/InterfaceForward.sln | 30 +++++ 28 files changed, 562 insertions(+) create mode 100644 .dockerignore create mode 100644 .gitignore create mode 100644 InterfaceForward.sln create mode 100644 NuGet.Config create mode 100644 common.props create mode 100644 src/InterfaceForward.Api/Dockerfile create mode 100644 src/InterfaceForward.Api/InterfaceForward.Api.csproj create mode 100644 src/InterfaceForward.Api/InterfaceForwardApiModule.cs create mode 100644 src/InterfaceForward.Api/Program.cs create mode 100644 src/InterfaceForward.Api/Properties/launchSettings.json create mode 100644 src/InterfaceForward.Api/appsettings.Development.json create mode 100644 src/InterfaceForward.Api/appsettings.json create mode 100644 src/InterfaceForward.AppHost/InterfaceForward.AppHost.csproj create mode 100644 src/InterfaceForward.AppHost/Program.cs create mode 100644 src/InterfaceForward.AppHost/Properties/launchSettings.json create mode 100644 src/InterfaceForward.AppHost/appsettings.Development.json create mode 100644 src/InterfaceForward.AppHost/appsettings.json create mode 100644 src/InterfaceForward.Domain/Aggregates/InterfaceAggregate/InterfaceEntity.cs create mode 100644 src/InterfaceForward.Domain/Enumerations/ParameterType.cs create mode 100644 src/InterfaceForward.Domain/Enumerations/RequestContentType.cs create mode 100644 src/InterfaceForward.Domain/Enumerations/ResponseContentType.cs create mode 100644 src/InterfaceForward.Domain/Enumerations/ServiceType.cs create mode 100644 src/InterfaceForward.Domain/GlobalUsings.cs create mode 100644 src/InterfaceForward.Domain/InterfaceForward.Domain.csproj create mode 100644 src/InterfaceForward.Domain/InterfaceForwardDomainModule.cs create mode 100644 src/InterfaceForward.ServiceDefaults/Extensions.cs create mode 100644 src/InterfaceForward.ServiceDefaults/InterfaceForward.ServiceDefaults.csproj create mode 100644 src/InterfaceForward.sln diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..cd967fc --- /dev/null +++ b/.dockerignore @@ -0,0 +1,25 @@ +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.idea +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6163587 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +bin/ +obj/ +riderModule.iml +/_ReSharper.Caches/ + +.idea/ +/.vs +/Fake.sln.DotSettings.user diff --git a/InterfaceForward.sln b/InterfaceForward.sln new file mode 100644 index 0000000..24b090f --- /dev/null +++ b/InterfaceForward.sln @@ -0,0 +1,49 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InterfaceForward.AppHost", "src\InterfaceForward.AppHost\InterfaceForward.AppHost.csproj", "{9609CC2D-CC83-4C01-BE36-C0429A06B238}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InterfaceForward.ServiceDefaults", "src\InterfaceForward.ServiceDefaults\InterfaceForward.ServiceDefaults.csproj", "{6DAA9CF1-155A-4FC7-9EAE-7E8FA22CD23A}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{EA8DF4A8-7618-4C28-BEDC-21629B8B219B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InterfaceForward.Api", "src\InterfaceForward.Api\InterfaceForward.Api.csproj", "{0E2F3A09-4EC4-4A61-85DC-9F9F770C3B65}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solution items", "solution items", "{670CD43F-BED5-431F-B47D-8CE1D5C1DE71}" + ProjectSection(SolutionItems) = preProject + NuGet.Config = NuGet.Config + common.props = common.props + .gitignore = .gitignore + EndProjectSection +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InterfaceForward.Domain", "src\InterfaceForward.Domain\InterfaceForward.Domain.csproj", "{AB53F0CC-DE7A-4AE9-B6AA-2A8283B4EB23}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9609CC2D-CC83-4C01-BE36-C0429A06B238}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9609CC2D-CC83-4C01-BE36-C0429A06B238}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9609CC2D-CC83-4C01-BE36-C0429A06B238}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9609CC2D-CC83-4C01-BE36-C0429A06B238}.Release|Any CPU.Build.0 = Release|Any CPU + {6DAA9CF1-155A-4FC7-9EAE-7E8FA22CD23A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6DAA9CF1-155A-4FC7-9EAE-7E8FA22CD23A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6DAA9CF1-155A-4FC7-9EAE-7E8FA22CD23A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6DAA9CF1-155A-4FC7-9EAE-7E8FA22CD23A}.Release|Any CPU.Build.0 = Release|Any CPU + {0E2F3A09-4EC4-4A61-85DC-9F9F770C3B65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0E2F3A09-4EC4-4A61-85DC-9F9F770C3B65}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0E2F3A09-4EC4-4A61-85DC-9F9F770C3B65}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0E2F3A09-4EC4-4A61-85DC-9F9F770C3B65}.Release|Any CPU.Build.0 = Release|Any CPU + {AB53F0CC-DE7A-4AE9-B6AA-2A8283B4EB23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AB53F0CC-DE7A-4AE9-B6AA-2A8283B4EB23}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AB53F0CC-DE7A-4AE9-B6AA-2A8283B4EB23}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AB53F0CC-DE7A-4AE9-B6AA-2A8283B4EB23}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {9609CC2D-CC83-4C01-BE36-C0429A06B238} = {EA8DF4A8-7618-4C28-BEDC-21629B8B219B} + {6DAA9CF1-155A-4FC7-9EAE-7E8FA22CD23A} = {EA8DF4A8-7618-4C28-BEDC-21629B8B219B} + {0E2F3A09-4EC4-4A61-85DC-9F9F770C3B65} = {EA8DF4A8-7618-4C28-BEDC-21629B8B219B} + {AB53F0CC-DE7A-4AE9-B6AA-2A8283B4EB23} = {EA8DF4A8-7618-4C28-BEDC-21629B8B219B} + EndGlobalSection +EndGlobal diff --git a/NuGet.Config b/NuGet.Config new file mode 100644 index 0000000..401fcb1 --- /dev/null +++ b/NuGet.Config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/common.props b/common.props new file mode 100644 index 0000000..016d1fb --- /dev/null +++ b/common.props @@ -0,0 +1,7 @@ + + + latest + enable + Nullable + + diff --git a/src/InterfaceForward.Api/Dockerfile b/src/InterfaceForward.Api/Dockerfile new file mode 100644 index 0000000..f07a18a --- /dev/null +++ b/src/InterfaceForward.Api/Dockerfile @@ -0,0 +1,23 @@ +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base +USER $APP_UID +WORKDIR /app +EXPOSE 8080 +EXPOSE 8081 + +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build +ARG BUILD_CONFIGURATION=Release +WORKDIR /src +COPY ["src/InterfaceForward.Api/InterfaceForward.Api.csproj", "src/InterfaceForward.Api/"] +RUN dotnet restore "src/InterfaceForward.Api/InterfaceForward.Api.csproj" +COPY . . +WORKDIR "/src/src/InterfaceForward.Api" +RUN dotnet build "InterfaceForward.Api.csproj" -c $BUILD_CONFIGURATION -o /app/build + +FROM build AS publish +ARG BUILD_CONFIGURATION=Release +RUN dotnet publish "InterfaceForward.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "InterfaceForward.Api.dll"] diff --git a/src/InterfaceForward.Api/InterfaceForward.Api.csproj b/src/InterfaceForward.Api/InterfaceForward.Api.csproj new file mode 100644 index 0000000..7b7f965 --- /dev/null +++ b/src/InterfaceForward.Api/InterfaceForward.Api.csproj @@ -0,0 +1,24 @@ + + + + net8.0 + enable + enable + Linux + + + + + .dockerignore + + + + + + + + + + + + diff --git a/src/InterfaceForward.Api/InterfaceForwardApiModule.cs b/src/InterfaceForward.Api/InterfaceForwardApiModule.cs new file mode 100644 index 0000000..a75bd46 --- /dev/null +++ b/src/InterfaceForward.Api/InterfaceForwardApiModule.cs @@ -0,0 +1,26 @@ +using Fake.AspNetCore; +using Fake.Modularity; + +namespace InterfaceForward.Api; + +[DependsOn(typeof(FakeAspNetCoreModule))] +public class InterfaceForwardApiModule : FakeModule +{ + public override void ConfigureServices(ServiceConfigurationContext context) + { + var services = context.Services; + + // Add services to the container. + services.AddProblemDetails(); + } + + public override void ConfigureApplication(ApplicationConfigureContext context) + { + var app = context.GetWebApplication(); + + // Add Aspire default endpoints. + app.MapDefaultEndpoints(); + + app.MapGet("/", () => "Hello World!"); + } +} \ No newline at end of file diff --git a/src/InterfaceForward.Api/Program.cs b/src/InterfaceForward.Api/Program.cs new file mode 100644 index 0000000..9fc5ea9 --- /dev/null +++ b/src/InterfaceForward.Api/Program.cs @@ -0,0 +1,12 @@ +using InterfaceForward.Api; + +var builder = WebApplication.CreateSlimBuilder(args); + +builder.Services.AddApplication(); + +// Add service defaults & Aspire components. +builder.AddServiceDefaults(); + +var app = builder.Build(); +app.InitializeApplication(); +app.Run(); \ No newline at end of file diff --git a/src/InterfaceForward.Api/Properties/launchSettings.json b/src/InterfaceForward.Api/Properties/launchSettings.json new file mode 100644 index 0000000..ba1b501 --- /dev/null +++ b/src/InterfaceForward.Api/Properties/launchSettings.json @@ -0,0 +1,38 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:55112", + "sslPort": 44332 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5145", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7210;http://localhost:5145", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/src/InterfaceForward.Api/appsettings.Development.json b/src/InterfaceForward.Api/appsettings.Development.json new file mode 100644 index 0000000..a6e86ac --- /dev/null +++ b/src/InterfaceForward.Api/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/src/InterfaceForward.Api/appsettings.json b/src/InterfaceForward.Api/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/src/InterfaceForward.Api/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/src/InterfaceForward.AppHost/InterfaceForward.AppHost.csproj b/src/InterfaceForward.AppHost/InterfaceForward.AppHost.csproj new file mode 100644 index 0000000..5308000 --- /dev/null +++ b/src/InterfaceForward.AppHost/InterfaceForward.AppHost.csproj @@ -0,0 +1,19 @@ + + + + Exe + net8.0 + enable + enable + true + + + + + + + + + + + diff --git a/src/InterfaceForward.AppHost/Program.cs b/src/InterfaceForward.AppHost/Program.cs new file mode 100644 index 0000000..cf57e62 --- /dev/null +++ b/src/InterfaceForward.AppHost/Program.cs @@ -0,0 +1,5 @@ +var builder = DistributedApplication.CreateBuilder(args); + +builder.AddProject("apiservice"); + +builder.Build().Run(); \ No newline at end of file diff --git a/src/InterfaceForward.AppHost/Properties/launchSettings.json b/src/InterfaceForward.AppHost/Properties/launchSettings.json new file mode 100644 index 0000000..c7b4e88 --- /dev/null +++ b/src/InterfaceForward.AppHost/Properties/launchSettings.json @@ -0,0 +1,16 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15177", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:16294" + } + } + } +} diff --git a/src/InterfaceForward.AppHost/appsettings.Development.json b/src/InterfaceForward.AppHost/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/src/InterfaceForward.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/src/InterfaceForward.AppHost/appsettings.json b/src/InterfaceForward.AppHost/appsettings.json new file mode 100644 index 0000000..31c092a --- /dev/null +++ b/src/InterfaceForward.AppHost/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + } +} diff --git a/src/InterfaceForward.Domain/Aggregates/InterfaceAggregate/InterfaceEntity.cs b/src/InterfaceForward.Domain/Aggregates/InterfaceAggregate/InterfaceEntity.cs new file mode 100644 index 0000000..90cb9b5 --- /dev/null +++ b/src/InterfaceForward.Domain/Aggregates/InterfaceAggregate/InterfaceEntity.cs @@ -0,0 +1,39 @@ +using System; +using Fake.DomainDrivenDesign.Entities.Auditing; + +namespace InterfaceForward.Domain.Aggregates.InterfaceAggregate; + +public class InterfaceEntity: FullAuditedEntity +{ + public int ServiceProviderId { get; private set; } + + public string Name { get; private set; } + + public ServiceType ServiceType { get; set; } + + public string Code { get; private set; } + + public string RequestAddress { get; private set; } + + public string RequestMethod { get; private set; } + + public RequestContentType RequestContentType { get; private set; } + + public ResponseContentType ResponseContentType { get; private set; } + + public string FlowCode { get; private set; } + + public int Timeout { get; private set; } = 20; + + /// + /// 接口单次请求数量上限,默认是1,表示不支持批量处理 + /// + public int Limit { get; private set; } = 1; + + public int Qps { get; private set; } + + /// + /// 映射后的数据类型 + /// + public ParameterType MappedType { get; set; } = ParameterType.Object; +} diff --git a/src/InterfaceForward.Domain/Enumerations/ParameterType.cs b/src/InterfaceForward.Domain/Enumerations/ParameterType.cs new file mode 100644 index 0000000..a542b7d --- /dev/null +++ b/src/InterfaceForward.Domain/Enumerations/ParameterType.cs @@ -0,0 +1,12 @@ +namespace InterfaceForward.Domain.Enumerations; + +public class ParameterType(int id, string name) : Enumeration(id, name) +{ + public static ParameterType String = new(1, "String"); + public static ParameterType Float = new(2, "Float"); + public static ParameterType Object = new(3, "Object"); + public static ParameterType Boolean = new(4, "Boolean"); + public static ParameterType Array = new(5, "Array"); + public static ParameterType Integer = new(6, "Integer"); + public static ParameterType Date = new(7, "Date"); +} \ No newline at end of file diff --git a/src/InterfaceForward.Domain/Enumerations/RequestContentType.cs b/src/InterfaceForward.Domain/Enumerations/RequestContentType.cs new file mode 100644 index 0000000..7e2d332 --- /dev/null +++ b/src/InterfaceForward.Domain/Enumerations/RequestContentType.cs @@ -0,0 +1,8 @@ +namespace InterfaceForward.Domain.Enumerations; + +public class RequestContentType(int id, string name) : Enumeration(id, name) +{ + public static RequestContentType Json = new(1, "application/json"); + public static RequestContentType Xml = new(2, "application/xml"); + public static RequestContentType FormUrlEncoded = new(3, "application/x-www-form-urlencoded"); +} \ No newline at end of file diff --git a/src/InterfaceForward.Domain/Enumerations/ResponseContentType.cs b/src/InterfaceForward.Domain/Enumerations/ResponseContentType.cs new file mode 100644 index 0000000..006bc4b --- /dev/null +++ b/src/InterfaceForward.Domain/Enumerations/ResponseContentType.cs @@ -0,0 +1,6 @@ +namespace InterfaceForward.Domain.Enumerations; + +public class ResponseContentType(int id, string name) : Enumeration(id, name) +{ + +} \ No newline at end of file diff --git a/src/InterfaceForward.Domain/Enumerations/ServiceType.cs b/src/InterfaceForward.Domain/Enumerations/ServiceType.cs new file mode 100644 index 0000000..727e0ff --- /dev/null +++ b/src/InterfaceForward.Domain/Enumerations/ServiceType.cs @@ -0,0 +1,7 @@ +namespace InterfaceForward.Domain.Enumerations; + +public class ServiceType(int id, string name) : Enumeration(id, name) +{ + public static ServiceType ServiceProvider = new(1, "服务商"); + public static ServiceType Platform = new(2, "平台"); +} \ No newline at end of file diff --git a/src/InterfaceForward.Domain/GlobalUsings.cs b/src/InterfaceForward.Domain/GlobalUsings.cs new file mode 100644 index 0000000..d6f38f7 --- /dev/null +++ b/src/InterfaceForward.Domain/GlobalUsings.cs @@ -0,0 +1,4 @@ +// Global using directives + +global using Fake.DomainDrivenDesign; +global using InterfaceForward.Domain.Enumerations; \ No newline at end of file diff --git a/src/InterfaceForward.Domain/InterfaceForward.Domain.csproj b/src/InterfaceForward.Domain/InterfaceForward.Domain.csproj new file mode 100644 index 0000000..db60c9b --- /dev/null +++ b/src/InterfaceForward.Domain/InterfaceForward.Domain.csproj @@ -0,0 +1,13 @@ + + + + + + netstandard2.0 + + + + + + + diff --git a/src/InterfaceForward.Domain/InterfaceForwardDomainModule.cs b/src/InterfaceForward.Domain/InterfaceForwardDomainModule.cs new file mode 100644 index 0000000..5b3852b --- /dev/null +++ b/src/InterfaceForward.Domain/InterfaceForwardDomainModule.cs @@ -0,0 +1,8 @@ +using System; + +namespace InterfaceForward.Domain +{ + public class InterfaceForwardDomainModule + { + } +} \ No newline at end of file diff --git a/src/InterfaceForward.ServiceDefaults/Extensions.cs b/src/InterfaceForward.ServiceDefaults/Extensions.cs new file mode 100644 index 0000000..6b236cd --- /dev/null +++ b/src/InterfaceForward.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 OpenTelemetry.Logs; +using OpenTelemetry.Metrics; +using OpenTelemetry.Trace; + +namespace Microsoft.Extensions.Hosting; + +public static class Extensions +{ + public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder) + { + 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.UseServiceDiscovery(); + }); + + return builder; + } + + public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder) + { + builder.Logging.AddOpenTelemetry(logging => + { + logging.IncludeFormattedMessage = true; + logging.IncludeScopes = true; + }); + + builder.Services.AddOpenTelemetry() + .WithMetrics(metrics => + { + metrics.AddRuntimeInstrumentation() + .AddBuiltInMeters(); + }) + .WithTracing(tracing => + { + if (builder.Environment.IsDevelopment()) + { + // We want to view all traces in development + tracing.SetSampler(new AlwaysOnSampler()); + } + + tracing.AddAspNetCoreInstrumentation() + .AddGrpcClientInstrumentation() + .AddHttpClientInstrumentation(); + }); + + builder.AddOpenTelemetryExporters(); + + return builder; + } + + private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder) + { + var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); + + if (useOtlpExporter) + { + builder.Services.Configure(logging => logging.AddOtlpExporter()); + builder.Services.ConfigureOpenTelemetryMeterProvider(metrics => metrics.AddOtlpExporter()); + builder.Services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddOtlpExporter()); + } + + // Uncomment the following lines to enable the Prometheus exporter (requires the OpenTelemetry.Exporter.Prometheus.AspNetCore package) + // builder.Services.AddOpenTelemetry() + // .WithMetrics(metrics => metrics.AddPrometheusExporter()); + + // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.Exporter package) + // builder.Services.AddOpenTelemetry() + // .UseAzureMonitor(); + + return builder; + } + + public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder) + { + 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) + { + // Uncomment the following line to enable the Prometheus endpoint (requires the OpenTelemetry.Exporter.Prometheus.AspNetCore package) + // app.MapPrometheusScrapingEndpoint(); + + // 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; + } + + private static MeterProviderBuilder AddBuiltInMeters(this MeterProviderBuilder meterProviderBuilder) => + meterProviderBuilder.AddMeter( + "Microsoft.AspNetCore.Hosting", + "Microsoft.AspNetCore.Server.Kestrel", + "System.Net.Http"); +} \ No newline at end of file diff --git a/src/InterfaceForward.ServiceDefaults/InterfaceForward.ServiceDefaults.csproj b/src/InterfaceForward.ServiceDefaults/InterfaceForward.ServiceDefaults.csproj new file mode 100644 index 0000000..97fe4f4 --- /dev/null +++ b/src/InterfaceForward.ServiceDefaults/InterfaceForward.ServiceDefaults.csproj @@ -0,0 +1,24 @@ + + + + Library + net8.0 + enable + enable + true + + + + + + + + + + + + + + + + diff --git a/src/InterfaceForward.sln b/src/InterfaceForward.sln new file mode 100644 index 0000000..32cf00b --- /dev/null +++ b/src/InterfaceForward.sln @@ -0,0 +1,30 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.0.0 +MinimumVisualStudioVersion = 17.8.0.0 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InterfaceForward.AppHost", "InterfaceForward.AppHost\InterfaceForward.AppHost.csproj", "{D4540BC1-77C8-407F-9009-CADE4E2A4CD9}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InterfaceForward.ServiceDefaults", "InterfaceForward.ServiceDefaults\InterfaceForward.ServiceDefaults.csproj", "{B0AC9CDE-3A63-4AE9-85A9-8854E9EB441E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D4540BC1-77C8-407F-9009-CADE4E2A4CD9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D4540BC1-77C8-407F-9009-CADE4E2A4CD9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D4540BC1-77C8-407F-9009-CADE4E2A4CD9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D4540BC1-77C8-407F-9009-CADE4E2A4CD9}.Release|Any CPU.Build.0 = Release|Any CPU + {B0AC9CDE-3A63-4AE9-85A9-8854E9EB441E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B0AC9CDE-3A63-4AE9-85A9-8854E9EB441E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B0AC9CDE-3A63-4AE9-85A9-8854E9EB441E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B0AC9CDE-3A63-4AE9-85A9-8854E9EB441E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B7073D5C-741D-4DC5-AFDB-C2C67E306A04} + EndGlobalSection +EndGlobal \ No newline at end of file