|
|
@@ -1,89 +1,39 @@
|
|
|
-using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
-using Microsoft.AspNetCore.StaticFiles.Infrastructure;
|
|
|
-using Microsoft.IdentityModel.Tokens;
|
|
|
+using RunnersMeet.Server.Domain;
|
|
|
using RunnersMeet.Server.Frontend;
|
|
|
-using RunnersMeet.Server.GpxFormat;
|
|
|
using RunnersMeet.Server.Persistence;
|
|
|
|
|
|
namespace RunnersMeet.Server;
|
|
|
|
|
|
public class AppServer
|
|
|
{
|
|
|
+ private readonly IList<IAppConfigurationModule> _modules = new List<IAppConfigurationModule>
|
|
|
+ {
|
|
|
+ new AuthenticationModule(),
|
|
|
+ new CorsModule(),
|
|
|
+ new SpaRoutingModule(),
|
|
|
+ new ApiControllersModule(),
|
|
|
+ new SwaggerModule(),
|
|
|
+ new PersistenceModule(),
|
|
|
+ new DomainServicesModule(),
|
|
|
+ };
|
|
|
+
|
|
|
public void Start(string[] args)
|
|
|
{
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
- ConfigureServices(builder.Services, builder.Configuration);
|
|
|
- builder.Services.AddControllers().AddJsonOptions(options =>
|
|
|
+ foreach (var appConfigurationModule in _modules)
|
|
|
{
|
|
|
- options.JsonSerializerOptions.Converters.Add(new ObjectIdConverter());
|
|
|
- });
|
|
|
- // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
|
- builder.Services.AddEndpointsApiExplorer();
|
|
|
- builder.Services.AddSwaggerGen();
|
|
|
-
|
|
|
- builder.Services.AddCors(options =>
|
|
|
- {
|
|
|
- options.AddDefaultPolicy(policy =>
|
|
|
- {
|
|
|
- policy.WithOrigins("http://localhost:4200", "https://gpx.studio");
|
|
|
- policy.WithHeaders("Authorization", "Content-Type");
|
|
|
- policy.AllowAnyMethod();
|
|
|
- policy.WithExposedHeaders("Content-Disposition");
|
|
|
- });
|
|
|
- });
|
|
|
-
|
|
|
- var authOptions = new AuthOptions();
|
|
|
- builder.Configuration.GetSection("Auth").Bind(authOptions);
|
|
|
- builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
- .AddJwtBearer(options =>
|
|
|
- {
|
|
|
- options.Authority = authOptions.Authority;
|
|
|
- options.TokenValidationParameters = new TokenValidationParameters
|
|
|
- {
|
|
|
- ValidIssuer = authOptions.Authority,
|
|
|
- ValidAudience = authOptions.Audience
|
|
|
- };
|
|
|
- });
|
|
|
-
|
|
|
- builder.Services.AddAuthorization(authorizationOptions =>
|
|
|
- {
|
|
|
- foreach (var policyPair in authOptions.PolicyClaims)
|
|
|
- {
|
|
|
- authorizationOptions.AddPolicy(policyPair.Key, policyBuilder => policyBuilder.RequireClaim("permissions", policyPair.Value));
|
|
|
- }
|
|
|
- });
|
|
|
+ appConfigurationModule.ConfigureServices(builder.Services, builder.Configuration);
|
|
|
+ }
|
|
|
|
|
|
var app = builder.Build();
|
|
|
+ app.UseHttpsRedirection();
|
|
|
|
|
|
- // Configure the HTTP request pipeline.
|
|
|
- if (app.Environment.IsDevelopment())
|
|
|
+ foreach (var appConfigurationModule in _modules)
|
|
|
{
|
|
|
- app.UseSwagger();
|
|
|
- app.UseSwaggerUI();
|
|
|
+ appConfigurationModule.ConfigureApplication(app);
|
|
|
}
|
|
|
|
|
|
- app.UseHttpsRedirection();
|
|
|
- app.UseCors();
|
|
|
- app.UseAuthorization();
|
|
|
- app.UseSpaDefaultPageMiddleware();
|
|
|
- app.UseStaticFiles();
|
|
|
- app.MapControllers();
|
|
|
-
|
|
|
-
|
|
|
app.Run();
|
|
|
}
|
|
|
-
|
|
|
- private void ConfigureServices(IServiceCollection services, IConfigurationRoot config)
|
|
|
- {
|
|
|
- services.AddSingleton<IDatabase, Database>();
|
|
|
- services.AddSingleton<IFileStorage, FileStorage>();
|
|
|
- services.AddScoped<QueryFactory, QueryFactory>();
|
|
|
-
|
|
|
- services.AddSingleton<GpxParser>();
|
|
|
-
|
|
|
- services.Configure<PersistenceOptions>(config.GetSection(PersistenceOptions.SectionName));
|
|
|
- services.AddTransient<IPersistenceOptions, PersistenceOptionsAccessor>();
|
|
|
- services.Configure<ApiSettings>(config.GetSection(ApiSettings.SectionName));
|
|
|
- }
|
|
|
}
|