Skip to content

Commit

Permalink
Add basic health check bagetter#105 (bagetter#106)
Browse files Browse the repository at this point in the history
* add basic healthcheck feature bagetter#105

* make health endpoint configurable

* documentation

* remove newlines

---------

Co-authored-by: GALLIKER\pafor <[email protected]>
  • Loading branch information
Hechamon and Hechamon authored Mar 6, 2024
1 parent 081aadb commit d554b46
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 0 deletions.
18 changes: 18 additions & 0 deletions docs/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ If not specified, the `MaxRequestBodySize` in BaGetter defaults to 250MB (262144
}
```

## Health Endpoint

When running within a containerized environment like Kubernetes, a basic health endpoint is exposed at `/health` that returns 200 OK and the text "Healthy" when running.

This path is configurable if needed:

```json
{
...

"HealthCheck": {
"Path": "/healthz"
},

...
}
```

## Load secrets from files

Mostly useful when running containerised (e.g. using Docker, Podman, Kubernetes, etc), the application will look for files named in the same pattern as environment variables under `/run/secrets`.
Expand Down
2 changes: 2 additions & 0 deletions src/BaGetter.Core/Configuration/BaGetterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ public class BaGetterOptions
public SearchOptions Search { get; set; }

public MirrorOptions Mirror { get; set; }

public HealthCheckOptions HealthCheck { get; set; }
}
20 changes: 20 additions & 0 deletions src/BaGetter.Core/Configuration/HealthCheckOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace BaGetter.Core;

public class HealthCheckOptions : IValidatableObject
{
[Required]
public string Path { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (! Path.StartsWith('/'))
{
yield return new ValidationResult(
$"The {nameof(Path)} needs to start with a /",
new[] { nameof(Path) });
}
}
}
4 changes: 4 additions & 0 deletions src/BaGetter.Core/Validation/ValidateStartupOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ public class ValidateStartupOptions
private readonly IOptions<DatabaseOptions> _database;
private readonly IOptions<StorageOptions> _storage;
private readonly IOptions<MirrorOptions> _mirror;
private readonly IOptions<HealthCheckOptions> _healthCheck;
private readonly ILogger<ValidateStartupOptions> _logger;

public ValidateStartupOptions(
IOptions<BaGetterOptions> root,
IOptions<DatabaseOptions> database,
IOptions<StorageOptions> storage,
IOptions<MirrorOptions> mirror,
IOptions<HealthCheckOptions> healthCheck,
ILogger<ValidateStartupOptions> logger)
{
_root = root ?? throw new ArgumentNullException(nameof(root));
_database = database ?? throw new ArgumentNullException(nameof(database));
_storage = storage ?? throw new ArgumentNullException(nameof(storage));
_mirror = mirror ?? throw new ArgumentNullException(nameof(mirror));
_healthCheck = healthCheck ?? throw new ArgumentNullException(nameof(healthCheck));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

Expand All @@ -39,6 +42,7 @@ public bool Validate()
_ = _database.Value;
_ = _storage.Value;
_ = _mirror.Value;
_ = _healthCheck.Value;

return true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/BaGetter/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public void ConfigureServices(IServiceCollection services)

services.AddSingleton<IConfigureOptions<MvcRazorRuntimeCompilationOptions>, ConfigureRazorRuntimeCompilation>();

services.AddHealthChecks();

services.AddCors();
}

Expand Down Expand Up @@ -89,5 +91,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

baget.MapEndpoints(endpoints);
});

app.UseHealthChecks(options.HealthCheck.Path);
}
}
4 changes: 4 additions & 0 deletions src/BaGetter/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@
"Default": "Warning"
}
}
},

"HealthCheck": {
"Path" : "/health"
}
}

0 comments on commit d554b46

Please sign in to comment.