-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
217 lines (187 loc) · 6.93 KB
/
Copy pathmain.py
File metadata and controls
217 lines (187 loc) · 6.93 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python3
"""
Production server for Speed Read application.
Serves both the frontend static files and backend API.
"""
import os
import sys
import subprocess
from pathlib import Path
# Add src to path for imports
sys.path.insert(0, str(Path(__file__).parent / "src"))
import uvicorn
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from backend.agui_integration import app as backend_app
from dotenv import load_dotenv
import logging
# Load environment variables
load_dotenv()
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Create main app
app = FastAPI(title="Speed Read Production Server")
# Get absolute paths for production deployment
base_dir = Path(__file__).parent.absolute()
dist_path = base_dir / "dist"
assets_path = dist_path / "assets"
index_path = dist_path / "index.html"
# Build frontend if dist doesn't exist
if not dist_path.exists():
logger.info("Building frontend...")
try:
subprocess.run(["npm", "install"], check=True, cwd=base_dir)
subprocess.run(["npm", "run", "build"], check=True, cwd=base_dir)
logger.info("Frontend build completed")
except subprocess.CalledProcessError as e:
logger.error(f"Failed to build frontend: {e}")
sys.exit(1)
# Verify required files exist
if not index_path.exists():
logger.error(f"Frontend build failed: {index_path} not found")
sys.exit(1)
# Include backend routes directly instead of mounting
# This allows better control over route precedence
from backend.agui_integration import agui_handler
# Add backend routes to main app
@app.post("/api/extract")
async def extract_url_route(request: dict):
"""REST endpoint for URL extraction."""
try:
result = await agui_handler.handle_extract_and_prepare(request)
return result
except Exception as e:
return {"success": False, "error": str(e)}
@app.websocket("/ws/agui")
async def websocket_endpoint(websocket):
"""WebSocket endpoint for AG-UI protocol."""
from fastapi import WebSocket, WebSocketDisconnect
import logging
logger = logging.getLogger(__name__)
await websocket.accept()
try:
while True:
message = await websocket.receive_text()
response = await agui_handler.handle_message(message)
await websocket.send_text(response)
except WebSocketDisconnect:
logger.info("WebSocket disconnected")
except Exception as e:
logger.error(f"WebSocket error: {str(e)}")
await websocket.close()
# Serve static assets
if assets_path.exists():
app.mount("/assets", StaticFiles(directory=str(assets_path)), name="assets")
logger.info(f"Serving static assets from: {assets_path}")
else:
logger.warning(f"Assets directory not found: {assets_path}")
# Add health check endpoint at root level
@app.get("/health")
async def health_check():
"""Health check endpoint."""
return {"status": "healthy", "service": "speed-read-production"}
# Serve index.html for all other routes (SPA support)
# This MUST be last to serve as catch-all
@app.get("/{full_path:path}")
async def serve_spa(full_path: str):
"""Serve the SPA for all non-API and non-asset routes."""
# This will catch all routes not handled above
if index_path.exists():
return FileResponse(str(index_path))
else:
logger.error(f"Index file not found: {index_path}")
return {"error": "Frontend not built", "path": full_path}
def check_environment():
"""Check required environment variables and system readiness."""
logger.info("Performing environment and system checks...")
# Check required environment variables
required_vars = [
"FIRECRAWL_API_KEY",
"GROQ_API_KEY",
]
missing = []
for var in required_vars:
if not os.getenv(var):
missing.append(var)
if missing:
logger.warning(f"Missing environment variables: {', '.join(missing)}")
logger.warning("URL extraction feature will not work without these keys")
logger.warning("Please add them to your .env file or platform secrets")
else:
logger.info("✓ All required environment variables are set")
# Check if we're in a production environment
env = os.getenv("ENVIRONMENT", "development")
logger.info(f"Running in {env} mode")
# Check frontend build
if dist_path.exists() and index_path.exists():
logger.info("✓ Frontend build is ready")
else:
logger.warning("Frontend build may be missing")
# Check backend dependencies
try:
from backend.agui_integration import app as backend_app
logger.info("✓ Backend modules loaded successfully")
except ImportError as e:
logger.error(f"✗ Backend import failed: {e}")
sys.exit(1)
return True
def main():
"""Run the production server."""
logger.info("Starting Speed Read Production Server...")
check_environment()
# Server configuration
host = os.getenv("HOST", "0.0.0.0")
port = int(os.getenv("PORT", "8000"))
workers = int(os.getenv("WORKERS", "1"))
log_level = os.getenv("LOG_LEVEL", "info").lower()
# Production settings
is_production = os.getenv("ENVIRONMENT", "development").lower() == "production"
logger.info("=" * 60)
logger.info(f"🚀 Speed Read Production Server")
logger.info(f"Host: {host}:{port}")
logger.info(f"Base directory: {base_dir}")
logger.info(f"Frontend files: {dist_path}")
logger.info(f"Workers: {workers}")
logger.info(f"Log level: {log_level}")
logger.info(f"Production mode: {is_production}")
logger.info("=" * 60)
logger.info("📍 Available endpoints:")
logger.info(f" Frontend: http://{host}:{port}/")
logger.info(f" API: http://{host}:{port}/api/")
logger.info(f" WebSocket: ws://{host}:{port}/ws/agui")
logger.info(f" Health check: http://{host}:{port}/health")
logger.info("=" * 60)
try:
# Run the server
if is_production:
# Production mode with multiple workers
uvicorn.run(
"main:app", # Use import string for workers
host=host,
port=port,
log_level=log_level,
access_log=True,
workers=workers
)
else:
# Development mode with reload
uvicorn.run(
app,
host=host,
port=port,
log_level=log_level,
access_log=True,
reload=False # Disable reload for now to avoid import issues
)
except KeyboardInterrupt:
logger.info("Server shutdown requested")
except Exception as e:
logger.error(f"Server failed to start: {e}")
sys.exit(1)
if __name__ == "__main__":
main()