File size: 3,965 Bytes
8b7b267
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Main entry point for HuggingFace Space
Loads the unified API server with all endpoints
Runs with uvicorn on port 7860 (Hugging Face Spaces standard)
"""
import os
import logging
from pathlib import Path
import sys

# Setup logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# Add current directory to path
current_dir = Path(__file__).resolve().parent
sys.path.insert(0, str(current_dir))

# Configuration
HOST = os.getenv("HOST", "0.0.0.0")
PORT = int(os.getenv("PORT", os.getenv("HF_PORT", "7860")))

# Import the unified server app with fallback
try:
    from hf_unified_server import app
    logger.info("βœ… Loaded hf_unified_server")
except ImportError as e:
    logger.warning(f"⚠️ Error importing hf_unified_server: {e}")
    logger.info("Falling back to basic app...")
    # Fallback to basic FastAPI app
    try:
        from fastapi import FastAPI
        app = FastAPI(title="Crypto API - Fallback Mode")
        
        @app.get("/health")
        def health():
            return {
                "status": "fallback",
                "message": "Server is running in fallback mode",
                "error": str(e)
            }
        
        @app.get("/")
        def root():
            return {
                "message": "Cryptocurrency Data API - Fallback Mode",
                "note": "Main server module not available"
            }
        logger.info("βœ… Fallback FastAPI app created")
    except ImportError as fastapi_error:
        logger.error(f"❌ FastAPI not available: {fastapi_error}")
        logger.error("Please install: pip install fastapi uvicorn")
        sys.exit(1)
except Exception as e:
    logger.error(f"❌ Unexpected error loading server: {e}")
    import traceback
    traceback.print_exc()
    # Still create fallback app
    from fastapi import FastAPI
    app = FastAPI(title="Crypto API - Error Mode")
    
    @app.get("/health")
    def health():
        return {"status": "error", "message": str(e)}

# Export app for uvicorn
__all__ = ["app"]

# Run server if executed directly
if __name__ == "__main__":
    try:
        import uvicorn
        
        logger.info("=" * 70)
        logger.info("πŸš€ Starting FastAPI Server with Uvicorn")
        logger.info("=" * 70)
        logger.info(f"πŸ“ Host: {HOST}")
        logger.info(f"πŸ“ Port: {PORT}")
        logger.info(f"🌐 Server URL: http://{HOST}:{PORT}")
        logger.info(f"πŸ“Š Dashboard: http://{HOST}:{PORT}/")
        logger.info(f"πŸ“š API Docs: http://{HOST}:{PORT}/docs")
        logger.info(f"πŸ“Š System Monitor: http://{HOST}:{PORT}/system-monitor")
        logger.info("=" * 70)
        logger.info("")
        logger.info("πŸ’‘ Tips:")
        logger.info("   - Press Ctrl+C to stop the server")
        logger.info("   - Set PORT environment variable to change port")
        logger.info("   - Set HOST environment variable to change host")
        logger.info("")
        
        uvicorn.run(
            "main:app",  # Use string reference for better reload support
            host=HOST,
            port=PORT,
            log_level="info",
            access_log=True,
            # Optimizations for production
            timeout_keep_alive=30,
            limit_concurrency=100,
            limit_max_requests=1000,
            # Reload only in development (if DEBUG env var is set)
            reload=os.getenv("DEBUG", "false").lower() == "true"
        )
    except ImportError:
        logger.error("❌ uvicorn not installed")
        logger.error("Please install with: pip install uvicorn")
        sys.exit(1)
    except KeyboardInterrupt:
        logger.info("")
        logger.info("πŸ›‘ Server stopped by user")
        sys.exit(0)
    except Exception as e:
        logger.error(f"❌ Server startup failed: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)