- ASP.NET Core 내장 Rate Limiting (FixedWindow, IP 기반 분당 100회) - 한도 초과 시 HTTP 429 + ApiResponse(에러코드 106) 반환 - Swashbuckle.AspNetCore 6.9.0 기반 Swagger UI 추가 - 도메인별 API 문서 그룹 (all, public, auth 등 10개) - JWT Bearer 인증 UI (Authorize 버튼) - X-Service-Code/X-API-KEY 커스텀 헤더 자동 표시 필터 - Microsoft.AspNetCore.OpenApi 제거 (Swashbuckle과 호환 충돌) Closes #30
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 서비스 식별 (미구현 — Issue #13) --
|
|
// app.UseMiddleware<ServiceCodeMiddleware>();
|
|
|
|
// -- 13. X-API-KEY 검증 (미구현 — Issue #13) --
|
|
// app.UseMiddleware<ApiKeyMiddleware>();
|
|
|
|
// -- 14. X-SPMS-TEST 샌드박스 모드 (미구현 — Issue #14) --
|
|
// 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;
|
|
}
|
|
}
|