Compare commits

..

6 Commits

Author SHA1 Message Date
f5dbc46856 build: add healthcheck for docker
All checks were successful
Build and Deploy Docker Container / build-and-deploy (push) Successful in 5m14s
2024-10-25 02:37:28 +03:00
ebec0a2d2b fix: remove request to /health from log 2024-10-25 02:37:08 +03:00
4fc28378c5 feat: add healthcheck for main project 2024-10-25 02:36:39 +03:00
98ee3c389c feat: add healthcheck for databases 2024-10-25 02:35:36 +03:00
428c2dc3ba refactor: return the modified interfaces for further modification 2024-10-25 02:22:42 +03:00
4970dd782a build: update ref 2024-10-25 01:54:56 +03:00
10 changed files with 83 additions and 53 deletions

View File

@ -3,6 +3,9 @@ LABEL company="Winsomnia"
LABEL maintainer.name="Wesser" maintainer.email="support@winsomnia.net"
WORKDIR /app
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl --fail http://localhost:8080/health || exit 1
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY . .

View File

@ -5,9 +5,9 @@ namespace Mirea.Api.Endpoint.Configuration.Core.Startup;
public static class ApiVersioningConfiguration
{
public static void AddCustomApiVersioning(this IServiceCollection services)
public static IApiVersioningBuilder AddCustomApiVersioning(this IServiceCollection services)
{
services.AddApiVersioning(options =>
return services.AddApiVersioning(options =>
{
options.DefaultApiVersion = new ApiVersion(1, 0);
options.AssumeDefaultVersionWhenUnspecified = true;

View File

@ -7,17 +7,19 @@ namespace Mirea.Api.Endpoint.Configuration.Core.Startup;
public static class CacheConfiguration
{
public static IServiceCollection AddCustomRedis(this IServiceCollection services, IConfiguration configuration)
public static IServiceCollection AddCustomRedis(this IServiceCollection services, IConfiguration configuration, IHealthChecksBuilder? healthChecksBuilder = null)
{
var cache = configuration.Get<GeneralConfig>()?.CacheSettings;
if (cache?.TypeDatabase == CacheSettings.CacheEnum.Redis)
{
if (cache?.TypeDatabase != CacheSettings.CacheEnum.Redis)
return services;
services.AddStackExchangeRedisCache(options =>
{
options.Configuration = cache.ConnectionString;
options.InstanceName = "mirea_";
});
}
healthChecksBuilder?.AddRedis(cache.ConnectionString!, name: "Redis");
return services;
}

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
@ -11,7 +12,7 @@ namespace Mirea.Api.Endpoint.Configuration.Core.Startup;
public static class JwtConfiguration
{
public static IServiceCollection AddJwtToken(this IServiceCollection services, IConfiguration configuration)
public static AuthenticationBuilder AddJwtToken(this IServiceCollection services, IConfiguration configuration)
{
var lifeTimeJwt = TimeSpan.FromMinutes(int.Parse(configuration["SECURITY_LIFE_TIME_JWT"]!));
@ -40,7 +41,7 @@ public static class JwtConfiguration
SigningKey = jwtKey
});
services.AddAuthentication(options =>
return services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
@ -60,7 +61,5 @@ public static class JwtConfiguration
TokenDecryptionKey = new SymmetricSecurityKey(jwtDecrypt)
};
});
return services;
}
}

View File

@ -15,7 +15,7 @@ public static class LoggerConfiguration
{
public static IHostBuilder AddCustomSerilog(this IHostBuilder hostBuilder)
{
hostBuilder.UseSerilog((context, _, configuration) =>
return hostBuilder.UseSerilog((context, _, configuration) =>
{
var generalConfig = context.Configuration.Get<GeneralConfig>()?.LogSettings;
configuration
@ -51,21 +51,25 @@ public static class LoggerConfiguration
configuration.Filter.ByExcluding(Matching.WithProperty<string>("SourceContext", sc =>
sc.Contains("Microsoft.EntityFrameworkCore.Database.Command")));
});
return hostBuilder;
}
public static IApplicationBuilder UseCustomSerilog(this IApplicationBuilder app)
{
app.UseSerilogRequestLogging(options =>
return app.UseSerilogRequestLogging(options =>
{
options.MessageTemplate = "[{RequestMethod}] {RequestPath} [Client {RemoteIPAddress}] [{StatusCode}] in {Elapsed:0.0000} ms";
options.GetLevel = (_, elapsed, ex) => elapsed >= 2500 || ex != null
options.GetLevel = (httpContext, elapsed, ex) =>
{
if (httpContext.Request.Path.StartsWithSegments("/health"))
return LogEventLevel.Verbose;
return elapsed >= 2500 || ex != null
? LogEventLevel.Warning
: elapsed >= 1000
? LogEventLevel.Information
: LogEventLevel.Debug;
};
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
{
@ -75,7 +79,5 @@ public static class LoggerConfiguration
diagnosticContext.Set("RemoteIPAddress", httpContext.Connection.RemoteIpAddress?.ToString());
};
});
return app;
}
}

View File

@ -51,15 +51,13 @@ public static class SwaggerConfiguration
options.IncludeXmlComments(Path.Combine(basePath, "ApiDtoDocs.xml"));
});
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
return services;
return services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
}
public static IApplicationBuilder UseCustomSwagger(this IApplicationBuilder app, IServiceProvider services)
{
app.UseSwagger();
app.UseSwaggerUI(options =>
return app.UseSwaggerUI(options =>
{
options.InjectStylesheet($"{UrlHelper.GetSubPath}css/swagger/SwaggerDark.css");
var provider = services.GetService<IApiVersionDescriptionProvider>();
@ -72,7 +70,5 @@ public static class SwaggerConfiguration
options.RoutePrefix = UrlHelper.GetSubPathSwagger.Trim('/');
}
});
return app;
}
}

