Hkoradiya123 commited on
Commit
cb3a4d9
ยท
1 Parent(s): f46e5bb

feat: Enhance Docker setup with Nginx and curl installation; improve backend startup process and health check

Browse files
Dockerfile CHANGED
@@ -32,10 +32,11 @@ ENV PYTHONUNBUFFERED=1
32
 
33
  WORKDIR /app
34
 
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
 
32
 
33
  WORKDIR /app
34
 
35
+ # Install Nginx and curl
36
  RUN apt-get update && apt-get install -y --no-install-recommends \
37
  nginx \
38
  ca-certificates \
39
+ curl \
40
  && rm -rf /var/lib/apt/lists/*
41
 
42
  # Copy and install Python dependencies
backend/app/utils/firestore_data.py CHANGED
@@ -72,6 +72,11 @@ def _client() -> MongoClient:
72
  return _mongo_client
73
 
74
 
 
 
 
 
 
75
  def _collection(name: str) -> Collection:
76
  db = _client()[_mongo_db_name()]
77
  return db[name]
 
72
  return _mongo_client
73
 
74
 
75
+ def init_db() -> MongoClient:
76
+ """Initialize and return MongoDB connection. Safe to call multiple times."""
77
+ return _client()
78
+
79
+
80
  def _collection(name: str) -> Collection:
81
  db = _client()[_mongo_db_name()]
82
  return db[name]
backend/main.py CHANGED
@@ -6,7 +6,8 @@ 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,
@@ -53,6 +54,18 @@ app.include_router(notifications_router)
53
  app.include_router(matches_router)
54
 
55
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  @app.get("/")
57
  async def root():
58
  """API root endpoint"""
 
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, init_db
10
+ from app.utils.logger import logger
11
  from app.routes import (
12
  auth_router,
13
  players_router,
 
54
  app.include_router(matches_router)
55
 
56
 
57
+ @app.on_event("startup")
58
+ async def startup_event():
59
+ """Initialize database connection on startup"""
60
+ logger.info("๐Ÿš€ Running startup initialization...")
61
+ try:
62
+ init_db()
63
+ logger.info("โœ… Database connected and ready")
64
+ except Exception as e:
65
+ logger.error(f"โŒ DB init failed: {str(e)}")
66
+ # Continue anyway - routes will handle connection errors
67
+
68
+
69
  @app.get("/")
70
  async def root():
71
  """API root endpoint"""
docker-entrypoint.sh CHANGED
@@ -27,9 +27,24 @@ EOF
27
  # Start backend in background
28
  echo "Starting backend..."
29
  cd /app/backend
30
- gunicorn main:app -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000 --workers 1 --timeout 90 --access-logfile - --error-logfile - --capture-output --log-level info &
 
 
 
 
 
 
 
 
 
 
31
  BACKEND_PID=$!
32
 
 
 
 
 
 
33
  # Start Nginx in foreground
34
  echo "Starting Nginx..."
35
  cd /app
 
27
  # Start backend in background
28
  echo "Starting backend..."
29
  cd /app/backend
30
+ gunicorn main:app \
31
+ -k uvicorn.workers.UvicornWorker \
32
+ --bind 0.0.0.0:8000 \
33
+ --workers 2 \
34
+ --threads 2 \
35
+ --timeout 30 \
36
+ --preload \
37
+ --access-logfile - \
38
+ --error-logfile - \
39
+ --capture-output \
40
+ --log-level info &
41
  BACKEND_PID=$!
42
 
43
+ # Warmup backend
44
+ echo "Warming up backend..."
45
+ sleep 3
46
+ curl -s http://127.0.0.1:8000/health || true
47
+
48
  # Start Nginx in foreground
49
  echo "Starting Nginx..."
50
  cd /app
nginx-huggingface.conf CHANGED
@@ -10,27 +10,27 @@ server {
10
 
11
  # API proxy to backend
12
  location /api/ {
13
- proxy_pass http://localhost:8000/;
14
  proxy_http_version 1.1;
15
  proxy_set_header Host $host;
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
 
25
  # Swagger/OpenAPI docs
26
  location /docs {
27
- proxy_pass http://localhost:8000/docs;
28
  proxy_http_version 1.1;
29
  proxy_set_header Host $host;
30
  }
31
 
32
  location /openapi.json {
33
- proxy_pass http://localhost:8000/openapi.json;
34
  proxy_http_version 1.1;
35
  proxy_set_header Host $host;
36
  }
 
10
 
11
  # API proxy to backend
12
  location /api/ {
13
+ proxy_pass http://127.0.0.1:8000/;
14
  proxy_http_version 1.1;
15
  proxy_set_header Host $host;
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 5s;
20
+ proxy_read_timeout 10s;
21
+ proxy_send_timeout 10s;
22
  proxy_redirect off;
23
  }
24
 
25
  # Swagger/OpenAPI docs
26
  location /docs {
27
+ proxy_pass http://127.0.0.1:8000/docs;
28
  proxy_http_version 1.1;
29
  proxy_set_header Host $host;
30
  }
31
 
32
  location /openapi.json {
33
+ proxy_pass http://127.0.0.1:8000/openapi.json;
34
  proxy_http_version 1.1;
35
  proxy_set_header Host $host;
36
  }