mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
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
This commit is contained in:
@@ -164,6 +164,14 @@ namespace SharpbinV3.Server
|
|||||||
}, cancellationToken: cancellationToken);
|
}, cancellationToken: cancellationToken);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
builder.Services.AddOutputCache(options =>
|
||||||
|
{
|
||||||
|
options.AddPolicy("1Day", builder =>
|
||||||
|
{
|
||||||
|
builder.Expire(TimeSpan.FromDays(1));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
builder.Services.AddCors(o =>
|
builder.Services.AddCors(o =>
|
||||||
{
|
{
|
||||||
o.AddDefaultPolicy(p =>
|
o.AddDefaultPolicy(p =>
|
||||||
@@ -186,10 +194,13 @@ namespace SharpbinV3.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
|
app.UseCors();
|
||||||
|
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.UseOutputCache();
|
||||||
|
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
|
|
||||||
using (var scope = app.Services.CreateScope())
|
using (var scope = app.Services.CreateScope())
|
||||||
@@ -213,6 +224,33 @@ namespace SharpbinV3.Server
|
|||||||
|
|
||||||
app.MapHealthChecks("/health");
|
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();
|
app.Run();
|
||||||
}
|
}
|
||||||
static DirectoryInfo GetDataProtectionKeyDirectory(IHostEnvironment env)
|
static DirectoryInfo GetDataProtectionKeyDirectory(IHostEnvironment env)
|
||||||
|
|||||||
Reference in New Issue
Block a user