feat(api): make Werkzeug debug opt-in via CM_DEBUG
This commit is contained in:
parent
7cea119ad7
commit
c3f02b36b9
@ -1,9 +1,20 @@
|
|||||||
|
import os
|
||||||
import threading
|
import threading
|
||||||
from flask import Flask, jsonify, request
|
from flask import Flask, jsonify, request
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
from .db import DB
|
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:
|
class CM_API:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -157,14 +168,16 @@ class CM_API:
|
|||||||
finally:
|
finally:
|
||||||
self._close_database_connection(db)
|
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 database connection before starting server
|
||||||
test_db = self._get_database_connection()
|
test_db = self._get_database_connection()
|
||||||
if test_db is None:
|
if test_db is None:
|
||||||
print("Cannot start server: Database not available")
|
print("Cannot start server: Database not available")
|
||||||
exit(1)
|
exit(1)
|
||||||
self._close_database_connection(test_db)
|
self._close_database_connection(test_db)
|
||||||
|
|
||||||
print(f'CM Bot DB API Listening at Port : {port}')
|
print(f'CM Bot DB API Listening at Port : {port}')
|
||||||
self.app.run(host='0.0.0.0', port=port, debug=debug)
|
self.app.run(host='0.0.0.0', port=port, debug=debug)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user