From 8fd23bce684cdba827b6cf6d80a954536d21f8f5 Mon Sep 17 00:00:00 2001 From: Tino Hager Date: Fri, 23 Feb 2024 11:03:06 +0100 Subject: [PATCH] allow cors requests --- src/Nager.PublicSuffixService/Program.cs | 41 +++++++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/src/Nager.PublicSuffixService/Program.cs b/src/Nager.PublicSuffixService/Program.cs index 1812256..5ead222 100644 --- a/src/Nager.PublicSuffixService/Program.cs +++ b/src/Nager.PublicSuffixService/Program.cs @@ -1,3 +1,4 @@ +using Microsoft.Extensions.Caching.Memory; using Nager.PublicSuffix; using Nager.PublicSuffix.RuleProviders; using Nager.PublicSuffix.RuleProviders.CacheProviders; @@ -5,6 +6,8 @@ using System.Text.Json.Serialization; using System.Web; +var corsPolicyName = "ApiPolicy"; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -14,14 +17,24 @@ builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddScoped(); +builder.Services.AddMemoryCache(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.Services.Configure(opt => +builder.Services.AddCors(configuration => + configuration.AddPolicy(corsPolicyName, builder => + { + builder.AllowAnyOrigin() + .AllowAnyMethod() + .AllowAnyHeader(); + }) +); + +builder.Services.Configure(configuration => { - opt.SerializerOptions.Converters.Add(new JsonStringEnumConverter()); + configuration.SerializerOptions.Converters.Add(new JsonStringEnumConverter()); }); var app = builder.Build(); @@ -32,6 +45,8 @@ await ruleProvider.BuildAsync(); } +app.UseCors(corsPolicyName); + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { @@ -49,17 +64,33 @@ .WithName("DomainInfo") .WithOpenApi(); -app.MapPost("/CheckLastCommit", async (GitHubClient gitHubClient, CancellationToken cancellationToken) => +app.MapPost("/CheckLastCommit", async (GitHubClient gitHubClient, IMemoryCache memoryCache, CancellationToken cancellationToken) => { + memoryCache.TryGetValue("lastCheckTime", out var lastCheckTime); + if (lastCheckTime > DateTime.UtcNow.AddMinutes(-1)) + { + return Results.StatusCode(StatusCodes.Status429TooManyRequests); + } + + memoryCache.Set("lastCheckTime", DateTime.UtcNow); + var lastGitHubCommit = await gitHubClient.GetCommitAsync("publicsuffix", "list", "master", cancellationToken); - return lastGitHubCommit?.Commit?.Committer?.Date; + return Results.Content(lastGitHubCommit?.Commit?.Committer?.Date?.ToString()); }) .WithName("CheckLastCommit") .WithOpenApi(); -app.MapPost("/UpdateRules", async (IRuleProvider ruleProvider) => +app.MapPost("/UpdateRules", async (IRuleProvider ruleProvider, IMemoryCache memoryCache) => { + memoryCache.TryGetValue("lastUpdateTime", out var lastUpdateTime); + if (lastUpdateTime > DateTime.UtcNow.AddMinutes(-10)) + { + return Results.StatusCode(StatusCodes.Status429TooManyRequests); + } + + memoryCache.Set("lastUpdateTime", DateTime.UtcNow); + await ruleProvider.BuildAsync(ignoreCache: true); return Results.NoContent(); })