feat(api): add create_app factory for gunicorn entrypoint

This commit is contained in:
yiekheng 2026-05-02 17:37:13 +08:00
parent 74d496b2bc
commit d32e4ba58b
2 changed files with 30 additions and 0 deletions

View File

@ -199,6 +199,17 @@ class CM_API:
return thread return thread
def create_app():
"""WSGI factory used by gunicorn (`app.cm_api:create_app()`).
Returns the Flask app object so gunicorn can serve it. The
surrounding CM_API class still owns route registration and DB
connection management this just hands gunicorn the underlying
Flask instance.
"""
return CM_API().app
if __name__ == '__main__': if __name__ == '__main__':
api = CM_API() api = CM_API()
api.run(port = 3000) api.run(port = 3000)

View File

@ -289,5 +289,24 @@ class CmdInteractiveTests(unittest.TestCase):
self.assertIn("CM Bot CLI", out.getvalue()) self.assertIn("CM Bot CLI", out.getvalue())
class CreateAppFactoryTests(unittest.TestCase):
"""The gunicorn entrypoint loads `app.cm_api:create_app()`. The factory
must exist as a module-level callable that returns the Flask app
object not the CM_API wrapper class."""
def test_create_app_returns_flask_instance(self):
from flask import Flask
from app.cm_api import create_app
wsgi = create_app()
self.assertIsInstance(wsgi, Flask)
def test_create_app_registers_acc_route(self):
from app.cm_api import create_app
wsgi = create_app()
rules = {r.rule for r in wsgi.url_map.iter_rules()}
self.assertIn("/acc/", rules)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()