cleanup. refactored program.cs to make use of static setup classes
This commit is contained in:
@@ -1,10 +1,4 @@
|
|||||||
using System;
|
namespace server.Models
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace server.Models
|
|
||||||
{
|
{
|
||||||
public class CharacterFilter
|
public class CharacterFilter
|
||||||
{
|
{
|
||||||
|
|||||||
+5
-97
@@ -1,14 +1,5 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
|
||||||
using Microsoft.AspNetCore.Mvc.Versioning;
|
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using server.SwaggerOptions;
|
|
||||||
using server.Persistence;
|
|
||||||
using server.Persistence.Repositories;
|
|
||||||
using server.Persistence.Seed;
|
using server.Persistence.Seed;
|
||||||
using System.Reflection;
|
using server.Setup;
|
||||||
using AspNetCoreRateLimit;
|
|
||||||
using server.SwaggerOptions.Filters;
|
|
||||||
|
|
||||||
if (args.Length == 2 && args[0].ToLower() == "seed")
|
if (args.Length == 2 && args[0].ToLower() == "seed")
|
||||||
{
|
{
|
||||||
@@ -36,98 +27,15 @@ if (args.Length == 2 && args[0].ToLower() == "seed")
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
builder.Services.AddMemoryCache();
|
Services.SetupDb(builder);
|
||||||
|
Services.SetupMvC(builder);
|
||||||
builder.Services.Configure<IpRateLimitOptions>(
|
Services.SetupSwagger(builder);
|
||||||
builder.Configuration.GetSection("IpRateLimiting"));
|
|
||||||
|
|
||||||
builder.Services.Configure<IpRateLimitPolicies>(
|
|
||||||
builder.Configuration.GetSection("IpRateLimitPolicies"));
|
|
||||||
|
|
||||||
builder.Services.AddInMemoryRateLimiting();
|
|
||||||
|
|
||||||
builder.Services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
|
|
||||||
|
|
||||||
builder.Services.Configure<DatabaseSettings>(
|
|
||||||
builder.Configuration.GetSection("MongoDBSettings"));
|
|
||||||
|
|
||||||
builder.Services.AddSingleton<IDatabaseSettings>(sp =>
|
|
||||||
sp.GetRequiredService<IOptions<DatabaseSettings>>().Value);
|
|
||||||
|
|
||||||
builder.Services.AddSingleton<IDbContext, DbContext>();
|
|
||||||
|
|
||||||
builder.Services.AddScoped<ISeasonRepository, SeasonRepository>();
|
|
||||||
|
|
||||||
builder.Services.AddScoped<IEpisodeRepository, EpisodeRepository>();
|
|
||||||
|
|
||||||
builder.Services.AddScoped<IQuoteRepository, QuoteRepository>();
|
|
||||||
|
|
||||||
builder.Services.AddScoped<ICharacterRepository, CharacterRepository>();
|
|
||||||
|
|
||||||
builder.Services.AddControllers();
|
|
||||||
|
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
|
||||||
|
|
||||||
builder.Services.AddSwaggerGen(options =>
|
|
||||||
{
|
|
||||||
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
||||||
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
|
||||||
|
|
||||||
options.IncludeXmlComments(xmlPath);
|
|
||||||
options.OperationFilter<ApiVersionOperationFilter>();
|
|
||||||
});
|
|
||||||
|
|
||||||
builder.Services.ConfigureOptions<ConfigureSwaggerOptions>();
|
|
||||||
|
|
||||||
builder.Services.AddApiVersioning(config =>
|
|
||||||
{
|
|
||||||
config.DefaultApiVersion = new ApiVersion(1, 0);
|
|
||||||
config.AssumeDefaultVersionWhenUnspecified = true;
|
|
||||||
config.ReportApiVersions = true;
|
|
||||||
config.ApiVersionReader = new HeaderApiVersionReader("x-api-version");
|
|
||||||
});
|
|
||||||
|
|
||||||
builder.Services.AddVersionedApiExplorer(config =>
|
|
||||||
{
|
|
||||||
config.GroupNameFormat = "'v'VVV";
|
|
||||||
});
|
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
app.UseStaticFiles();
|
Middleware.Setup(app);
|
||||||
|
|
||||||
app.UseSwagger(options =>
|
|
||||||
{
|
|
||||||
options.RouteTemplate = "/{documentName}/docs.json";
|
|
||||||
});
|
|
||||||
|
|
||||||
app.UseSwaggerUI(options =>
|
|
||||||
{
|
|
||||||
var provider = app.Services.GetRequiredService<IApiVersionDescriptionProvider>();
|
|
||||||
|
|
||||||
foreach (var description in provider.ApiVersionDescriptions)
|
|
||||||
{
|
|
||||||
var url = $"/{description.GroupName}/docs.json";
|
|
||||||
var name = $"criminalmindsapi v{description.ApiVersion}";
|
|
||||||
|
|
||||||
options.RoutePrefix = String.Empty;
|
|
||||||
options.SwaggerEndpoint(url, name);
|
|
||||||
options.EnableTryItOutByDefault();
|
|
||||||
options.DisplayRequestDuration();
|
|
||||||
options.DocumentTitle = "criminalmindsapi";
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
|
||||||
|
|
||||||
app.UseAuthorization();
|
|
||||||
|
|
||||||
app.MapControllers();
|
|
||||||
|
|
||||||
app.UseIpRateLimiting();
|
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
using AspNetCoreRateLimit;
|
||||||
|
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
||||||
|
|
||||||
|
namespace server.Setup
|
||||||
|
{
|
||||||
|
public static class Middleware
|
||||||
|
{
|
||||||
|
public static void Setup(WebApplication app)
|
||||||
|
{
|
||||||
|
app.UseSwagger(options =>
|
||||||
|
{
|
||||||
|
options.RouteTemplate = "/{documentName}/docs.json";
|
||||||
|
});
|
||||||
|
|
||||||
|
app.UseSwaggerUI(options =>
|
||||||
|
{
|
||||||
|
var provider = app.Services.GetRequiredService<IApiVersionDescriptionProvider>();
|
||||||
|
|
||||||
|
foreach (var description in provider.ApiVersionDescriptions)
|
||||||
|
{
|
||||||
|
var url = $"/{description.GroupName}/docs.json";
|
||||||
|
var name = $"criminalmindsapi v{description.ApiVersion}";
|
||||||
|
|
||||||
|
options.RoutePrefix = String.Empty;
|
||||||
|
options.SwaggerEndpoint(url, name);
|
||||||
|
options.EnableTryItOutByDefault();
|
||||||
|
options.DisplayRequestDuration();
|
||||||
|
options.DocumentTitle = "criminalmindsapi";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.MapControllers();
|
||||||
|
|
||||||
|
app.UseIpRateLimiting();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
using AspNetCoreRateLimit;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Versioning;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
using server.Persistence;
|
||||||
|
using server.Persistence.Repositories;
|
||||||
|
using server.SwaggerOptions;
|
||||||
|
using server.SwaggerOptions.Filters;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace server.Setup
|
||||||
|
{
|
||||||
|
public static class Services
|
||||||
|
{
|
||||||
|
public static void SetupDb(WebApplicationBuilder builder)
|
||||||
|
{
|
||||||
|
builder.Services.Configure<DatabaseSettings>(
|
||||||
|
builder.Configuration.GetSection("MongoDBSettings"));
|
||||||
|
|
||||||
|
builder.Services.AddSingleton<IDatabaseSettings>(sp =>
|
||||||
|
sp.GetRequiredService<IOptions<DatabaseSettings>>().Value);
|
||||||
|
|
||||||
|
builder.Services.AddSingleton<IDbContext, DbContext>();
|
||||||
|
|
||||||
|
builder.Services.AddScoped<ISeasonRepository, SeasonRepository>();
|
||||||
|
|
||||||
|
builder.Services.AddScoped<IEpisodeRepository, EpisodeRepository>();
|
||||||
|
|
||||||
|
builder.Services.AddScoped<IQuoteRepository, QuoteRepository>();
|
||||||
|
|
||||||
|
builder.Services.AddScoped<ICharacterRepository, CharacterRepository>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void SetupMvC(WebApplicationBuilder builder)
|
||||||
|
{
|
||||||
|
builder.Services.AddMemoryCache();
|
||||||
|
|
||||||
|
builder.Services.Configure<IpRateLimitOptions>(
|
||||||
|
builder.Configuration.GetSection("IpRateLimiting"));
|
||||||
|
|
||||||
|
builder.Services.Configure<IpRateLimitPolicies>(
|
||||||
|
builder.Configuration.GetSection("IpRateLimitPolicies"));
|
||||||
|
|
||||||
|
builder.Services.AddInMemoryRateLimiting();
|
||||||
|
|
||||||
|
builder.Services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
|
||||||
|
|
||||||
|
builder.Services.AddControllers();
|
||||||
|
|
||||||
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
|
|
||||||
|
builder.Services.AddApiVersioning(config =>
|
||||||
|
{
|
||||||
|
config.DefaultApiVersion = new ApiVersion(1, 0);
|
||||||
|
config.AssumeDefaultVersionWhenUnspecified = true;
|
||||||
|
config.ReportApiVersions = true;
|
||||||
|
config.ApiVersionReader = new HeaderApiVersionReader("x-api-version");
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.Services.AddVersionedApiExplorer(config =>
|
||||||
|
{
|
||||||
|
config.GroupNameFormat = "'v'VVV";
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void SetupSwagger(WebApplicationBuilder builder)
|
||||||
|
{
|
||||||
|
builder.Services.AddSwaggerGen(options =>
|
||||||
|
{
|
||||||
|
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
||||||
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
||||||
|
|
||||||
|
options.IncludeXmlComments(xmlPath);
|
||||||
|
options.OperationFilter<ApiVersionOperationFilter>();
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.Services.ConfigureOptions<ConfigureSwaggerOptions>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user