v1.0.93: separate DB_* env vars (DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD, DB_SSLMODE) + TEST endpoint default

This commit is contained in:
2026-07-28 09:50:00 +04:00
parent f175319148
commit 5e120db0d6
4 changed files with 29 additions and 13 deletions
+1 -1
View File
@@ -18,7 +18,7 @@ from routes.api import bp as api_bp
from routes.api_test import bp as api_test_bp
# Версия — меняется при КАЖДОМ изменении кода. Показывается в топбаре UI.
VERSION = "1.0.92"
VERSION = "1.0.93"
app = Flask(__name__, template_folder="templates", static_folder="static")
app.config["NUBES_API_ENDPOINT"] = os.getenv("NUBES_API_ENDPOINT", "https://lk-api-gateway-test.ngcloud.ru/api/v1/svc")
+2 -2
View File
@@ -49,8 +49,8 @@ CREATE INDEX IF NOT EXISTS idx_runs_created
def init_db():
"""Применить схему — идемпотентно, безопасно для конкурентного вызова."""
if not os.getenv("DATABASE_URL"):
print("[DB] DATABASE_URL not set — skipping init", flush=True)
if not os.getenv("DB_USER"):
print("[DB] DB_USER not set — skipping init", flush=True)
return
conn = get_conn()
+20 -9
View File
@@ -2,10 +2,14 @@
Connection pool для PostgreSQL (psycopg2).
Lazy-init: пул создаётся при первом обращении к БД в каждом gunicorn-воркере.
Безопасно для fork-модели — соединения открываются ПОСЛЕ fork.
Переменная окружения:
DATABASE_URL — postgresql://user:pass@host:5432/dbname?sslmode=require
Переменные окружения (КАЖДАЯ отдельно):
DB_HOST — хост
DB_PORT — порт (5432)
DB_NAME — имя БД
DB_USER — пользователь
DB_PASSWORD — пароль
DB_SSLMODE — sslmode (require)
"""
import os
@@ -15,25 +19,32 @@ from psycopg2 import pool
_pool = None
def _dsn():
return (
f"host={os.getenv('DB_HOST')} "
f"port={os.getenv('DB_PORT', '5432')} "
f"dbname={os.getenv('DB_NAME')} "
f"user={os.getenv('DB_USER')} "
f"password={os.getenv('DB_PASSWORD')} "
f"sslmode={os.getenv('DB_SSLMODE', 'require')}"
)
def get_pool():
"""Lazy-init ThreadedConnectionPool (1-5 соединений)."""
global _pool
if _pool is None:
dsn = os.getenv("DATABASE_URL", "")
if not dsn:
if not os.getenv("DB_USER"):
return None
_pool = pool.ThreadedConnectionPool(1, 5, dsn)
_pool = pool.ThreadedConnectionPool(1, 5, _dsn())
return _pool
def get_conn():
"""Взять соединение из пула."""
p = get_pool()
return p.getconn() if p else None
def put_conn(conn):
"""Вернуть соединение в пул."""
p = get_pool()
if p and conn:
p.putconn(conn)