From ca210c5ea3e97d7a4e93f51794d1da7a6da40ebf Mon Sep 17 00:00:00 2001 From: whoswhip Date: Wed, 28 Jan 2026 07:53:58 -0500 Subject: [PATCH] feat: add output caching and stats endpoint to track pastes and users i plan to implement caching for all other endpoints but output cache doesn't have what i need right now --- SharpbinV3.Server/Program.cs | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/SharpbinV3.Server/Program.cs b/SharpbinV3.Server/Program.cs index e441edc..11a4a75 100644 --- a/SharpbinV3.Server/Program.cs +++ b/SharpbinV3.Server/Program.cs @@ -164,6 +164,14 @@ namespace SharpbinV3.Server }, cancellationToken: cancellationToken); }; }); + builder.Services.AddOutputCache(options => + { + options.AddPolicy("1Day", builder => + { + builder.Expire(TimeSpan.FromDays(1)); + }); + }); + builder.Services.AddCors(o => { o.AddDefaultPolicy(p => @@ -186,10 +194,13 @@ namespace SharpbinV3.Server } app.UseHttpsRedirection(); + app.UseCors(); app.UseAuthentication(); app.UseAuthorization(); + app.UseOutputCache(); + app.MapControllers(); using (var scope = app.Services.CreateScope()) @@ -213,6 +224,33 @@ namespace SharpbinV3.Server app.MapHealthChecks("/health"); + app.MapGet("/api/stats", async (AppDbContext db) => + { + var pasteCount = await db.Pastes.CountAsync(); + var userCount = await db.Users.CountAsync(); + var totalPasteSize = await db.Pastes.SumAsync(p => p.Size); + var pastSevenDays = DateTimeOffset.UtcNow.AddDays(-7).ToUnixTimeMilliseconds(); + var pasteCountWeek = await db.Pastes.CountAsync(p => p.CreatedAt >= pastSevenDays); + + return Results.Ok(new + { + success = true, + stats = new + { + pastes = new + { + total = pasteCount, + past7Days = pasteCountWeek, + totalSizeInBytes = totalPasteSize + }, + users = new + { + total = userCount + }, + } + }); + }).CacheOutput("1Day"); + app.Run(); } static DirectoryInfo GetDataProtectionKeyDirectory(IHostEnvironment env)