mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
feat(api): config Cache-Control
This commit is contained in:
parent
d2ba27c712
commit
2d08fb07ce
3 changed files with 45 additions and 9 deletions
|
|
@ -0,0 +1,26 @@
|
|||
using Microsoft.Net.Http.Headers;
|
||||
|
||||
namespace FilterLists.Directory.Api;
|
||||
|
||||
internal sealed class CacheControlFilter(int clientCacheSeconds, int cdnCacheSeconds) : IEndpointFilter
|
||||
{
|
||||
private readonly string _cacheControlHeaderValue = new CacheControlHeaderValue
|
||||
{
|
||||
Public = true,
|
||||
MaxAge = TimeSpan.FromSeconds(clientCacheSeconds),
|
||||
SharedMaxAge = TimeSpan.FromSeconds(cdnCacheSeconds)
|
||||
}
|
||||
.ToString();
|
||||
|
||||
public async ValueTask<object?> InvokeAsync(
|
||||
EndpointFilterInvocationContext context,
|
||||
EndpointFilterDelegate next)
|
||||
{
|
||||
var result = await next(context);
|
||||
|
||||
if (context.HttpContext.Response.StatusCode is >= 200 and < 300)
|
||||
context.HttpContext.Response.Headers.CacheControl = _cacheControlHeaderValue;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
@ -8,6 +8,8 @@ namespace FilterLists.Directory.Api;
|
|||
|
||||
internal static class Endpoints
|
||||
{
|
||||
private static readonly CacheControlFilter DefaultGetCacheFilter = new(3600, 86400);
|
||||
|
||||
internal static void MapEndpoints(this WebApplication app)
|
||||
{
|
||||
app.MapGet("/languages", async (GetLanguages.Query query, CancellationToken ct) => await query.ExecuteAsync(ct))
|
||||
|
|
@ -17,7 +19,8 @@ internal static void MapEndpoints(this WebApplication app)
|
|||
Tags = [LanguagesTag],
|
||||
Summary = "Gets the languages targeted by the FilterLists.",
|
||||
OperationId = nameof(GetLanguages)
|
||||
});
|
||||
})
|
||||
.AddEndpointFilter(DefaultGetCacheFilter);
|
||||
|
||||
app.MapGet("/licenses", async (GetLicenses.Query query, CancellationToken ct) => await query.ExecuteAsync(ct))
|
||||
.Produces<List<GetLicenses.Response>>()
|
||||
|
|
@ -26,7 +29,8 @@ internal static void MapEndpoints(this WebApplication app)
|
|||
Tags = [LicensesTag],
|
||||
Summary = "Gets the licenses applied to the FilterLists.",
|
||||
OperationId = nameof(GetLicenses)
|
||||
});
|
||||
})
|
||||
.AddEndpointFilter(DefaultGetCacheFilter);
|
||||
|
||||
app.MapGet("/lists", async (GetLists.Query query, CancellationToken ct) => await query.ExecuteAsync(ct))
|
||||
.Produces<List<GetLists.Response>>()
|
||||
|
|
@ -35,7 +39,8 @@ internal static void MapEndpoints(this WebApplication app)
|
|||
Tags = [FilterListsTag],
|
||||
Summary = "Gets the FilterLists.",
|
||||
OperationId = nameof(GetLists)
|
||||
});
|
||||
})
|
||||
.AddEndpointFilter(DefaultGetCacheFilter);
|
||||
|
||||
app.MapGet("/lists/{id:int}",
|
||||
async Task<Results<Ok<GetListDetails.Response>, NotFound>>
|
||||
|
|
@ -64,7 +69,8 @@ internal static void MapEndpoints(this WebApplication app)
|
|||
Example = new OpenApiInteger(1)
|
||||
}
|
||||
]
|
||||
});
|
||||
})
|
||||
.AddEndpointFilter(DefaultGetCacheFilter);
|
||||
|
||||
app.MapGet("/maintainers",
|
||||
async (GetMaintainers.Query query, CancellationToken ct) => await query.ExecuteAsync(ct))
|
||||
|
|
@ -74,7 +80,8 @@ internal static void MapEndpoints(this WebApplication app)
|
|||
Tags = [MaintainersTag],
|
||||
Summary = "Gets the maintainers of the FilterLists.",
|
||||
OperationId = nameof(GetMaintainers)
|
||||
});
|
||||
})
|
||||
.AddEndpointFilter(DefaultGetCacheFilter);
|
||||
|
||||
app.MapGet("/software", async (GetSoftware.Query query, CancellationToken ct) => await query.ExecuteAsync(ct))
|
||||
.Produces<List<GetSoftware.Response>>()
|
||||
|
|
@ -83,7 +90,8 @@ internal static void MapEndpoints(this WebApplication app)
|
|||
Tags = [SoftwareTag],
|
||||
Summary = "Gets the software that subscribes to the FilterLists.",
|
||||
OperationId = nameof(GetSoftware)
|
||||
});
|
||||
})
|
||||
.AddEndpointFilter(DefaultGetCacheFilter);
|
||||
|
||||
app.MapGet("/syntaxes", async (GetSyntaxes.Query query, CancellationToken ct) => await query.ExecuteAsync(ct))
|
||||
.Produces<List<GetSyntaxes.Response>>()
|
||||
|
|
@ -92,7 +100,8 @@ internal static void MapEndpoints(this WebApplication app)
|
|||
Tags = [SyntaxesTag],
|
||||
Summary = "Gets the syntaxes of the FilterLists.",
|
||||
OperationId = nameof(GetSyntaxes)
|
||||
});
|
||||
})
|
||||
.AddEndpointFilter(DefaultGetCacheFilter);
|
||||
|
||||
app.MapGet("/tags", async (GetTags.Query query, CancellationToken ct) => await query.ExecuteAsync(ct))
|
||||
.Produces<List<GetTags.Response>>()
|
||||
|
|
@ -101,6 +110,7 @@ internal static void MapEndpoints(this WebApplication app)
|
|||
Tags = [TagsTag],
|
||||
Summary = "Gets the tags of the FilterLists.",
|
||||
OperationId = nameof(GetTags)
|
||||
});
|
||||
})
|
||||
.AddEndpointFilter(DefaultGetCacheFilter);
|
||||
}
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Aspire.Microsoft.EntityFrameworkCore.SqlServer" Version="8.2.2"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Hybrid" Version="9.5.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Hybrid" Version="9.5.0"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
Reference in a new issue