-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathwebapp.py
More file actions
61 lines (53 loc) · 1.82 KB
/
webapp.py
File metadata and controls
61 lines (53 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from flask import Flask
import os
import logging
import pymongo
from datetime import datetime, timezone
from config import MONGODB_URL
from typing import Dict, Any
logger = logging.getLogger(__name__)
app = Flask(__name__)
def check_mongodb_connection() -> bool:
"""Check MongoDB connection and return True if connected."""
try:
client = pymongo.MongoClient(MONGODB_URL)
client.server_info() # This will raise an exception if not connected
logger.info("MongoDB connection successful")
return True
except Exception as e:
logger.error(f"MongoDB connection failed: {str(e)}")
return False
def get_system_status() -> Dict[str, Any]:
"""Get system status information."""
return {
"mongodb_connected": check_mongodb_connection(),
"timestamp": datetime.now(timezone.utc).isoformat(),
}
@app.route('/')
def home():
logger.info("Home endpoint accessed")
return "Bot is running!", 200
@app.route('/health')
def health():
logger.info("Health check endpoint accessed")
status = get_system_status()
if status["mongodb_connected"]:
return {"status": "healthy", **status}, 200
return {"status": "unhealthy", **status}, 503
@app.route('/kaithheathcheck')
def kaithheathcheck():
"""Leapcell.io specific health check endpoint"""
logger.info("Leapcell.io health check endpoint accessed")
status = get_system_status()
if status["mongodb_connected"]:
return "OK", 200
return "Service Unavailable", 503
def run_flask():
"""Start the Flask app in development mode"""
port = int(os.environ.get('PORT', 8080))
# Only run Flask directly in development
if os.environ.get('FLASK_ENV') == 'development':
app.run(host='0.0.0.0', port=port, debug=True)
else:
# In production, we use gunicorn
pass