- SandboxMiddleware 추가: X-SPMS-TEST 헤더로 테스트 모드 감지 - HttpContext.Items["IsSandbox"] 플래그 설정 - 미들웨어 파이프라인 14번 위치에 등록 Closes #34
94 lines
3.3 KiB
C#
94 lines
3.3 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 속도 제한 (IP별 분당 100회) --
|
|
app.UseRateLimiter();
|
|
|
|
// -- 10. JWT 인증 --
|
|
app.UseAuthentication();
|
|
|
|
// -- 11. 역할 인가 --
|
|
app.UseAuthorization();
|
|
|
|
// -- 12. X-Service-Code 서비스 식별 --
|
|
app.UseMiddleware<ServiceCodeMiddleware>();
|
|
|
|
// -- 13. X-API-KEY 검증 (SDK/디바이스 엔드포인트용) --
|
|
app.UseMiddleware<ApiKeyMiddleware>();
|
|
|
|
// -- 14. X-SPMS-TEST 샌드박스 모드 --
|
|
app.UseMiddleware<SandboxMiddleware>();
|
|
|
|
// -- 15. Swagger UI (개발 환경만) --
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI(options =>
|
|
{
|
|
options.SwaggerEndpoint("/swagger/all/swagger.json", "SPMS API - 전체");
|
|
options.SwaggerEndpoint("/swagger/public/swagger.json", "공개 API");
|
|
options.SwaggerEndpoint("/swagger/auth/swagger.json", "인증 API");
|
|
options.SwaggerEndpoint("/swagger/account/swagger.json", "계정 API");
|
|
options.SwaggerEndpoint("/swagger/service/swagger.json", "서비스 API");
|
|
options.SwaggerEndpoint("/swagger/device/swagger.json", "디바이스 API");
|
|
options.SwaggerEndpoint("/swagger/message/swagger.json", "메시지 API");
|
|
options.SwaggerEndpoint("/swagger/push/swagger.json", "푸시 API");
|
|
options.SwaggerEndpoint("/swagger/stats/swagger.json", "통계 API");
|
|
options.SwaggerEndpoint("/swagger/file/swagger.json", "파일 API");
|
|
});
|
|
}
|
|
|
|
// -- 16. 엔드포인트 매핑 --
|
|
app.MapControllers();
|
|
app.MapFallbackToFile("index.html");
|
|
|
|
return app;
|
|
}
|
|
}
|