mockapi/wsgi.py
2026-03-16 05:47:01 +00:00

41 lines
No EOL
1.4 KiB
Python

"""
WSGI entry point for production deployment with Waitress.
Wraps the FastAPI ASGI application with ASGI-to-WSGI adapter using a2wsgi.
Also triggers route refresh on startup since WSGI doesn't support ASGI lifespan events.
"""
import asyncio
import logging
from a2wsgi import ASGIMiddleware
from app import app
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Refresh routes on startup (since WSGI doesn't call ASGI lifespan)
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...")
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)
# Function that returns the WSGI application (for --call)
def create_wsgi_app():
return wsgi_app
if __name__ == "__main__":
# This block is for running directly with python wsgi.py (development)
from waitress import serve
logger.info("Starting Waitress server on http://0.0.0.0:8000")
serve(wsgi_app, host="0.0.0.0", port=8000, threads=4)