-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·102 lines (80 loc) · 2.87 KB
/
main.py
File metadata and controls
executable file
·102 lines (80 loc) · 2.87 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
"""SmarterRouter - AI-powered LLM router.
Main entry point that creates the FastAPI application and wires together
all components: middleware, API routers, lifecycle events, and shared state.
"""
import logging
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from router.api import admin_router, chat_router, health_router, models_router
from router.config import settings
from router.lifecycle import lifespan
from router.middleware import register_middleware
# Re-export shared state and utilities for test compatibility
from router.state import (
app_state,
get_settings,
verify_admin_token,
_get_client_ip,
_ip_in_whitelist,
)
logger = logging.getLogger(__name__)
# Create FastAPI app with lifespan management
app = FastAPI(
title="SmarterRouter",
description="AI-powered LLM router that intelligently selects the best model",
version="2.2.5",
lifespan=lifespan,
)
# CORS middleware (Item #22)
# Parse comma-separated origins
cors_origins_list = [origin.strip() for origin in settings.cors_origins.split(",")]
app.add_middleware(
CORSMiddleware,
allow_origins=cors_origins_list,
allow_credentials=settings.cors_allow_credentials,
allow_methods=[method.strip() for method in settings.cors_allow_methods.split(",")],
allow_headers=[header.strip() for header in settings.cors_allow_headers.split(",")],
max_age=settings.cors_max_age,
)
logger.info(f"CORS configured with origins: {cors_origins_list}")
# Optional gzip compression
# Note: Import inside conditional to avoid hard dependency
# This allows the app to start even if starlette is not installed
# (though it should be in requirements.txt)
try:
from starlette.middleware.gzip import GZipMiddleware
from router.config import settings
if settings.enable_response_compression:
app.add_middleware(
GZipMiddleware, minimum_size=settings.compression_minimum_size
)
logger.info("GZip compression enabled")
except ImportError:
logger.warning("Starlette not available, GZip compression disabled")
# Register all middleware
register_middleware(app)
# Include all API routers
app.include_router(health_router)
app.include_router(models_router)
app.include_router(chat_router)
app.include_router(admin_router)
logger.info("SmarterRouter application initialized")
# Entry points for running the server
async def main_async() -> None:
"""Async entry point for running the server programmatically."""
import uvicorn
from router.config import settings
config = uvicorn.Config(
"main:app",
host=settings.host,
port=settings.port,
reload=False,
)
server = uvicorn.Server(config)
await server.serve()
def main() -> None:
"""Synchronous entry point for running from command line."""
import asyncio
asyncio.run(main_async())
if __name__ == "__main__":
main()