Program.cs 2.8 KB

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