2024-03-02 03:21:59 +00:00
|
|
|
using Asp.Versioning;
|
2024-03-02 03:46:29 +00:00
|
|
|
using Asp.Versioning.ApiExplorer;
|
|
|
|
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
|
|
|
using OliverBooth.Api;
|
2024-03-02 03:21:59 +00:00
|
|
|
using OliverBooth.Common.Extensions;
|
|
|
|
using Serilog;
|
2024-03-02 03:46:29 +00:00
|
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
2024-03-02 03:21:59 +00:00
|
|
|
|
|
|
|
Log.Logger = new LoggerConfiguration()
|
|
|
|
.WriteTo.Console()
|
|
|
|
.WriteTo.File("logs/latest.log", rollingInterval: RollingInterval.Day)
|
|
|
|
#if DEBUG
|
|
|
|
.MinimumLevel.Debug()
|
|
|
|
#endif
|
|
|
|
.CreateLogger();
|
|
|
|
|
|
|
|
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Configuration.AddTomlFile("data/config.toml", true, true);
|
|
|
|
builder.Logging.ClearProviders();
|
|
|
|
builder.Logging.AddSerilog();
|
|
|
|
|
|
|
|
builder.Services.AddCors(options =>
|
|
|
|
{
|
2024-03-02 05:25:42 +00:00
|
|
|
options.AddPolicy("localhost", policy => policy.WithOrigins("https://localhost:2843", "https://localhost:2845"));
|
|
|
|
options.AddPolicy("site", policy => policy.WithOrigins("https://admin.oliverbooth.dev", "https://oliverbooth.dev"));
|
2024-03-02 03:21:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
builder.Services.AddCommonServices();
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
2024-03-02 03:46:29 +00:00
|
|
|
builder.Services.AddSwaggerGen(options => options.ResolveConflictingActions(resolver =>
|
|
|
|
{
|
|
|
|
foreach (ApiDescription description in resolver)
|
|
|
|
{
|
|
|
|
if (description.GetApiVersion()?.MajorVersion == 2)
|
|
|
|
{
|
|
|
|
return description;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}));
|
2024-03-02 03:21:59 +00:00
|
|
|
builder.Services.AddControllers();
|
|
|
|
builder.Services.AddRouting(options => options.LowercaseUrls = true);
|
|
|
|
builder.Services.AddApiVersioning(options =>
|
|
|
|
{
|
|
|
|
options.AssumeDefaultVersionWhenUnspecified = true;
|
2024-03-02 03:46:29 +00:00
|
|
|
options.DefaultApiVersion = new ApiVersion(2);
|
2024-03-02 03:21:59 +00:00
|
|
|
options.ReportApiVersions = true;
|
|
|
|
options.ApiVersionReader = new UrlSegmentApiVersionReader();
|
2024-03-02 03:46:29 +00:00
|
|
|
}).AddApiExplorer(options =>
|
|
|
|
{
|
|
|
|
options.GroupNameFormat = "'v'VVV";
|
|
|
|
options.SubstituteApiVersionInUrl = true;
|
2024-03-02 03:21:59 +00:00
|
|
|
});
|
|
|
|
|
2024-03-02 03:46:29 +00:00
|
|
|
builder.Services.ConfigureOptions<ConfigureSwaggerOptions>();
|
|
|
|
|
2024-03-02 03:21:59 +00:00
|
|
|
if (builder.Environment.IsProduction())
|
|
|
|
{
|
|
|
|
builder.WebHost.AddCertificateFromEnvironment(2844, 5048);
|
|
|
|
}
|
|
|
|
|
|
|
|
WebApplication app = builder.Build();
|
|
|
|
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
{
|
|
|
|
app.UseSwagger();
|
2024-03-02 03:46:29 +00:00
|
|
|
app.UseSwaggerUI(options =>
|
|
|
|
{
|
|
|
|
var provider = app.Services.GetRequiredService<IApiVersionDescriptionProvider>();
|
|
|
|
foreach (ApiVersionDescription description in provider.ApiVersionDescriptions)
|
|
|
|
{
|
|
|
|
var url = $"/swagger/{description.GroupName}/swagger.json";
|
|
|
|
options.SwaggerEndpoint(url, description.GroupName.ToUpperInvariant());
|
|
|
|
}
|
|
|
|
});
|
2024-03-02 03:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.MapControllers();
|
|
|
|
app.UseCors(app.Environment.IsDevelopment() ? "localhost" : "site");
|
|
|
|
|
|
|
|
app.Run();
|