116 lines
No EOL
3.9 KiB
C#
116 lines
No EOL
3.9 KiB
C#
using System.Text.Json.Serialization;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.OpenApi.Models;
|
|
using SurveyBackend.Contexts;
|
|
using SurveyBackend.Core.Contexts;
|
|
using SurveyBackend.Filters;
|
|
using SurveyBackend.Infrastructure;
|
|
using SurveyBackend.Infrastructure.Data;
|
|
using SurveyBackend.Middlewares;
|
|
using SurveyBackend.Services;
|
|
using SurveyLib.Infrastructure.EFCore;
|
|
using SurveyLib.Infrastructure.EFCore.Data;
|
|
|
|
namespace SurveyBackend;
|
|
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
AuthOptions.MakeOptions(builder.Configuration, Environment.GetEnvironmentVariable("JWT_SECRET_KEY"));
|
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
|
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));
|
|
|
|
builder.Services.AddScoped<SurveyDbContext>(provider => provider.GetRequiredService<ApplicationDbContext>());
|
|
|
|
builder.Services.AddHttpContextAccessor();
|
|
|
|
builder.Services.AddScoped<IUserContext, UserContext>();
|
|
|
|
builder.Services.AddSurveyBackendInfrastructure();
|
|
builder.Services.AddSurveyLibInfrastructure();
|
|
builder.Services.AddSurveyBackendServices();
|
|
|
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidIssuer = AuthOptions.Issuer,
|
|
ValidAudience = AuthOptions.Audience,
|
|
IssuerSigningKey = AuthOptions.SymmetricSecurityKey
|
|
};
|
|
});
|
|
|
|
builder.Services.AddControllers().AddJsonOptions(opts =>
|
|
{
|
|
opts.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
|
|
});
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen(c =>
|
|
{
|
|
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
{
|
|
Scheme = "bearer",
|
|
BearerFormat = "JWT",
|
|
In = ParameterLocation.Header,
|
|
Description = "JWT Authorization header using the Bearer scheme.",
|
|
Name = "Authorization",
|
|
Type = SecuritySchemeType.ApiKey
|
|
});
|
|
|
|
c.OperationFilter<EndpointAuthRequirementFilter>();
|
|
|
|
var filePath = Path.Combine(System.AppContext.BaseDirectory, "SurveyBackend.API.xml");
|
|
c.IncludeXmlComments(filePath);
|
|
});
|
|
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("AllowAll",
|
|
policyBuilder =>
|
|
{
|
|
policyBuilder
|
|
.AllowAnyOrigin()
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod()
|
|
.SetIsOriginAllowedToAllowWildcardSubdomains();
|
|
});
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger(options => { options.RouteTemplate = "api/swagger/{documentName}/swagger.json"; });
|
|
app.UseSwaggerUI(options =>
|
|
{
|
|
options.SwaggerEndpoint("/api/swagger/v1/swagger.json", "Survey Backend V1");
|
|
options.RoutePrefix = "api/swagger";
|
|
});
|
|
|
|
app.UseCors("AllowAll");
|
|
}
|
|
|
|
app.UseMiddleware<ExceptionsMiddleware>();
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|
|
}
|
|
} |