Program.cs 2.8 KB

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