51 lines
No EOL
1.6 KiB
Python
51 lines
No EOL
1.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
WSGI entry point for production servers (Waitress, Gunicorn, etc.).
|
|
"""
|
|
import asyncio
|
|
import logging
|
|
from a2wsgi import ASGIMiddleware
|
|
from app.core.app import create_app
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Create the FastAPI application instance
|
|
app = create_app()
|
|
|
|
def create_wsgi_app():
|
|
"""
|
|
Create a WSGI application with route refresh on startup.
|
|
|
|
This function is intended for production WSGI servers (e.g., Waitress).
|
|
Since WSGI does not support ASGI lifespan events, we manually refresh
|
|
routes from the database once when the WSGI app is created.
|
|
"""
|
|
loop = None
|
|
try:
|
|
loop = asyncio.new_event_loop()
|
|
asyncio.set_event_loop(loop)
|
|
route_manager = app.state.route_manager
|
|
logger.info("Refreshing routes from database (WSGI startup)...")
|
|
loop.run_until_complete(route_manager.refresh_routes())
|
|
logger.info(f"Registered {len(route_manager.registered_routes)} routes")
|
|
except Exception as e:
|
|
logger.warning(f"Failed to refresh routes on startup: {e}")
|
|
# Continue anyway; routes can be refreshed later via admin interface
|
|
finally:
|
|
if loop is not None:
|
|
loop.close()
|
|
|
|
# Wrap FastAPI ASGI app with WSGI adapter
|
|
wsgi_app = ASGIMiddleware(app)
|
|
return wsgi_app
|
|
|
|
# Create WSGI application
|
|
wsgi_app = create_wsgi_app()
|
|
|
|
if __name__ == "__main__":
|
|
# For testing WSGI app directly
|
|
from wsgiref.simple_server import make_server
|
|
print("Starting WSGI server on http://localhost:8000")
|
|
server = make_server('localhost', 8000, wsgi_app)
|
|
server.serve_forever() |