View File

@ -5,9 +5,9 @@
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<Company>Winsomnia</Company>
<Version>1.0.0-rc2</Version>
<AssemblyVersion>1.0.2.2</AssemblyVersion>
<FileVersion>1.0.2.2</FileVersion>
<Version>1.0-rc3</Version>
<AssemblyVersion>1.0.2.3</AssemblyVersion>
<FileVersion>1.0.2.3</FileVersion>
<AssemblyName>Mirea.Api.Endpoint</AssemblyName>
<RootNamespace>$(AssemblyName)</RootNamespace>
<OutputType>Exe</OutputType>
@ -24,8 +24,10 @@
<ItemGroup>
<PackageReference Include="Asp.Versioning.Mvc" Version="8.1.0" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
<PackageReference Include="AspNetCore.HealthChecks.Redis" Version="8.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.System" Version="8.0.1" />
<PackageReference Include="Cronos" Version="0.8.4" />
<PackageReference Include="EPPlus" Version="7.4.0" />
<PackageReference Include="EPPlus" Version="7.4.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.11.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.11.0" />
@ -39,21 +41,22 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="8.1.2" />
<PackageReference Include="Mirea.Tools.Schedule.WebParser" Version="1.0.3" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.2" />
<PackageReference Include="Mirea.Tools.Schedule.WebParser" Version="1.0.4" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.3" />
<PackageReference Include="Serilog.Formatting.Compact" Version="3.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.Debug" Version="3.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.10" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.8.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0" />
<PackageReference Include="System.CodeDom" Version="8.0.0" />
<PackageReference Include="System.Composition" Version="8.0.0" />
<PackageReference Include="System.Composition.TypedParts" Version="8.0.0" />
<PackageReference Include="System.Drawing.Common" Version="8.0.10" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.1.2" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
<PackageReference Include="System.Threading.Channels" Version="8.0.0" />
<PackageReference Include="Z.EntityFramework.Extensions.EFCore" Version="8.103.5" />
<PackageReference Include="Z.EntityFramework.Extensions.EFCore" Version="8.103.6" />
</ItemGroup>
<ItemGroup>

View File

