- Infrastructure/DependencyInjection.cs: AddInfrastructure() 확장 메서드 - Application/DependencyInjection.cs: AddApplication() 확장 메서드 - API/Extensions/ApplicationBuilderExtensions.cs: UseMiddlewarePipeline() 확장 메서드 - Program.cs 정리 (DI/파이프라인 분리) Closes #26
81 lines
2.4 KiB
C#
81 lines
2.4 KiB
C#
using Serilog;
|
|
using SPMS.API.Middlewares;
|
|
|
|
namespace SPMS.API.Extensions;
|
|
|
|
public static class ApplicationBuilderExtensions
|
|
{
|
|
public static WebApplication UseMiddlewarePipeline(this WebApplication app)
|
|
{
|
|
// -- 1. 예외 처리 (최외곽 — 이후 모든 미들웨어 예외 포착) --
|
|
app.UseMiddleware<ExceptionMiddleware>();
|
|
|
|
// -- 2. 보안 헤더 주입 (미구현) --
|
|
// app.UseMiddleware<SecurityHeadersMiddleware>();
|
|
|
|
// -- 3. X-Request-ID 발급/반환 (클라이언트 디버깅용) --
|
|
app.UseMiddleware<RequestIdMiddleware>();
|
|
|
|
// -- 4. Serilog 구조적 로깅 (X-Request-ID 이후에 위치) --
|
|
app.UseSerilogRequestLogging(options =>
|
|
{
|
|
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
|
|
{
|
|
if (httpContext.Items.TryGetValue("RequestId", out var requestId))
|
|
{
|
|
diagnosticContext.Set("RequestId", requestId);
|
|
}
|
|
};
|
|
});
|
|
|
|
// -- 5. HTTPS 리다이렉션 (Nginx가 HTTPS 처리하므로 Production에서만) --
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
app.UseHttpsRedirection();
|
|
}
|
|
|
|
// -- 6. 정적 파일 서빙 --
|
|
var webRoot = app.Environment.WebRootPath;
|
|
if (Directory.Exists(webRoot))
|
|
{
|
|
app.UseStaticFiles();
|
|
}
|
|
|
|
// -- 7. 라우팅 --
|
|
app.UseRouting();
|
|
|
|
// -- 8. CORS (미구현) --
|
|
// app.UseCors("DefaultPolicy");
|
|
|
|
// -- 9. API 속도 제한 (미구현 — Issue #12) --
|
|
// app.UseRateLimiter();
|
|
|
|
// -- 10. JWT 인증 --
|
|
app.UseAuthentication();
|
|
|
|
// -- 11. 역할 인가 --
|
|
app.UseAuthorization();
|
|
|
|
// -- 12. X-Service-Code 서비스 식별 (미구현 — Issue #13) --
|
|
// app.UseMiddleware<ServiceCodeMiddleware>();
|
|
|
|
// -- 13. X-API-KEY 검증 (미구현 — Issue #13) --
|
|
// app.UseMiddleware<ApiKeyMiddleware>();
|
|
|
|
// -- 14. X-SPMS-TEST 샌드박스 모드 (미구현 — Issue #14) --
|
|
// app.UseMiddleware<SandboxMiddleware>();
|
|
|
|
// -- 15. OpenAPI (개발 환경만) --
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.MapOpenApi();
|
|
}
|
|
|
|
// -- 16. 엔드포인트 매핑 --
|
|
app.MapControllers();
|
|
app.MapFallbackToFile("index.html");
|
|
|
|
return app;
|
|
}
|
|
}
|