2026-01-29 18:38:57 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2025-09-18 07:45:53 +00:00
|
|
|
import os
|
2024-12-31 03:42:51 +00:00
|
|
|
import sys
|
2026-01-29 18:38:57 +00:00
|
|
|
from typing import TYPE_CHECKING, cast
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from celery import Celery
|
|
|
|
|
|
|
|
|
|
celery: Celery
|
2024-12-18 01:05:31 +00:00
|
|
|
|
|
|
|
|
|
2025-11-12 11:27:27 +00:00
|
|
|
def is_db_command() -> bool:
|
2024-12-18 01:05:31 +00:00
|
|
|
if len(sys.argv) > 1 and sys.argv[0].endswith("flask") and sys.argv[1] == "db":
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
2023-05-15 00:51:32 +00:00
|
|
|
|
|
|
|
|
# create app
|
2025-09-20 03:15:13 +00:00
|
|
|
flask_app = None
|
|
|
|
|
socketio_app = None
|
|
|
|
|
|
2024-12-18 01:05:31 +00:00
|
|
|
if is_db_command():
|
|
|
|
|
from app_factory import create_migrations_app
|
|
|
|
|
|
|
|
|
|
app = create_migrations_app()
|
2025-09-20 03:15:13 +00:00
|
|
|
socketio_app = app
|
|
|
|
|
flask_app = app
|
2024-12-18 01:05:31 +00:00
|
|
|
else:
|
2025-10-30 07:43:08 +00:00
|
|
|
# Gunicorn and Celery handle monkey patching automatically in production by
|
|
|
|
|
# specifying the `gevent` worker class. Manual monkey patching is not required here.
|
2025-09-18 04:49:10 +00:00
|
|
|
#
|
2025-10-30 07:43:08 +00:00
|
|
|
# See `api/docker/entrypoint.sh` (lines 33 and 47) for details.
|
2025-09-18 04:49:10 +00:00
|
|
|
#
|
2025-10-30 07:43:08 +00:00
|
|
|
# For third-party library patching, refer to `gunicorn.conf.py` and `celery_entrypoint.py`.
|
2024-12-31 06:45:59 +00:00
|
|
|
|
2024-12-31 03:42:51 +00:00
|
|
|
from app_factory import create_app
|
2024-12-18 01:05:31 +00:00
|
|
|
|
2025-09-20 03:15:13 +00:00
|
|
|
socketio_app, flask_app = create_app()
|
|
|
|
|
app = flask_app
|
2026-01-30 12:08:35 +00:00
|
|
|
celery = cast("Celery", flask_app.extensions["celery"])
|
2023-05-15 00:51:32 +00:00
|
|
|
|
2024-08-15 04:54:05 +00:00
|
|
|
if __name__ == "__main__":
|
2025-07-21 06:57:03 +00:00
|
|
|
from gevent import pywsgi
|
2026-01-17 07:54:32 +00:00
|
|
|
from geventwebsocket.handler import WebSocketHandler # type: ignore[reportMissingTypeStubs]
|
2025-07-21 06:57:03 +00:00
|
|
|
|
2025-07-21 08:13:04 +00:00
|
|
|
host = os.environ.get("HOST", "0.0.0.0")
|
|
|
|
|
port = int(os.environ.get("PORT", 5001))
|
2025-09-20 03:15:13 +00:00
|
|
|
server = pywsgi.WSGIServer((host, port), socketio_app, handler_class=WebSocketHandler)
|
2025-07-21 06:57:03 +00:00
|
|
|
server.serve_forever()
|