Spaces:
Running
Running
backend api key
Browse files- app/__init__.py +21 -1
- app/api/factcheck_router.py +4 -2
- app/api/routes.py +5 -4
app/__init__.py
CHANGED
|
@@ -1,4 +1,7 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
| 2 |
from slowapi import _rate_limit_exceeded_handler
|
| 3 |
from slowapi.errors import RateLimitExceeded
|
| 4 |
from app.api.routes import router as api_router
|
|
@@ -13,6 +16,23 @@ app = FastAPI(
|
|
| 13 |
version="1.0.0",
|
| 14 |
)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
app.state.limiter = limiter
|
| 17 |
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
| 18 |
app.include_router(api_router)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
from fastapi import FastAPI, HTTPException, Security, status
|
| 4 |
+
from fastapi.security import APIKeyHeader
|
| 5 |
from slowapi import _rate_limit_exceeded_handler
|
| 6 |
from slowapi.errors import RateLimitExceeded
|
| 7 |
from app.api.routes import router as api_router
|
|
|
|
| 16 |
version="1.0.0",
|
| 17 |
)
|
| 18 |
|
| 19 |
+
API_KEY_NAME = "X-API-Key"
|
| 20 |
+
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
|
| 21 |
+
EXPECTED_API_KEY = os.getenv("BACKEND_API_KEY")
|
| 22 |
+
|
| 23 |
+
async def verify_api_key(api_key: str = Security(api_key_header)):
|
| 24 |
+
if not EXPECTED_API_KEY:
|
| 25 |
+
raise HTTPException(
|
| 26 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 27 |
+
detail="Błąd serwera: Klucz API nie został skonfigurowany."
|
| 28 |
+
)
|
| 29 |
+
if api_key != EXPECTED_API_KEY:
|
| 30 |
+
raise HTTPException(
|
| 31 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 32 |
+
detail="Brak autoryzacji: Nieprawidłowy lub brakujący klucz API."
|
| 33 |
+
)
|
| 34 |
+
return api_key
|
| 35 |
+
|
| 36 |
app.state.limiter = limiter
|
| 37 |
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
| 38 |
app.include_router(api_router)
|
app/api/factcheck_router.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
-
from fastapi import APIRouter, HTTPException
|
|
|
|
| 2 |
from app.models.factcheck_schemas import FactCheckRequest, FactCheckResponse, FactCheckSource
|
| 3 |
from app.services.factcheck_service import analyze_with_gemini_grounding
|
| 4 |
|
|
@@ -8,7 +9,8 @@ router = APIRouter()
|
|
| 8 |
"/factcheck",
|
| 9 |
response_model=FactCheckResponse,
|
| 10 |
tags=["Fact-checking"],
|
| 11 |
-
summary="Zweryfikuj prawdziwość stwierdzenia"
|
|
|
|
| 12 |
)
|
| 13 |
async def fact_check_endpoint(payload: FactCheckRequest):
|
| 14 |
statement = payload.statement.strip()
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Depends, HTTPException
|
| 2 |
+
from app import verify_api_key
|
| 3 |
from app.models.factcheck_schemas import FactCheckRequest, FactCheckResponse, FactCheckSource
|
| 4 |
from app.services.factcheck_service import analyze_with_gemini_grounding
|
| 5 |
|
|
|
|
| 9 |
"/factcheck",
|
| 10 |
response_model=FactCheckResponse,
|
| 11 |
tags=["Fact-checking"],
|
| 12 |
+
summary="Zweryfikuj prawdziwość stwierdzenia",
|
| 13 |
+
dependencies=[Depends(verify_api_key)]
|
| 14 |
)
|
| 15 |
async def fact_check_endpoint(payload: FactCheckRequest):
|
| 16 |
statement = payload.statement.strip()
|
app/api/routes.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
import asyncio
|
| 2 |
from collections import defaultdict
|
| 3 |
import logging
|
| 4 |
-
from fastapi import APIRouter, HTTPException, Request, status
|
|
|
|
| 5 |
from app.services.queue import get_queue_service
|
| 6 |
from slowapi.errors import RateLimitExceeded
|
| 7 |
from limits import parse
|
|
@@ -68,8 +69,7 @@ async def health_check() -> HealthResponse:
|
|
| 68 |
models_status=models_status,
|
| 69 |
)
|
| 70 |
|
| 71 |
-
|
| 72 |
-
@router.post("/guilds/{guild_id}/setup", tags=["Setup"])
|
| 73 |
async def save_discord_guild_setup(guild_id: str, payload: GuildConfigSchema):
|
| 74 |
# Walidacja modeli z pliku ustawień
|
| 75 |
settings = get_settings()
|
|
@@ -102,7 +102,7 @@ async def save_discord_guild_setup(guild_id: str, payload: GuildConfigSchema):
|
|
| 102 |
"config": config_dict,
|
| 103 |
}
|
| 104 |
|
| 105 |
-
@router.get("/guilds/{guild_id}/config", tags=["Setup"])
|
| 106 |
async def get_discord_guild_config(guild_id: str):
|
| 107 |
"""Zwraca zapisaną konfigurację dla konkretnego serwera Discord."""
|
| 108 |
configs = _load_all_configs()
|
|
@@ -170,6 +170,7 @@ async def _execute_analysis(payload: AnalysisRequest, guild_id: str, settings) -
|
|
| 170 |
},
|
| 171 |
tags=["Analysis"],
|
| 172 |
summary="Analyze content for deepfake detection",
|
|
|
|
| 173 |
)
|
| 174 |
async def analyze(request: Request, payload: AnalysisRequest) -> AnalysisResponse:
|
| 175 |
guild_id = payload.guild_id
|
|
|
|
| 1 |
import asyncio
|
| 2 |
from collections import defaultdict
|
| 3 |
import logging
|
| 4 |
+
from fastapi import APIRouter, Depends, HTTPException, Request, status
|
| 5 |
+
from app import verify_api_key
|
| 6 |
from app.services.queue import get_queue_service
|
| 7 |
from slowapi.errors import RateLimitExceeded
|
| 8 |
from limits import parse
|
|
|
|
| 69 |
models_status=models_status,
|
| 70 |
)
|
| 71 |
|
| 72 |
+
@router.post("/guilds/{guild_id}/setup", tags=["Setup"], dependencies=[Depends(verify_api_key)])
|
|
|
|
| 73 |
async def save_discord_guild_setup(guild_id: str, payload: GuildConfigSchema):
|
| 74 |
# Walidacja modeli z pliku ustawień
|
| 75 |
settings = get_settings()
|
|
|
|
| 102 |
"config": config_dict,
|
| 103 |
}
|
| 104 |
|
| 105 |
+
@router.get("/guilds/{guild_id}/config", tags=["Setup"], dependencies=[Depends(verify_api_key)])
|
| 106 |
async def get_discord_guild_config(guild_id: str):
|
| 107 |
"""Zwraca zapisaną konfigurację dla konkretnego serwera Discord."""
|
| 108 |
configs = _load_all_configs()
|
|
|
|
| 170 |
},
|
| 171 |
tags=["Analysis"],
|
| 172 |
summary="Analyze content for deepfake detection",
|
| 173 |
+
dependencies=[Depends(verify_api_key)]
|
| 174 |
)
|
| 175 |
async def analyze(request: Request, payload: AnalysisRequest) -> AnalysisResponse:
|
| 176 |
guild_id = payload.guild_id
|