| [877c13c] | 1 | using ChapterX.Application;
|
|---|
| 2 | using ChapterX.Infrastructure;
|
|---|
| [e294f7d] | 3 | using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|---|
| 4 | using Microsoft.IdentityModel.Tokens;
|
|---|
| [877c13c] | 5 | using System.Reflection;
|
|---|
| [e294f7d] | 6 | using System.Text;
|
|---|
| [877c13c] | 7 |
|
|---|
| 8 | var builder = WebApplication.CreateBuilder(args);
|
|---|
| 9 |
|
|---|
| [b62cefc] | 10 | builder.Services.AddCors(options =>
|
|---|
| 11 | {
|
|---|
| 12 | options.AddPolicy("Frontend", policy =>
|
|---|
| 13 | policy.WithOrigins("http://localhost:5173", "https://localhost:5173")
|
|---|
| 14 | .AllowAnyHeader()
|
|---|
| 15 | .AllowAnyMethod());
|
|---|
| 16 | });
|
|---|
| 17 |
|
|---|
| [877c13c] | 18 | builder.Services.AddControllers();
|
|---|
| 19 | builder.Services.AddEndpointsApiExplorer();
|
|---|
| 20 | builder.Services.AddSwaggerGen(options =>
|
|---|
| 21 | {
|
|---|
| 22 | options.CustomSchemaIds(type => type.FullName);
|
|---|
| [e294f7d] | 23 | options.AddSecurityDefinition("Bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme
|
|---|
| 24 | {
|
|---|
| 25 | Name = "Authorization",
|
|---|
| 26 | Type = Microsoft.OpenApi.Models.SecuritySchemeType.Http,
|
|---|
| 27 | Scheme = "Bearer",
|
|---|
| 28 | BearerFormat = "JWT",
|
|---|
| 29 | In = Microsoft.OpenApi.Models.ParameterLocation.Header,
|
|---|
| 30 | Description = "Enter your JWT token"
|
|---|
| 31 | });
|
|---|
| 32 | options.AddSecurityRequirement(new Microsoft.OpenApi.Models.OpenApiSecurityRequirement
|
|---|
| 33 | {
|
|---|
| 34 | {
|
|---|
| 35 | new Microsoft.OpenApi.Models.OpenApiSecurityScheme
|
|---|
| 36 | {
|
|---|
| 37 | Reference = new Microsoft.OpenApi.Models.OpenApiReference
|
|---|
| 38 | {
|
|---|
| 39 | Type = Microsoft.OpenApi.Models.ReferenceType.SecurityScheme,
|
|---|
| 40 | Id = "Bearer"
|
|---|
| 41 | }
|
|---|
| 42 | },
|
|---|
| 43 | Array.Empty<string>()
|
|---|
| 44 | }
|
|---|
| 45 | });
|
|---|
| [877c13c] | 46 | });
|
|---|
| 47 |
|
|---|
| [e294f7d] | 48 | builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|---|
| 49 | .AddJwtBearer(options =>
|
|---|
| 50 | {
|
|---|
| 51 | options.TokenValidationParameters = new TokenValidationParameters
|
|---|
| 52 | {
|
|---|
| 53 | ValidateIssuer = true,
|
|---|
| 54 | ValidateAudience = true,
|
|---|
| 55 | ValidateLifetime = true,
|
|---|
| 56 | ValidateIssuerSigningKey = true,
|
|---|
| 57 | ValidIssuer = builder.Configuration["Jwt:Issuer"],
|
|---|
| 58 | ValidAudience = builder.Configuration["Jwt:Audience"],
|
|---|
| 59 | IssuerSigningKey = new SymmetricSecurityKey(
|
|---|
| 60 | Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]!))
|
|---|
| 61 | };
|
|---|
| 62 | });
|
|---|
| [877c13c] | 63 | builder.Services.AddAuthorization();
|
|---|
| 64 |
|
|---|
| 65 | builder.Services.AddApplication();
|
|---|
| 66 | builder.Services.AddInfrastructure(builder.Configuration);
|
|---|
| 67 |
|
|---|
| 68 | var app = builder.Build();
|
|---|
| 69 |
|
|---|
| 70 | if (app.Environment.IsDevelopment())
|
|---|
| 71 | {
|
|---|
| 72 | app.UseSwagger();
|
|---|
| 73 | app.UseSwaggerUI();
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | app.UseHttpsRedirection();
|
|---|
| [b62cefc] | 77 | app.UseCors("Frontend");
|
|---|
| [877c13c] | 78 | app.UseAuthentication();
|
|---|
| 79 | app.UseAuthorization();
|
|---|
| 80 | try
|
|---|
| 81 | {
|
|---|
| 82 | app.MapControllers();
|
|---|
| 83 | }
|
|---|
| 84 | catch (ReflectionTypeLoadException ex)
|
|---|
| 85 | {
|
|---|
| 86 | Console.Error.WriteLine("ReflectionTypeLoadException while mapping controllers:");
|
|---|
| 87 | foreach (var loaderEx in ex.LoaderExceptions ?? [])
|
|---|
| 88 | {
|
|---|
| 89 | Console.Error.WriteLine(loaderEx.ToString());
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | throw;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | app.Run();
|
|---|