"""Point d'entrée WSGI pour O2Switch (Phusion Passenger).

FastAPI est une app ASGI (callable app(scope, receive, send)), Passenger parle
WSGI (app(environ, start_response)). On adapte ASGI -> WSGI avec
`a2wsgi.ASGIMiddleware`, et on neutralise stdout/stderr pendant l'import
(sinon le handshake WSGI est corrompu => « Incomplete response »).
"""
import os
import sys
import io
import contextlib

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

try:
    from dotenv import load_dotenv
    load_dotenv(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".env"))
except Exception:
    pass

with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()):
    from main import app as asgi_app
    from a2wsgi import ASGIMiddleware

application = ASGIMiddleware(asgi_app)
