-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathstart-backend.py
More file actions
91 lines (71 loc) · 2.45 KB
/
Copy pathstart-backend.py
File metadata and controls
91 lines (71 loc) · 2.45 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
"""
VishwaGuru Backend Startup Script
Handles environment validation and application startup.
"""
import os
import sys
import uvicorn
from pathlib import Path
from dotenv import load_dotenv
# Load .env file from project root
load_dotenv(Path(__file__).parent / ".env")
# Add backend to Python path
backend_path = Path(__file__).parent / "backend"
sys.path.insert(0, str(backend_path))
def validate_environment():
"""Validate required environment variables"""
required_vars = ["TELEGRAM_BOT_TOKEN", "FRONTEND_URL"]
missing_vars = []
for var in required_vars:
if not os.getenv(var):
missing_vars.append(var)
# Check for at least one AI API key
if not os.getenv("NVIDIA_API_KEY") and not os.getenv("GEMINI_API_KEY"):
missing_vars.append("NVIDIA_API_KEY or GEMINI_API_KEY")
if missing_vars:
print("❌ Missing required environment variables:")
for var in missing_vars:
print(f" - {var}")
print("\nPlease set these variables or create a .env file.")
print("See backend/.env.example for reference.")
return False
# Set defaults for optional variables
if not os.getenv("DATABASE_URL"):
os.environ["DATABASE_URL"] = "sqlite:///./data/issues.db"
if not os.getenv("ENVIRONMENT"):
os.environ["ENVIRONMENT"] = "production"
if not os.getenv("DEBUG"):
os.environ["DEBUG"] = "false"
# Check for optional HF_TOKEN
if not os.getenv("HF_TOKEN"):
print("⚠️ HF_TOKEN is missing. AI features using Hugging Face will be disabled.")
else:
print("✅ HF_TOKEN found")
print("✅ Environment validation passed")
return True
def create_data_directory():
"""Create data directory for SQLite database"""
data_dir = Path("data")
data_dir.mkdir(exist_ok=True)
print("✅ Data directory ready")
def main():
"""Main startup function"""
print("🚀 Starting VishwaGuru Backend")
if not validate_environment():
sys.exit(1)
create_data_directory()
# Get port from environment or default to 8000
port = int(os.getenv("PORT", "8000"))
host = os.getenv("HOST", "0.0.0.0")
print(f"📡 Starting server on {host}:{port}")
# Start the server
uvicorn.run(
"backend.main_fixed:app",
host=host,
port=port,
reload=False, # Disable reload in production
log_level="info"
)
if __name__ == "__main__":
main()