Program.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System.Text;
  2. using Common.Global;
  3. using Database.Database;
  4. using Esim.Apis.Business;
  5. using Microsoft.AspNetCore.Authentication.JwtBearer;
  6. using Microsoft.EntityFrameworkCore;
  7. using Microsoft.IdentityModel.Tokens;
  8. var builder = WebApplication.CreateBuilder(args);
  9. // Set global configuration for use by singletons like ConfigManager
  10. GlobalConfig.Configuration = builder.Configuration;
  11. // Add services to the container.
  12. builder.Services.AddControllersWithViews();
  13. // Add DbContext with Oracle provider
  14. var connectionString = builder.Configuration.GetSection("Connection").Value;
  15. builder.Services.AddDbContext<ModelContext>(options =>
  16. options.UseOracle(connectionString));
  17. // Add Business Services
  18. builder.Services.AddScoped<IUserBusiness, UserBusinessImpl>();
  19. builder.Services.AddScoped<IArticleBusiness, ArticleBusinessImpl>();
  20. builder.Services.AddScoped<IContentBusiness, ContentBusinessImpl>();
  21. // Configure JWT Authentication
  22. var jwtKey = builder.Configuration["Jwt:Key"] ?? "EsimLaoSecretKey12345678901234567890";
  23. var jwtIssuer = builder.Configuration["Jwt:Issuer"] ?? "EsimLao";
  24. var jwtAudience = builder.Configuration["Jwt:Audience"] ?? "EsimLaoClient";
  25. builder.Services.AddAuthentication(options =>
  26. {
  27. options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  28. options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  29. })
  30. .AddJwtBearer(options =>
  31. {
  32. options.TokenValidationParameters = new TokenValidationParameters
  33. {
  34. ValidateIssuer = true,
  35. ValidateAudience = true,
  36. ValidateLifetime = true,
  37. ValidateIssuerSigningKey = true,
  38. ValidIssuer = jwtIssuer,
  39. ValidAudience = jwtAudience,
  40. IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtKey))
  41. };
  42. });
  43. // Add Swagger for API documentation
  44. builder.Services.AddEndpointsApiExplorer();
  45. builder.Services.AddSwaggerGen();
  46. var app = builder.Build();
  47. // Initialize ConfigManager to load configs from database
  48. using (var scope = app.Services.CreateScope())
  49. {
  50. var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
  51. try
  52. {
  53. logger.LogInformation("Initializing ConfigManager...");
  54. Esim.Apis.Singleton.ConfigManager.Instance.Initialize();
  55. logger.LogInformation("ConfigManager initialized successfully");
  56. // Start background refresh task
  57. Task.Run(() => Esim.Apis.Singleton.ConfigManager.Instance.RefreshConfigs());
  58. }
  59. catch (Exception ex)
  60. {
  61. logger.LogError(ex, "Failed to initialize ConfigManager");
  62. }
  63. }
  64. app.UseSwagger();
  65. app.UseSwaggerUI();
  66. // Configure the HTTP request pipeline.
  67. //if (app.Environment.IsDevelopment())
  68. //{
  69. // app.UseSwagger();
  70. // app.UseSwaggerUI();
  71. //}
  72. //else
  73. //{
  74. // app.UseExceptionHandler("/Home/Error");
  75. // // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  76. // app.UseHsts();
  77. //}
  78. app.UseHttpsRedirection();
  79. app.UseRouting();
  80. app.UseAuthentication();
  81. app.UseAuthorization();
  82. app.MapStaticAssets();
  83. app.MapControllerRoute(
  84. name: "default",
  85. pattern: "{controller=Home}/{action=Index}/{id?}")
  86. .WithStaticAssets();
  87. app.MapControllers();
  88. app.Run();