3.5만 에피소드 기반 표절·AI·판례 분석 파이프라인 구현
This commit is contained in:
1 parent
4f6898d0b2
commit
ce50e89089
32 files changed
+4505
-69
No files matched your search
+20
@@ -1,10 +1,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import hmac
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi import Request
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.responses import FileResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
@@ -19,6 +22,11 @@ logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
settings = get_settings()
|
||||
if not settings.api_key.strip():
|
||||
logging.critical(
|
||||
"API_KEY is empty: all /v1 endpoints are unauthenticated. "
|
||||
"Do not expose unpublished manuscripts publicly until client key rollout is complete."
|
||||
)
|
||||
app.state.settings = settings
|
||||
app.state.detector = PlagiarismDetector(settings=settings)
|
||||
app.state.job_store = JobStore()
|
||||
@@ -55,6 +63,18 @@ app = FastAPI(
|
||||
|
||||
app.include_router(api_router)
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def optional_api_key_auth(request: Request, call_next):
|
||||
"""API_KEY가 설정된 운영 환경에서만 API 인증을 강제한다."""
|
||||
configured = _settings.api_key.strip()
|
||||
public_paths = {"/", "/v1/health", "/docs", "/openapi.json", "/redoc"}
|
||||
if configured and request.url.path not in public_paths and request.url.path.startswith("/v1"):
|
||||
supplied = request.headers.get("x-api-key", "")
|
||||
if not hmac.compare_digest(configured, supplied):
|
||||
return JSONResponse(status_code=401, content={"detail": "Invalid or missing API key"})
|
||||
return await call_next(request)
|
||||
|
||||
_STATIC_DIR = Path(__file__).resolve().parent / "static"
|
||||
if _STATIC_DIR.exists():
|
||||
app.mount("/static", StaticFiles(directory=str(_STATIC_DIR)), name="static")
|
||||
|
||||
Reference in new issue
Block a user