You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
5.3KB

  1. using Microsoft.AspNetCore.Authentication.JwtBearer;
  2. using Microsoft.AspNetCore.Cors;
  3. using Microsoft.AspNetCore.Server.Kestrel.Core;
  4. using Microsoft.EntityFrameworkCore;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Microsoft.IdentityModel.Tokens;
  7. using ReAct_PME.Application;
  8. using ReAct_PME.Domain;
  9. using ReAct_PME.Infrastructure;
  10. using ReAct_PME.Infrastructure.Persistence;
  11. using System.Text;
  12. var builder = WebApplication.CreateBuilder(args);
  13. builder.Configuration
  14. .SetBasePath(Directory.GetCurrentDirectory())
  15. .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
  16. .AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true)
  17. .AddEnvironmentVariables(); // ← prioritaire
  18. var allowedOrigins = builder.Configuration
  19. .GetSection("Cors:AllowedOrigins")
  20. .Get<string[]>();
  21. var secretKey = builder.Configuration["Jwt:SecretKey"];
  22. if (!string.IsNullOrWhiteSpace(secretKey))
  23. Console.WriteLine($"JWT SecretKey length: {secretKey?.Length ?? 0}");
  24. var environnement = builder.Configuration["Params:Environment"];
  25. var apiUrl = builder.Configuration["Params:ApiUrl"];
  26. if(apiUrl == null)
  27. {
  28. apiUrl = "https://localhost:7008";
  29. }
  30. var isHttps = (apiUrl.ToLower().Contains("https://"));
  31. Console.WriteLine($"ENV : {environnement}");
  32. Console.WriteLine($"API : {apiUrl}");
  33. builder.Services.AddAuthentication(options =>
  34. {
  35. options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  36. options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  37. })
  38. .AddJwtBearer(options =>
  39. {
  40. options.TokenValidationParameters = new TokenValidationParameters
  41. {
  42. ValidateIssuer = true,
  43. ValidateAudience = true,
  44. ValidateLifetime = true,
  45. ValidateIssuerSigningKey = true,
  46. ValidIssuer = builder.Configuration["Jwt:Issuer"],
  47. ValidAudience = builder.Configuration["Jwt:Audience"],
  48. IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey!))
  49. };
  50. });
  51. // Add services to the container.
  52. builder.Services.AddControllers();
  53. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  54. builder.Services.AddEndpointsApiExplorer();
  55. builder.Services.AddSwaggerGen();
  56. builder.Services.AddScoped<IDT_CONVERSATIONRepository, DT_CONVERSATIONRepository>();
  57. builder.Services.AddScoped<IDT_CONVERSATIONService, DT_CONVERSATIONService>();
  58. builder.Services.AddScoped<IDT_CONVERSATION_LIGNERepository, DT_CONVERSATION_LIGNERepository>();
  59. builder.Services.AddScoped<IDT_CONVERSATION_LIGNEService, DT_CONVERSATION_LIGNEService>();
  60. builder.Services.AddScoped<IDT_LOGSRepository, DT_LOGSRepository>();
  61. builder.Services.AddScoped<IDT_LOGSService, DT_LOGSService>();
  62. builder.Services.AddScoped<IDT_ROLERepository, DT_ROLERepository>();
  63. builder.Services.AddScoped<IDT_ROLEService, DT_ROLEService>();
  64. builder.Services.AddScoped<IDT_WORK_FORCERepository, DT_WORK_FORCERepository>();
  65. builder.Services.AddScoped<IDT_WORK_FORCEService, DT_WORK_FORCEService>();
  66. builder.Services.AddScoped<IDT_WORK_FORCE_ROLERepository, DT_WORK_FORCE_ROLERepository>();
  67. builder.Services.AddScoped<IDT_WORK_FORCE_ROLEService, DT_WORK_FORCE_ROLEService>();
  68. builder.Services.AddScoped<IListeMailsService, ListeMailsService>();
  69. builder.Services.AddScoped<IChatRoomService, ChatRoomService>();
  70. builder.Services.AddScoped<IVerifServices, VerifServices>();
  71. builder.Services.AddScoped<IParametresService, ParametresService>();
  72. builder.Services.AddScoped<IAuthRepository, AuthRepository>();
  73. builder.Services.AddScoped<IAuthService, AuthService>();
  74. builder.Services.AddDbContext<AppDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
  75. builder.Services.AddCors(options =>
  76. {
  77. options.AddPolicy("AllowBlazor", policy =>
  78. {
  79. policy.WithOrigins(allowedOrigins ?? Array.Empty<string>())
  80. .AllowAnyHeader()
  81. .AllowAnyMethod();
  82. });
  83. });
  84. builder.WebHost.UseUrls(apiUrl);
  85. // ci-dessous : nécessaire avec docker / linux
  86. if(environnement == "Docker")
  87. {
  88. builder.WebHost.ConfigureKestrel(options =>
  89. {
  90. options.ListenAnyIP(80); // HTTP seulement
  91. });
  92. }
  93. builder.Services.Configure<KestrelServerOptions>(options =>
  94. {
  95. options.Limits.MaxRequestBodySize = 1024 * 1024 * 100; // 100MB
  96. });
  97. var app = builder.Build();
  98. app.Use(async (context, next) =>
  99. {
  100. Console.WriteLine($"[Request] {context.Request.Method} {context.Request.Path}");
  101. await next.Invoke();
  102. });
  103. /*
  104. // Migration auto de la base de données : PAS POUR L'INSTANT
  105. using (var scope = app.Services.CreateScope())
  106. {
  107. try{
  108. var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
  109. dbContext.Database.Migrate();
  110. }
  111. catch(Exception ex){
  112. Console.Writeline(ex.ToString()
  113. }
  114. }
  115. */
  116. app.UseRouting();
  117. app.UseCors("AllowBlazor");
  118. app.UseAuthentication();
  119. app.UseAuthorization();
  120. // Configure the HTTP request pipeline.
  121. if (app.Environment.IsDevelopment())
  122. {
  123. app.UseSwagger();
  124. app.UseSwaggerUI();
  125. builder.Configuration.AddUserSecrets<Program>();
  126. }
  127. if(isHttps)
  128. {
  129. app.UseHttpsRedirection();
  130. Console.WriteLine("HTTPS redirection is enabled. ");
  131. }
  132. else
  133. {
  134. Console.WriteLine("Warning: HTTPS redirection is disabled. Ensure you are aware of the security implications.");
  135. }
  136. app.MapControllers();
  137. app.Run();