@ -23,7 +23,7 @@ namespace Mirea.Api.Endpoint;
public class Program
{
public static IServiceCollection AddDatabase(IServiceCollection services, IConfiguration configuration)
public static IServiceCollection AddDatabase(IServiceCollection services, IConfiguration configuration, IHealthChecksBuilder? healthCheckBuilder = null)
{
var dbSettings = configuration.Get<GeneralConfig>()?.DbSettings;
services.AddApplication();
@ -31,6 +31,10 @@ public class Program
dbSettings?.DatabaseProvider ?? DatabaseProvider.Sqlite,
dbSettings?.ConnectionStringSql ?? string.Empty);
healthCheckBuilder?.AddDatabaseHealthCheck(
dbSettings?.DatabaseProvider ?? DatabaseProvider.Sqlite,
dbSettings?.ConnectionStringSql ?? string.Empty);
return services;
}
@ -40,13 +44,18 @@ public class Program
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddConfiguration(EnvironmentConfiguration.GetEnvironment());
var healthCheckBuilder = builder.Services.AddHealthChecks();
builder.Configuration.AddJsonFile(GeneralConfig.FilePath, optional: true, reloadOnChange: true);
builder.Services.Configure<GeneralConfig>(builder.Configuration);
healthCheckBuilder.AddFile(x => x.AddFile(GeneralConfig.FilePath), name: nameof(GeneralConfig));
builder.Configuration.AddJsonFile(Admin.FilePath, optional: true, reloadOnChange: true);
builder.Services.Configure<Admin>(builder.Configuration);
healthCheckBuilder.AddFile(x => x.AddFile(Admin.FilePath), name: nameof(Admin));
builder.Host.AddCustomSerilog();
AddDatabase(builder.Services, builder.Configuration);
AddDatabase(builder.Services, builder.Configuration, healthCheckBuilder);
builder.Services.AddControllers();
@ -55,7 +64,7 @@ public class Program
builder.Services.AddSingleton<ISetupToken, SetupTokenService>();
builder.Services.AddMemoryCache();
builder.Services.AddCustomRedis(builder.Configuration);
builder.Services.AddCustomRedis(builder.Configuration, healthCheckBuilder);
builder.Services.AddCors(options =>
{
@ -108,6 +117,7 @@ public class Program
app.UseStaticFiles(UrlHelper.GetSubPath.TrimEnd('/'));
app.UseCors("AllowAll");
app.UseCustomSerilog();
app.MapHealthChecks("/health");
using (var scope = app.Services.CreateScope())
{

View File

@ -86,4 +86,15 @@ public static class DependencyInjection
};
}
}
public static IHealthChecksBuilder AddDatabaseHealthCheck(this IHealthChecksBuilder healthChecksBuilder, DatabaseProvider dbProvider, string connection)
{
return dbProvider switch
{
DatabaseProvider.Mysql => healthChecksBuilder.AddMySql(connection, name: "MySql"),
DatabaseProvider.Sqlite => healthChecksBuilder.AddSqlite(connection, name: "Sqlite"),
DatabaseProvider.Postgresql => healthChecksBuilder.AddNpgSql(connection, name: "PostgreSQL"),
_ => throw new ArgumentException("Unsupported database provider", Enum.GetName(dbProvider))
};
}
}

View File

@ -5,17 +5,21 @@
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<Company>Winsomnia</Company>
<Version>1.0.0</Version>
<AssemblyVersion>1.0.3.0</AssemblyVersion>
<FileVersion>1.0.3.0</FileVersion>
<Version>1.0.1</Version>
<AssemblyVersion>1.0.3.1</AssemblyVersion>
<FileVersion>1.0.3.1</FileVersion>
<AssemblyName>Mirea.Api.DataAccess.Persistence</AssemblyName>
<RootNamespace>$(AssemblyName)</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.MySql" Version="8.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="8.0.2" />
<PackageReference Include="AspNetCore.HealthChecks.Sqlite" Version="8.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.10" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.8" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="8.0.10" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.10" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
</ItemGroup>