From c3f02b36b933a37c9724d52241160f04a481edf0 Mon Sep 17 00:00:00 2001 From: yiekheng Date: Sat, 2 May 2026 16:22:23 +0800 Subject: [PATCH] feat(api): make Werkzeug debug opt-in via CM_DEBUG --- app/cm_api.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/cm_api.py b/app/cm_api.py index 0bb59d9..bc2eb44 100644 --- a/app/cm_api.py +++ b/app/cm_api.py @@ -1,9 +1,20 @@ +import os import threading from flask import Flask, jsonify, request from flask_cors import CORS from .db import DB +def _debug_enabled() -> bool: + """Return True iff CM_DEBUG env var is set to a truthy value. + + Truthy: '1', 'true', 'yes' (case-insensitive, whitespace-trimmed). + Anything else, including unset, is False. Default-off so the + Werkzeug debugger is never reachable in production containers. + """ + return os.getenv("CM_DEBUG", "false").strip().lower() in ("1", "true", "yes") + + class CM_API: def __init__(self): @@ -157,14 +168,16 @@ class CM_API: finally: self._close_database_connection(db) - def run(self, port=3000, debug=True): + def run(self, port=3000, debug=None): + if debug is None: + debug = _debug_enabled() # Test database connection before starting server test_db = self._get_database_connection() if test_db is None: print("Cannot start server: Database not available") exit(1) self._close_database_connection(test_db) - + print(f'CM Bot DB API Listening at Port : {port}') self.app.run(host='0.0.0.0', port=port, debug=debug)