Add Gitea deploy workflow, compose stack and health endpoint
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7c39cb5562
commit
e9758499ff
13 files changed
+421
-2
No files matched your search
@@ -4,6 +4,8 @@ public sealed class HttpTrapOptions
|
||||
{
|
||||
public const string SectionName = "Flypaper:Http";
|
||||
|
||||
public const string HealthPath = "/health";
|
||||
|
||||
public string[] KnownHosts { get; set; } = [];
|
||||
|
||||
public string[] TrapPaths { get; set; } = [];
|
||||
|
||||
@@ -18,7 +18,7 @@ internal sealed class FlypaperMiddleware(
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
var request = context.Request;
|
||||
if (matcher.Match(request.Host.Host, request.Path.Value) == TrapMatch.None)
|
||||
if (IsInternalHealthProbe(context) || matcher.Match(request.Host.Host, request.Path.Value) == TrapMatch.None)
|
||||
{
|
||||
await next(context);
|
||||
return;
|
||||
@@ -46,6 +46,11 @@ internal sealed class FlypaperMiddleware(
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsInternalHealthProbe(HttpContext context) =>
|
||||
context.Request.Path.Equals(HttpTrapOptions.HealthPath, StringComparison.OrdinalIgnoreCase)
|
||||
&& context.Connection.RemoteIpAddress is { } remoteIp
|
||||
&& PrivateNetworks.Contains(remoteIp);
|
||||
|
||||
private static async ValueTask<ITrapChannel> OpenChannelAsync(
|
||||
HttpContext context,
|
||||
TrapSession session,
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.Net;
|
||||
|
||||
namespace Flypaper.Http.Middleware;
|
||||
|
||||
internal static class PrivateNetworks
|
||||
{
|
||||
private static readonly IPNetwork[] Networks =
|
||||
[
|
||||
IPNetwork.Parse("127.0.0.0/8"),
|
||||
IPNetwork.Parse("10.0.0.0/8"),
|
||||
IPNetwork.Parse("172.16.0.0/12"),
|
||||
IPNetwork.Parse("192.168.0.0/16"),
|
||||
IPNetwork.Parse("::1/128"),
|
||||
IPNetwork.Parse("fc00::/7")
|
||||
];
|
||||
|
||||
public static bool Contains(IPAddress address)
|
||||
{
|
||||
var ip = address.IsIPv4MappedToIPv6 ? address.MapToIPv4() : address;
|
||||
return Networks.Any(network => network.Contains(ip));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Flypaper.Http;
|
||||
using Flypaper.Storage;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Flypaper.Web.Health;
|
||||
|
||||
public static class HealthEndpoints
|
||||
{
|
||||
public static IEndpointRouteBuilder MapHealthEndpoint(this IEndpointRouteBuilder app)
|
||||
{
|
||||
app.MapGet(HttpTrapOptions.HealthPath, async (IDbContextFactory<FlypaperDbContext> dbContextFactory, CancellationToken ct) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
||||
await db.Settings.AnyAsync(ct);
|
||||
return Results.Text("healthy", "text/plain; charset=utf-8");
|
||||
}
|
||||
catch (SqliteException)
|
||||
{
|
||||
return Results.Text("database unavailable", "text/plain; charset=utf-8", statusCode: StatusCodes.Status503ServiceUnavailable);
|
||||
}
|
||||
}).AllowAnonymous();
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using Flypaper.Http;
|
||||
using Flypaper.Storage;
|
||||
using Flypaper.Web.Auth;
|
||||
using Flypaper.Web.Components;
|
||||
using Flypaper.Web.Health;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
@@ -57,6 +58,9 @@ app.UseAntiforgery();
|
||||
|
||||
app.MapStaticAssets().AllowAnonymous();
|
||||
app.MapAuthEndpoints();
|
||||
app.MapHealthEndpoint();
|
||||
app.MapRazorComponents<App>();
|
||||
|
||||
await app.RunAsync();
|
||||
|
||||
public partial class Program;
|
||||
Reference in new issue
Block a user