source: ChapterX.API/Program.cs@ b62cefc

main
Last change on this file since b62cefc was b62cefc, checked in by kikisrbinoska <srbinoskakristina07@…>, 4 months ago

Added frontend and imporoved AI Suggestions service

  • Property mode set to 100644
File size: 2.7 KB
Line 
1using ChapterX.Application;
2using ChapterX.Infrastructure;
3using Microsoft.AspNetCore.Authentication.JwtBearer;
4using Microsoft.IdentityModel.Tokens;
5using System.Reflection;
6using System.Text;
7
8var builder = WebApplication.CreateBuilder(args);
9
10builder.Services.AddCors(options =>
11{
12 options.AddPolicy("Frontend", policy =>
13 policy.WithOrigins("http://localhost:5173", "https://localhost:5173")
14 .AllowAnyHeader()
15 .AllowAnyMethod());
16});
17
18builder.Services.AddControllers();
19builder.Services.AddEndpointsApiExplorer();
20builder.Services.AddSwaggerGen(options =>
21{
22 options.CustomSchemaIds(type => type.FullName);
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 });
46});
47
48builder.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 });
63builder.Services.AddAuthorization();
64
65builder.Services.AddApplication();
66builder.Services.AddInfrastructure(builder.Configuration);
67
68var app = builder.Build();
69
70if (app.Environment.IsDevelopment())
71{
72 app.UseSwagger();
73 app.UseSwaggerUI();
74}
75
76app.UseHttpsRedirection();
77app.UseCors("Frontend");
78app.UseAuthentication();
79app.UseAuthorization();
80try
81{
82 app.MapControllers();
83}
84catch (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
95app.Run();
Note: See TracBrowser for help on using the repository browser.