#!/usr/bin/env python3
"""
Startup script for Crawl4AI RAG API Server
Configurable to run in either server mode (hosting API) or client mode (forwarding to remote)
"""
import os
import sys
import uvicorn
from dotenv import load_dotenv
sys.path.insert(0, '/app')
load_dotenv()
def main():
"""Start the API server based on configuration"""
is_server = os.getenv("IS_SERVER", "true").lower() == "true"
if not is_server:
print("ā IS_SERVER=false: This script is for server mode only.")
print("š” For client mode, use: python3 core/rag_processor.py")
print(" The MCP client will automatically forward requests to the remote API.")
sys.exit(1)
host = os.getenv("SERVER_HOST", "0.0.0.0")
port = int(os.getenv("SERVER_PORT", "8080"))
log_level = os.getenv("LOG_LEVEL", "info").lower()
api_key = os.getenv("LOCAL_API_KEY")
if not api_key:
print("ā LOCAL_API_KEY not set in .env file")
print("š” Generate a secure API key and add it to .env:")
print(" LOCAL_API_KEY=your-secure-api-key-here")
sys.exit(1)
print("š Starting Crawl4AI RAG API Server")
print(f"š Server Mode: hosting REST API on {host}:{port}")
print(f"š API Key: {api_key[:8]}...")
print(f"š Database: {os.getenv('DB_PATH', 'crawl4ai_rag.db')}")
print(f"š Crawl4AI: {os.getenv('CRAWL4AI_URL', 'http://localhost:11235')}")
print("")
print("š API Documentation will be available at:")
print(f" http://{host}:{port}/docs")
print("")
from api.api import create_app
app = create_app()
try:
uvicorn.run(
app,
host=host,
port=port,
log_level=log_level,
access_log=True
)
except KeyboardInterrupt:
print("\nš Shutting down API server...")
except Exception as e:
print(f"ā Error starting server: {e}")
sys.exit(1)
if __name__ == "__main__":
main()