mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
fix(dir): 🐛 allow CORS for localhost web
This commit is contained in:
parent
c4cb6887fd
commit
fcc4172782
5 changed files with 79 additions and 65 deletions
|
|
@ -0,0 +1,23 @@
|
|||
using Microsoft.AspNetCore.Cors.Infrastructure;
|
||||
|
||||
namespace FilterLists.Directory.Api;
|
||||
|
||||
internal static class CorsConfiguration
|
||||
{
|
||||
private const string ProductionClientOrigin = "https://filterlists.com";
|
||||
private const string StagingClientOriginStart = "https://nice-water-05873140f";
|
||||
private const string StagingClientOriginEnd = ".eastus2.5.azurestaticapps.net";
|
||||
private const string LocalClientOrigin = "http://localhost:3000";
|
||||
|
||||
internal static readonly Action<CorsOptions> SetupAction = options => options.AddDefaultPolicy(policy =>
|
||||
{
|
||||
policy.SetIsOriginAllowed(origin => origin switch
|
||||
{
|
||||
ProductionClientOrigin or LocalClientOrigin => true,
|
||||
_ => origin.StartsWith(StagingClientOriginStart, StringComparison.InvariantCulture) &&
|
||||
origin.EndsWith(StagingClientOriginEnd, StringComparison.InvariantCulture)
|
||||
})
|
||||
.WithMethods("GET")
|
||||
.AllowAnyHeader();
|
||||
});
|
||||
}
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
using Microsoft.OpenApi.Models;
|
||||
using ConfigurationExtensions = FilterLists.Directory.Application.ConfigurationExtensions;
|
||||
|
||||
namespace FilterLists.Directory.Api.OpenApi;
|
||||
|
||||
internal static class OpenApiConfigurationExtensions
|
||||
{
|
||||
internal static void AddOpenApiGen(this IServiceCollection services)
|
||||
{
|
||||
services.AddEndpointsApiExplorer();
|
||||
services.AddSwaggerGen(options =>
|
||||
{
|
||||
options.SwaggerDoc(
|
||||
"v1",
|
||||
new OpenApiInfo
|
||||
{
|
||||
Title = "FilterLists Directory API",
|
||||
Description = "An ASP.NET Core API serving the core FilterList information.",
|
||||
Version = "v1",
|
||||
TermsOfService =
|
||||
new Uri("https://github.com/collinbarrett/FilterLists/blob/main/.github/CODE_OF_CONDUCT.md"),
|
||||
Contact = new OpenApiContact { Name = "FilterLists", Url = new Uri("https://filterlists.com") },
|
||||
License = new OpenApiLicense
|
||||
{
|
||||
Name = "MIT License",
|
||||
Url = new Uri("https://github.com/collinbarrett/FilterLists/blob/main/LICENSE")
|
||||
}
|
||||
});
|
||||
|
||||
// include view model xml comments
|
||||
var xmlFilename = $"{typeof(ConfigurationExtensions).Assembly.GetName().Name}.xml";
|
||||
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
|
||||
|
||||
// include OpenApiTag Description and ExternalDocs
|
||||
options.DocumentFilter<OpenApiTags.TagDescriptionsDocumentFilter>();
|
||||
|
||||
// allow re-using simple type names like "Response"
|
||||
options.CustomSchemaIds(s => s.FullName?.Replace("+", "."));
|
||||
});
|
||||
}
|
||||
|
||||
internal static void UseSwaggerCustom(this IApplicationBuilder app)
|
||||
{
|
||||
app.UseSwagger(o => o.RouteTemplate = "{documentName}/openapi.json");
|
||||
app.UseSwaggerUI(options =>
|
||||
{
|
||||
options.DocumentTitle = "FilterLists API";
|
||||
options.SwaggerEndpoint("/v1/openapi.json", "FilterLists Directory API v1");
|
||||
options.RoutePrefix = string.Empty;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
using ConfigurationExtensions = FilterLists.Directory.Application.ConfigurationExtensions;
|
||||
|
||||
namespace FilterLists.Directory.Api.OpenApi;
|
||||
|
||||
internal static class OpenApiGenConfiguration
|
||||
{
|
||||
internal static readonly Action<SwaggerGenOptions> SetupAction = options =>
|
||||
{
|
||||
options.SwaggerDoc(
|
||||
"v1",
|
||||
new OpenApiInfo
|
||||
{
|
||||
Title = "FilterLists Directory API",
|
||||
Description = "An ASP.NET Core API serving the core FilterList information.",
|
||||
Version = "v1",
|
||||
TermsOfService =
|
||||
new Uri("https://github.com/collinbarrett/FilterLists/blob/main/.github/CODE_OF_CONDUCT.md"),
|
||||
Contact = new OpenApiContact { Name = "FilterLists", Url = new Uri("https://filterlists.com") },
|
||||
License = new OpenApiLicense
|
||||
{
|
||||
Name = "MIT License",
|
||||
Url = new Uri("https://github.com/collinbarrett/FilterLists/blob/main/LICENSE")
|
||||
}
|
||||
});
|
||||
|
||||
// include view model xml comments
|
||||
var xmlFilename = $"{typeof(ConfigurationExtensions).Assembly.GetName().Name}.xml";
|
||||
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
|
||||
|
||||
// include OpenApiTag Description and ExternalDocs
|
||||
options.DocumentFilter<OpenApiTags.TagDescriptionsDocumentFilter>();
|
||||
|
||||
// allow re-using simple type names like "Response"
|
||||
options.CustomSchemaIds(s => s.FullName?.Replace("+", "."));
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
using Swashbuckle.AspNetCore.SwaggerUI;
|
||||
|
||||
namespace FilterLists.Directory.Api.OpenApi;
|
||||
|
||||
internal static class SwaggerUiConfiguration
|
||||
{
|
||||
internal static readonly Action<SwaggerUIOptions> SetupAction = options =>
|
||||
{
|
||||
options.DocumentTitle = "FilterLists API";
|
||||
options.SwaggerEndpoint("/v1/openapi.json", "FilterLists Directory API v1");
|
||||
options.RoutePrefix = string.Empty;
|
||||
};
|
||||
}
|
||||
|
|
@ -4,21 +4,12 @@
|
|||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddCors(options => options.AddDefaultPolicy(policy =>
|
||||
{
|
||||
policy.SetIsOriginAllowed(origin =>
|
||||
{
|
||||
if (origin == "https://filterlists.com") return true;
|
||||
return origin.StartsWith("https://nice-water-05873140f", StringComparison.InvariantCulture) &&
|
||||
origin.EndsWith(".eastus2.5.azurestaticapps.net", StringComparison.InvariantCulture);
|
||||
})
|
||||
.WithMethods("GET")
|
||||
.AllowAnyHeader();
|
||||
}));
|
||||
builder.WebHost.ConfigureKestrel(serverOptions => serverOptions.AddServerHeader = false);
|
||||
builder.AddServiceDefaults();
|
||||
builder.Services.AddCors(CorsConfiguration.SetupAction);
|
||||
builder.Services.AddProblemDetails();
|
||||
builder.Services.AddOpenApiGen();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(OpenApiGenConfiguration.SetupAction);
|
||||
builder.AddApplication();
|
||||
|
||||
var app = builder.Build();
|
||||
|
|
@ -27,6 +18,7 @@
|
|||
app.UseCors();
|
||||
app.MapEndpoints();
|
||||
app.MapDefaultEndpoints();
|
||||
app.UseSwaggerCustom();
|
||||
app.UseSwagger(o => o.RouteTemplate = "{documentName}/openapi.json");
|
||||
app.UseSwaggerUI(SwaggerUiConfiguration.SetupAction);
|
||||
|
||||
app.Run();
|
||||
Loading…
Reference in a new issue