diff --git a/app/api/v1/admin.py b/app/api/v1/admin.py index 5163485..22c2b69 100644 --- a/app/api/v1/admin.py +++ b/app/api/v1/admin.py @@ -1,6 +1,6 @@ from fastapi import APIRouter, Depends, HTTPException, Request, Query from fastapi.responses import HTMLResponse, StreamingResponse -from app.core.auth import verify_api_key, verify_app_key +from app.core.auth import verify_api_key, verify_app_key, get_admin_api_key from app.core.config import config, get_config from app.core.batch_tasks import create_task, get_task, expire_task from app.core.storage import get_storage, LocalStorage, RedisStorage, SQLStorage @@ -33,7 +33,7 @@ def _sse_event(payload: dict) -> str: def _verify_stream_api_key(request: Request) -> None: - api_key = get_config("app.api_key", "") + api_key = get_admin_api_key() if not api_key: return key = request.query_params.get("api_key") @@ -110,7 +110,7 @@ async def admin_token_page(): @router.post("/api/v1/admin/login", dependencies=[Depends(verify_app_key)]) async def admin_login_api(): """管理后台登录验证(使用 app_key)""" - return {"status": "success", "api_key": get_config("app.api_key", "")} + return {"status": "success", "api_key": get_admin_api_key()} @router.get("/api/v1/admin/config", dependencies=[Depends(verify_api_key)]) diff --git a/app/core/auth.py b/app/core/auth.py index 7c96460..a4c8735 100644 --- a/app/core/auth.py +++ b/app/core/auth.py @@ -16,15 +16,27 @@ security = HTTPBearer( ) +def get_admin_api_key() -> str: + """ + 获取后台 API Key。 + + 为空时表示不启用后台接口认证。 + """ + api_key = get_config("app.api_key", "") + return api_key or "" + + async def verify_api_key( auth: Optional[HTTPAuthorizationCredentials] = Security(security), ) -> Optional[str]: """ 验证 Bearer Token - 如果 config.toml 中未配置 api_key,则使用默认密钥 "grok2api"。 + 如果 config.toml 中未配置 api_key,则不启用认证。 """ - api_key = get_config("app.api_key", "grok2api") + api_key = get_admin_api_key() + if not api_key: + return None if not auth: raise HTTPException( diff --git a/app/static/config/config.js b/app/static/config/config.js index 56097cf..9c8beaa 100644 --- a/app/static/config/config.js +++ b/app/static/config/config.js @@ -4,6 +4,10 @@ const byId = (id) => document.getElementById(id); const NUMERIC_FIELDS = new Set([ 'timeout', 'max_retry', + 'retry_backoff_base', + 'retry_backoff_factor', + 'retry_backoff_max', + 'retry_budget', 'refresh_interval_hours', 'fail_threshold', 'limit_mb', @@ -17,6 +21,8 @@ const NUMERIC_FIELDS = new Set([ 'usage_batch_size', 'usage_max_tokens', 'reload_interval_sec', + 'stream_idle_timeout', + 'video_idle_timeout', 'nsfw_max_concurrent', 'nsfw_batch_size', 'nsfw_max_tokens' @@ -43,7 +49,13 @@ const LOCALE_MAP = { "asset_proxy_url": { title: "资源代理 URL", desc: "代理请求到 Grok 官网的静态资源(图片/视频)地址。" }, "cf_clearance": { title: "CF Clearance", desc: "Cloudflare 验证 Cookie,用于验证 Cloudflare 的验证。" }, "max_retry": { title: "最大重试", desc: "请求 Grok 服务失败时的最大重试次数。" }, - "retry_status_codes": { title: "重试状态码", desc: "触发重试的 HTTP 状态码列表。" } + "retry_status_codes": { title: "重试状态码", desc: "触发重试的 HTTP 状态码列表。" }, + "retry_backoff_base": { title: "退避基数", desc: "重试退避的基础延迟(秒)。" }, + "retry_backoff_factor": { title: "退避倍率", desc: "重试退避的指数放大系数。" }, + "retry_backoff_max": { title: "退避上限", desc: "单次重试等待的最大延迟(秒)。" }, + "retry_budget": { title: "退避预算", desc: "单次请求的最大重试总耗时(秒)。" }, + "stream_idle_timeout": { title: "流空闲超时", desc: "流式响应空闲超时(秒),超过将断开。" }, + "video_idle_timeout": { title: "视频空闲超时", desc: "视频生成空闲超时(秒),超过将断开。" } }, "token": { "label": "Token 池设置", diff --git a/data/config.toml b/data/config.toml index da8be8e..82eee63 100644 --- a/data/config.toml +++ b/data/config.toml @@ -10,6 +10,12 @@ asset_proxy_url = "" cf_clearance = "" max_retry = 3 retry_status_codes = [401,429,403] +retry_backoff_base = 0.5 +retry_backoff_factor = 2.0 +retry_backoff_max = 30.0 +retry_budget = 90.0 +stream_idle_timeout = 45.0 +video_idle_timeout = 90.0 [app] app_url = "http://127.0.0.1:8000" diff --git a/docs/README.en.md b/docs/README.en.md index f5cb201..3505656 100644 --- a/docs/README.en.md +++ b/docs/README.en.md @@ -169,6 +169,12 @@ Config file: `data/config.toml` | | `cf_clearance` | CF Clearance | Cloudflare clearance cookie for verification. | `""` | | | `max_retry` | Max retries | Max retries on Grok request failure. | `3` | | | `retry_status_codes` | Retry status codes | HTTP status codes that trigger retry. | `[401, 429, 403]` | +| | `retry_backoff_base` | Backoff base | Base delay for retry backoff (seconds). | `0.5` | +| | `retry_backoff_factor` | Backoff factor | Exponential multiplier for retry backoff. | `2.0` | +| | `retry_backoff_max` | Backoff max | Max wait per retry (seconds). | `30.0` | +| | `retry_budget` | Backoff budget | Max total retry time per request (seconds). | `90.0` | +| | `stream_idle_timeout` | Stream idle timeout | Idle timeout for streaming responses (seconds). | `45.0` | +| | `video_idle_timeout` | Video idle timeout | Idle timeout for video generation (seconds). | `90.0` | | **token** | `auto_refresh` | Auto refresh | Enable automatic token refresh. | `true` | | | `refresh_interval_hours` | Refresh interval | Token refresh interval (hours). | `8` | | | `fail_threshold` | Failure threshold | Consecutive failures before a token is disabled. | `5` | diff --git a/readme.md b/readme.md index 976ffa2..3f15261 100644 --- a/readme.md +++ b/readme.md @@ -202,6 +202,12 @@ curl http://localhost:8000/v1/images/generations \ | | `cf_clearance` | CF Clearance | Cloudflare 验证 Cookie,用于验证 Cloudflare 的验证。 | `""` | | | `max_retry` | 最大重试 | 请求 Grok 服务失败时的最大重试次数。 | `3` | | | `retry_status_codes` | 重试状态码 | 触发重试的 HTTP 状态码列表。 | `[401, 429, 403]` | +| | `retry_backoff_base` | 退避基数 | 重试退避的基础延迟(秒)。 | `0.5` | +| | `retry_backoff_factor` | 退避倍率 | 重试退避的指数放大系数。 | `2.0` | +| | `retry_backoff_max` | 退避上限 | 单次重试等待的最大延迟(秒)。 | `30.0` | +| | `retry_budget` | 退避预算 | 单次请求的最大重试总耗时(秒)。 | `90.0` | +| | `stream_idle_timeout` | 流空闲超时 | 流式响应空闲超时(秒),超过将断开。 | `45.0` | +| | `video_idle_timeout` | 视频空闲超时 | 视频生成空闲超时(秒),超过将断开。 | `90.0` | | **token** | `auto_refresh` | 自动刷新 | 是否开启 Token 自动刷新机制。 | `true` | | | `refresh_interval_hours` | 刷新间隔 | Token 刷新的时间间隔(小时)。 | `8` | | | `fail_threshold` | 失败阈值 | 单个 Token 连续失败多少次后被标记为不可用。 | `5` |