Skip to content

Commit

Permalink
Add HealthCheck library to check sql server and redis.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadKarimi committed Jan 11, 2024
1 parent b4adb28 commit 80274fa
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/SwiftLink.Application/Constants/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

internal static class Constants
{
public static class Database
{
public static string InsertFailed = "Insert into db is failed :(";

}

public static class Link
{
public static string UrlMustBeSent = "Url must be sent!";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<Result<object>> Handle(GenerateShortCodeCommand request, Cance

var dbResult = await _dbContext.SaveChangesAsync(cancellationToken);
if (dbResult.IsFailure)
return Result<object>.Failure();
return Result<object>.Failure(Constants.Database.InsertFailed);

await _cache.Set(request.Url, JsonSerializer.Serialize(link), link.ExpirationDate);
return Result<object>.Success(link);
Expand Down
19 changes: 18 additions & 1 deletion src/SwiftLink.Presentation/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using SwiftLink.Infrastructure;
using SwiftLink.Presentation;
using SwiftLink.Shared;
using HealthChecks;
using SwiftLink.Infrastructure.Persistence.Context;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;

var builder = WebApplication.CreateBuilder(args);
{
Expand Down Expand Up @@ -31,11 +34,16 @@
options.SubstituteApiVersionInUrl = true;
});
builder.Services.AddHealthChecks();

builder.Services
.AddHealthChecks()

.AddSqlServer(builder.Configuration.GetConnectionString(nameof(ApplicationDbContext)));
// .AddRedis(builder.Configuration["AppSettings:RedisCacheUrl"]);
}

var app = builder.Build();
{
app.MapHealthChecks("/health");
app.UseExceptionHandler(error =>
{
error.Run(async context =>
Expand All @@ -51,5 +59,14 @@
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.UseRouting()
.UseEndpoints(config =>
{
config.MapHealthChecks("/health", new HealthCheckOptions
{
Predicate = _ => true
//ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
});
app.Run();
}
5 changes: 4 additions & 1 deletion src/SwiftLink.Presentation/SwiftLink.Presentation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<Nullable>disable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
<UserSecretsId>9f22be05-027c-470e-b138-4656b265436d</UserSecretsId>
Expand All @@ -14,6 +14,9 @@
<ItemGroup>
<PackageReference Include="Asp.Versioning.Http" Version="8.0.0" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.0.0" />
<PackageReference Include="AspNetCore.HealthChecks.Redis" Version="8.0.0" />
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="8.0.0" />
<PackageReference Include="AspNetCore.HealthChecks.UI" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down

0 comments on commit 80274fa

Please sign in to comment.