Skip to content

Commit

Permalink
allow cors requests
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Feb 23, 2024
1 parent 4ee84f9 commit 8fd23bc
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions src/Nager.PublicSuffixService/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using Microsoft.Extensions.Caching.Memory;
using Nager.PublicSuffix;
using Nager.PublicSuffix.RuleProviders;
using Nager.PublicSuffix.RuleProviders.CacheProviders;
using Nager.PublicSuffix.WebApi.GitHub;
using System.Text.Json.Serialization;
using System.Web;

var corsPolicyName = "ApiPolicy";

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
Expand All @@ -14,14 +17,24 @@
builder.Services.AddSingleton<IRuleProvider, CachedHttpRuleProvider>();
builder.Services.AddSingleton<IDomainParser, DomainParser>();
builder.Services.AddScoped<GitHubClient>();
builder.Services.AddMemoryCache();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.Configure<Microsoft.AspNetCore.Http.Json.JsonOptions>(opt =>
builder.Services.AddCors(configuration =>
configuration.AddPolicy(corsPolicyName, builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
})
);

builder.Services.Configure<Microsoft.AspNetCore.Http.Json.JsonOptions>(configuration =>
{
opt.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
configuration.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
});

var app = builder.Build();
Expand All @@ -32,6 +45,8 @@
await ruleProvider.BuildAsync();
}

app.UseCors(corsPolicyName);

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
Expand All @@ -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<DateTime>("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<DateTime>("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();
})
Expand Down

0 comments on commit 8fd23bc

Please sign in to comment.