mockapi/main.py
2026-03-16 10:49:01 +00:00

63 lines
No EOL
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
Unified entry point for the Mock API application.
This module serves as both the development entry point (via uvicorn) and the
production WSGI entry point (via Waitress or other WSGI servers).
"""
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
# Expose the WSGI application for production servers
wsgi_app = create_wsgi_app()
if __name__ == "__main__":
# Development entry point: run uvicorn with autoreload
import uvicorn
logger.info("Starting development server on http://0.0.0.0:8000")
uvicorn.run(
app,
host="0.0.0.0",
port=8000,
reload=True,
log_level="info"
)