diff --git a/site/api/auth.py b/site/api/auth.py index 539e566..327140d 100644 --- a/site/api/auth.py +++ b/site/api/auth.py @@ -28,10 +28,26 @@ Cookie (устанавливаются main.py:action=set_mode): import base64 import json -from flask import request, current_app +from flask import request, current_app, g from api.http_client import HttpClient, detect_endpoint, stand_name, STANDS +def _cached_detect(token): + """detect_endpoint с кешированием на время запроса (flask.g). + + get_client() и get_stand() вызывают detect_endpoint() независимо — + это двойной HTTP-запрос к Nubes API. Кеш в g решает проблему.""" + cache = g.get('_detected_endpoint') + if cache is None: + g._detected_endpoint = cache = {} + if token not in cache: + endpoint = current_app.config["NUBES_API_ENDPOINT"] + if endpoint in STANDS: + endpoint = detect_endpoint(token) or endpoint + cache[token] = endpoint + return cache[token] + + def get_mode(): """Режим: 'polygon' (эмуляция) или 'cloud' (реальное облако). @@ -76,11 +92,8 @@ def get_client(): # Простая замена "/api/v1/svc" → "/{stand}/api/v1/svc" return HttpClient(polygon.replace("/api/v1/svc", f"/{stand}/api/v1/svc"), token) - # Облако: NUBES_API_ENDPOINT с автоопределением - endpoint = current_app.config["NUBES_API_ENDPOINT"] - if endpoint in STANDS: - endpoint = detect_endpoint(token) or endpoint - return HttpClient(endpoint, token) + # Облако: NUBES_API_ENDPOINT с автоопределением (кешированным) + return HttpClient(_cached_detect(token), token) def get_real_client(): @@ -131,8 +144,7 @@ def get_stand(): if endpoint not in STANDS: s = stand_name(endpoint) return s if s != "?" else "mock" - endpoint = detect_endpoint(token) or endpoint - return stand_name(endpoint) + return stand_name(_cached_detect(token)) def get_token_info(): diff --git a/site/app.py b/site/app.py index d16a64c..3fc2ed3 100644 --- a/site/app.py +++ b/site/app.py @@ -32,7 +32,7 @@ from routes.api_scenario_defs import bp_defs as api_scenario_defs_bp # Версия — показывается в топбаре UI. Меняется при КАЖДОМ изменении кода. # Нужна для фильтрации истории (пользователь видит только записи своей версии). -VERSION = "1.2.40" +VERSION = "1.2.41" # Flask-приложение с Jinja2-шаблонами из папки templates/ app = Flask(__name__, template_folder="templates", static_folder="static")