Program.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using SharedKernel;
  2. using Admin.Extensions;
  3. using Admin.Pages.Shared.Layout;
  4. using Admin.Middlewares;
  5. using Application;
  6. using Infrastructure;
  7. var builder = WebApplication.CreateBuilder(args);
  8. var settings = builder.Configuration.Get<AppSettings>()!;
  9. Console.Title = settings.App.Name;
  10. Console.WriteLine($"ENV={builder.Environment.EnvironmentName}");
  11. Console.WriteLine($"현재 시간: {DateTime.Now} / {TimeZoneInfo.Local.Id}");
  12. // Add services to the container.
  13. var mvcBuilder = builder.Services.AddRazorPages(options =>
  14. {
  15. options.Conventions.AuthorizeFolder("/");
  16. options.Conventions.AllowAnonymousToAreaFolder("Identity", "/Account");
  17. });
  18. // 프로그램 설정 값 배치
  19. builder.Services.Configure<AppSettings>(builder.Configuration);
  20. // DB 연결
  21. builder.Services.AddApplication().AddAdminInfrastructure(builder.Configuration);
  22. // 관리자 레이아웃
  23. builder.Services.AddScoped<ILayoutDataProvider, LayoutDataProvider>();
  24. // 인증 및 권한 부여
  25. builder.Services.AddAdminForwardedHeaders(settings); // 리버스 프록시 및 접근 제어
  26. builder.Services.AddAdminIdentity(settings); // 관리자단 Identity 인증 및 비밀번호 정책
  27. builder.Logging.AddConsole(); // 터미널에 로그 출력
  28. if (builder.Environment.IsDevelopment())
  29. {
  30. builder.Logging.AddDebug(); // 디버깅 창에 로그 출력
  31. }
  32. /**
  33. * =======================================================================================================================================================
  34. */
  35. var app = builder.Build();
  36. /**
  37. * =======================================================================================================================================================
  38. */
  39. // 환경변수 불러오기
  40. var env = Environment.GetEnvironmentVariable("environmentVariables");
  41. app.Use(async (context, next) =>
  42. {
  43. context.Items["env"] = env; // 환경변수를 static 변수로 저장
  44. await next();
  45. });
  46. /**
  47. * =======================================================================================================================================================
  48. */
  49. // Configure the HTTP request pipeline.
  50. if (!app.Environment.IsDevelopment())
  51. {
  52. app.UseExceptionHandler("/Error");
  53. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  54. app.UseHsts();
  55. }
  56. app.UseHttpsRedirection();
  57. app.UseRouting();
  58. app.UseAuthentication();
  59. app.UseAuthorization();
  60. // 메뉴 기반 역할 권한 체크
  61. app.UseMiddleware<MenuAuthorizationMiddleware>();
  62. // 관리자 접속 기록
  63. app.UseMiddleware<AdminAccessLogMiddleware>();
  64. app.MapStaticAssets();
  65. app.MapRazorPages().WithStaticAssets();
  66. app.Run();