Added backend functionality
This commit is contained in:
83
WorkTime.Defaults/DataResult.cs
Normal file
83
WorkTime.Defaults/DataResult.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace WorkTime.Defaults;
|
||||
|
||||
public class DataResult<TResult, TError>
|
||||
where TResult : notnull
|
||||
where TError : notnull {
|
||||
public TResult? Content { get; }
|
||||
public TError? Error { get; }
|
||||
|
||||
public bool IsSuccessful => Error is null && Content is not null;
|
||||
|
||||
public IResult HttpResult => ToHttpResult(this);
|
||||
|
||||
public DataResult(TResult result) {
|
||||
Content = result;
|
||||
}
|
||||
|
||||
public DataResult(TError error) {
|
||||
Error = error;
|
||||
}
|
||||
|
||||
public static IResult ToHttpResult(DataResult<TResult, TError> dataResult) {
|
||||
if (dataResult.Content is bool content)
|
||||
return content ? Results.Ok() : Results.BadRequest();
|
||||
|
||||
if (dataResult.IsSuccessful)
|
||||
return Results.Ok(dataResult.Content);
|
||||
|
||||
if (dataResult.Error is ValidationProblemDetails validationProblem)
|
||||
return Results.ValidationProblem(
|
||||
validationProblem.Errors,
|
||||
validationProblem.Detail,
|
||||
validationProblem.Instance,
|
||||
validationProblem.Status,
|
||||
validationProblem.Title,
|
||||
validationProblem.Type);
|
||||
|
||||
if (dataResult.Error is ProblemDetails problem)
|
||||
return Results.Problem(problem);
|
||||
|
||||
if (typeof(TError).IsAssignableTo(typeof(IResult)))
|
||||
return dataResult.Error as IResult ?? Results.Problem();
|
||||
|
||||
return Results.Problem();
|
||||
}
|
||||
|
||||
public static implicit operator DataResult<TResult, TError>(TResult result) {
|
||||
return new DataResult<TResult, TError>(result);
|
||||
}
|
||||
|
||||
public static implicit operator DataResult<TResult, TError>(TError error) {
|
||||
return new DataResult<TResult, TError>(error);
|
||||
}
|
||||
}
|
||||
|
||||
public class DataResult<TResult> : DataResult<TResult, Exception> where TResult : notnull {
|
||||
public DataResult(TResult result) : base(result) { }
|
||||
public DataResult(Exception error) : base(error) { }
|
||||
|
||||
public static implicit operator DataResult<TResult>(TResult result) {
|
||||
return new DataResult<TResult>(result);
|
||||
}
|
||||
|
||||
public static implicit operator DataResult<TResult>(Exception error) {
|
||||
return new DataResult<TResult>(error);
|
||||
}
|
||||
}
|
||||
|
||||
public class DataResult : DataResult<bool> {
|
||||
public DataResult(bool result) : base(result) { }
|
||||
public DataResult(Exception error) : base(error) { }
|
||||
|
||||
public static implicit operator DataResult(bool result) {
|
||||
return new DataResult(result);
|
||||
}
|
||||
|
||||
public static implicit operator DataResult(Exception error) {
|
||||
return new DataResult(error);
|
||||
}
|
||||
}
|
||||
110
WorkTime.Defaults/Extensions.cs
Normal file
110
WorkTime.Defaults/Extensions.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
22
WorkTime.Defaults/WorkTime.Defaults.csproj
Normal file
22
WorkTime.Defaults/WorkTime.Defaults.csproj
Normal file
@@ -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>
|
||||
Reference in New Issue
Block a user