Program.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. builder.Services.AddRazorPages(options =>
  16. {
  17. options.Conventions.AuthorizeFolder("/");
  18. options.Conventions.AllowAnonymousToAreaFolder("Identity", "/Account");
  19. });
  20. // 프로그램 설정 값 배치
  21. builder.Services.Configure<AppSettings>(builder.Configuration);
  22. // DB 연결
  23. builder.Services.AddApplication().AddAdminInfrastructure(builder.Configuration);
  24. // 관리자 레이아웃
  25. builder.Services.AddScoped<ILayoutDataProvider, LayoutDataProvider>();
  26. // 인증 및 권한 부여
  27. builder.Services.AddAdminForwardedHeaders(settings); // 리버스 프록시 및 접근 제어
  28. builder.Services.AddAdminIdentity(settings); // 관리자단 Identity 인증 및 비밀번호 정책
  29. builder.Logging.AddConsole(); // 터미널에 로그 출력
  30. if (builder.Environment.IsDevelopment())
  31. {
  32. builder.Logging.AddDebug(); // 디버깅 창에 로그 출력
  33. }
  34. /**
  35. * =======================================================================================================================================================
  36. */
  37. var app = builder.Build();
  38. /**
  39. * =======================================================================================================================================================
  40. */
  41. // 환경변수 불러오기
  42. var env = Environment.GetEnvironmentVariable("environmentVariables");
  43. app.Use(async (context, next) =>
  44. {
  45. context.Items["env"] = env; // 환경변수를 static 변수로 저장
  46. await next();
  47. });
  48. /**
  49. * =======================================================================================================================================================
  50. */
  51. // Configure the HTTP request pipeline.
  52. if (!app.Environment.IsDevelopment())
  53. {
  54. app.UseExceptionHandler("/Error");
  55. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  56. app.UseHsts();
  57. }
  58. app.UseHttpsRedirection();
  59. app.UseRouting();
  60. app.UseAuthentication();
  61. app.UseAuthorization();
  62. // 메뉴 기반 역할 권한 체크
  63. app.UseMiddleware<MenuAuthorizationMiddleware>();
  64. // 관리자 접속 기록
  65. app.UseMiddleware<AdminAccessLogMiddleware>();
  66. app.MapStaticAssets();
  67. app.MapRazorPages().WithStaticAssets();
  68. app.Run();