app.py 1.45 KB
"""
Flask main application entrypoint for the BettaFish web API.
"""

import sys

try:
    from .bootstrap.import_path import ensure_project_root_on_sys_path
except ImportError:  # pragma: no cover
    from bootstrap.import_path import ensure_project_root_on_sys_path

PROJECT_ROOT = ensure_project_root_on_sys_path(__file__, sys.path)

from apps.web_api.bootstrap.app_module import bootstrap_app_module
from services.shared.config import get_settings
from services.shared.logging import bind_logger

_PREVIOUS_APP_MODULE = globals().get("_APP_MODULE")
_APP_MODULE = bootstrap_app_module(
    previous_cleanup_handler=getattr(_PREVIOUS_APP_MODULE, "cleanup_handler", None),
)

app = _APP_MODULE.app
socketio = _APP_MODULE.socketio


def main() -> int:
    """Start the Flask + Socket.IO server using configured host and port."""
    configured_settings = get_settings()
    host = configured_settings.HOST
    port = configured_settings.PORT
    startup_logger = bind_logger(source="web_api.app", host=host, port=port)

    startup_logger.info("Starting BettaFish web API server...")
    startup_logger.info(f"Flask service listening at: http://{host}:{port}")

    try:
        socketio.run(app, host=host, port=port, debug=False)
    except KeyboardInterrupt:
        startup_logger.info("\nShutdown requested via keyboard interrupt...")
        _APP_MODULE.cleanup_handler()
    return 0


__all__ = ["app", "socketio", "main"]


if __name__ == "__main__":
    raise SystemExit(main())