Spaces:
Sleeping
Sleeping
Hkoradiya123 commited on
Commit ·
f08a7bf
1
Parent(s): ec1f6b2
feat: Enhance Dockerfile and Dockerfile.huggingface with ca-certificates installation; improve MongoDB connection handling and error management in firestore_data.py; add exception handler for DatabaseUnavailableError in main.py; update nginx configuration with timeout settings.
Browse files- Dockerfile +1 -0
- Dockerfile.huggingface +1 -0
- backend/app/utils/firestore_data.py +26 -2
- backend/main.py +11 -0
- nginx-huggingface.conf +5 -0
Dockerfile
CHANGED
|
@@ -35,6 +35,7 @@ WORKDIR /app
|
|
| 35 |
# Install Nginx
|
| 36 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 37 |
nginx \
|
|
|
|
| 38 |
&& rm -rf /var/lib/apt/lists/*
|
| 39 |
|
| 40 |
# Copy and install Python dependencies
|
|
|
|
| 35 |
# Install Nginx
|
| 36 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 37 |
nginx \
|
| 38 |
+
ca-certificates \
|
| 39 |
&& rm -rf /var/lib/apt/lists/*
|
| 40 |
|
| 41 |
# Copy and install Python dependencies
|
Dockerfile.huggingface
CHANGED
|
@@ -35,6 +35,7 @@ WORKDIR /app
|
|
| 35 |
# Install Nginx
|
| 36 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 37 |
nginx \
|
|
|
|
| 38 |
&& rm -rf /var/lib/apt/lists/*
|
| 39 |
|
| 40 |
# Copy and install Python dependencies
|
|
|
|
| 35 |
# Install Nginx
|
| 36 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 37 |
nginx \
|
| 38 |
+
ca-certificates \
|
| 39 |
&& rm -rf /var/lib/apt/lists/*
|
| 40 |
|
| 41 |
# Copy and install Python dependencies
|
backend/app/utils/firestore_data.py
CHANGED
|
@@ -5,11 +5,14 @@ from datetime import datetime, timezone
|
|
| 5 |
from types import SimpleNamespace
|
| 6 |
from typing import Any, Callable
|
| 7 |
|
|
|
|
| 8 |
from bson import ObjectId
|
| 9 |
from pymongo import MongoClient, ReturnDocument
|
| 10 |
from pymongo.collection import Collection
|
|
|
|
| 11 |
|
| 12 |
from app.config import settings
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
@dataclass
|
|
@@ -31,6 +34,10 @@ COLL = CollectionNames()
|
|
| 31 |
_mongo_client: MongoClient | None = None
|
| 32 |
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def _mongo_db_name() -> str:
|
| 35 |
return settings.MONGODB_DB_NAME or "ssc"
|
| 36 |
|
|
@@ -42,9 +49,26 @@ def _client() -> MongoClient:
|
|
| 42 |
return _mongo_client
|
| 43 |
|
| 44 |
if not settings.MONGODB_URI:
|
| 45 |
-
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
_mongo_client = MongoClient(settings.MONGODB_URI, tz_aware=True)
|
| 48 |
return _mongo_client
|
| 49 |
|
| 50 |
|
|
|
|
| 5 |
from types import SimpleNamespace
|
| 6 |
from typing import Any, Callable
|
| 7 |
|
| 8 |
+
import certifi
|
| 9 |
from bson import ObjectId
|
| 10 |
from pymongo import MongoClient, ReturnDocument
|
| 11 |
from pymongo.collection import Collection
|
| 12 |
+
from pymongo.errors import PyMongoError
|
| 13 |
|
| 14 |
from app.config import settings
|
| 15 |
+
from app.utils.logger import logger
|
| 16 |
|
| 17 |
|
| 18 |
@dataclass
|
|
|
|
| 34 |
_mongo_client: MongoClient | None = None
|
| 35 |
|
| 36 |
|
| 37 |
+
class DatabaseUnavailableError(RuntimeError):
|
| 38 |
+
"""Raised when MongoDB is not configured or temporarily unavailable."""
|
| 39 |
+
|
| 40 |
+
|
| 41 |
def _mongo_db_name() -> str:
|
| 42 |
return settings.MONGODB_DB_NAME or "ssc"
|
| 43 |
|
|
|
|
| 49 |
return _mongo_client
|
| 50 |
|
| 51 |
if not settings.MONGODB_URI:
|
| 52 |
+
raise DatabaseUnavailableError("MONGODB_URI is not configured")
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
_mongo_client = MongoClient(
|
| 56 |
+
settings.MONGODB_URI,
|
| 57 |
+
tz_aware=True,
|
| 58 |
+
tlsCAFile=certifi.where(),
|
| 59 |
+
serverSelectionTimeoutMS=10000,
|
| 60 |
+
connectTimeoutMS=10000,
|
| 61 |
+
socketTimeoutMS=20000,
|
| 62 |
+
)
|
| 63 |
+
# Force an early connectivity check so failures surface clearly in API responses.
|
| 64 |
+
_mongo_client.admin.command("ping")
|
| 65 |
+
except PyMongoError as exc:
|
| 66 |
+
_mongo_client = None
|
| 67 |
+
logger.error(f"MongoDB connectivity check failed | Details: {str(exc)}")
|
| 68 |
+
raise DatabaseUnavailableError(
|
| 69 |
+
"Unable to connect to MongoDB. Verify MONGODB_URI and Atlas network access (allow Hugging Face egress IPs or 0.0.0.0/0)."
|
| 70 |
+
) from exc
|
| 71 |
|
|
|
|
| 72 |
return _mongo_client
|
| 73 |
|
| 74 |
|
backend/main.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from fastapi.openapi.utils import get_openapi
|
|
|
|
| 4 |
from app.config import settings
|
| 5 |
from app.middleware import LoggingMiddleware, RateLimitMiddleware
|
| 6 |
from app.middleware.auth import security
|
|
|
|
| 7 |
from app.routes import (
|
| 8 |
auth_router,
|
| 9 |
players_router,
|
|
@@ -90,6 +93,14 @@ def custom_openapi():
|
|
| 90 |
app.openapi = custom_openapi
|
| 91 |
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
if __name__ == "__main__":
|
| 94 |
import uvicorn
|
| 95 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from fastapi import Request
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from fastapi.openapi.utils import get_openapi
|
| 5 |
+
from fastapi.responses import JSONResponse
|
| 6 |
from app.config import settings
|
| 7 |
from app.middleware import LoggingMiddleware, RateLimitMiddleware
|
| 8 |
from app.middleware.auth import security
|
| 9 |
+
from app.utils.firestore_data import DatabaseUnavailableError
|
| 10 |
from app.routes import (
|
| 11 |
auth_router,
|
| 12 |
players_router,
|
|
|
|
| 93 |
app.openapi = custom_openapi
|
| 94 |
|
| 95 |
|
| 96 |
+
@app.exception_handler(DatabaseUnavailableError)
|
| 97 |
+
async def database_unavailable_handler(request: Request, exc: DatabaseUnavailableError):
|
| 98 |
+
return JSONResponse(
|
| 99 |
+
status_code=503,
|
| 100 |
+
content={"detail": str(exc)},
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
|
| 104 |
if __name__ == "__main__":
|
| 105 |
import uvicorn
|
| 106 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
nginx-huggingface.conf
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
server {
|
| 2 |
listen 7860;
|
| 3 |
server_name _;
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Serve frontend static files
|
| 6 |
root /app/frontend/dist;
|
|
@@ -14,6 +16,9 @@ server {
|
|
| 14 |
proxy_set_header X-Real-IP $remote_addr;
|
| 15 |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
| 16 |
proxy_set_header X-Forwarded-Proto $scheme;
|
|
|
|
|
|
|
|
|
|
| 17 |
proxy_redirect off;
|
| 18 |
}
|
| 19 |
|
|
|
|
| 1 |
server {
|
| 2 |
listen 7860;
|
| 3 |
server_name _;
|
| 4 |
+
access_log /dev/stdout;
|
| 5 |
+
error_log /dev/stderr info;
|
| 6 |
|
| 7 |
# Serve frontend static files
|
| 8 |
root /app/frontend/dist;
|
|
|
|
| 16 |
proxy_set_header X-Real-IP $remote_addr;
|
| 17 |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
| 18 |
proxy_set_header X-Forwarded-Proto $scheme;
|
| 19 |
+
proxy_connect_timeout 30s;
|
| 20 |
+
proxy_read_timeout 60s;
|
| 21 |
+
proxy_send_timeout 60s;
|
| 22 |
proxy_redirect off;
|
| 23 |
}
|
| 24 |
|