Spaces:
Sleeping
Sleeping
yu commited on
Commit ·
320e660
1
Parent(s): 595e7f0
refactor: 应用装饰器消除重复验证代码
Browse files- 应用 require_path_prefix 和 require_path_and_admin 装饰器到 10 个路由
- 删除 67 行重复的路径前缀和管理员密钥验证代码
- 代码量减少 57 行,可维护性提升
main.py
CHANGED
|
@@ -1813,11 +1813,8 @@ async def admin_home(path_prefix: str, request: Request, key: str = None, author
|
|
| 1813 |
return HTMLResponse(content=html_content)
|
| 1814 |
|
| 1815 |
@app.get("/{path_prefix}/v1/models")
|
|
|
|
| 1816 |
async def list_models(path_prefix: str, authorization: str = Header(None)):
|
| 1817 |
-
# 验证路径前缀
|
| 1818 |
-
if path_prefix != PATH_PREFIX:
|
| 1819 |
-
raise HTTPException(404, "Not Found")
|
| 1820 |
-
|
| 1821 |
# 验证 API Key
|
| 1822 |
verify_api_key(authorization)
|
| 1823 |
|
|
@@ -1834,41 +1831,22 @@ async def list_models(path_prefix: str, authorization: str = Header(None)):
|
|
| 1834 |
return {"object": "list", "data": data}
|
| 1835 |
|
| 1836 |
@app.get("/{path_prefix}/v1/models/{model_id}")
|
|
|
|
| 1837 |
async def get_model(path_prefix: str, model_id: str, authorization: str = Header(None)):
|
| 1838 |
-
# 验证路径前缀
|
| 1839 |
-
if path_prefix != PATH_PREFIX:
|
| 1840 |
-
raise HTTPException(404, "Not Found")
|
| 1841 |
-
|
| 1842 |
# 验证 API Key
|
| 1843 |
verify_api_key(authorization)
|
| 1844 |
|
| 1845 |
return {"id": model_id, "object": "model"}
|
| 1846 |
|
| 1847 |
@app.get("/{path_prefix}/admin/health")
|
|
|
|
| 1848 |
async def admin_health(path_prefix: str, key: str = None, authorization: str = Header(None)):
|
| 1849 |
-
# 验证路径前缀
|
| 1850 |
-
if path_prefix != PATH_PREFIX:
|
| 1851 |
-
raise HTTPException(404, "Not Found")
|
| 1852 |
-
|
| 1853 |
-
# 验证管理员密钥
|
| 1854 |
-
admin_key = key or (authorization.replace("Bearer ", "") if authorization and authorization.startswith("Bearer ") else authorization)
|
| 1855 |
-
if admin_key != ADMIN_KEY:
|
| 1856 |
-
raise HTTPException(404, "Not Found")
|
| 1857 |
-
|
| 1858 |
return {"status": "ok", "time": datetime.utcnow().isoformat()}
|
| 1859 |
|
| 1860 |
@app.get("/{path_prefix}/admin/accounts")
|
|
|
|
| 1861 |
async def admin_get_accounts(path_prefix: str, key: str = None, authorization: str = Header(None)):
|
| 1862 |
"""获取所有账户的状态信息"""
|
| 1863 |
-
# 验证路径前缀
|
| 1864 |
-
if path_prefix != PATH_PREFIX:
|
| 1865 |
-
raise HTTPException(404, "Not Found")
|
| 1866 |
-
|
| 1867 |
-
# 验证管理员密钥
|
| 1868 |
-
admin_key = key or (authorization.replace("Bearer ", "") if authorization and authorization.startswith("Bearer ") else authorization)
|
| 1869 |
-
if admin_key != ADMIN_KEY:
|
| 1870 |
-
raise HTTPException(404, "Not Found")
|
| 1871 |
-
|
| 1872 |
accounts_info = []
|
| 1873 |
for account_id, account_manager in multi_account_mgr.accounts.items():
|
| 1874 |
config = account_manager.config
|
|
@@ -1893,13 +1871,9 @@ async def admin_get_accounts(path_prefix: str, key: str = None, authorization: s
|
|
| 1893 |
}
|
| 1894 |
|
| 1895 |
@app.put("/{path_prefix}/admin/accounts-config")
|
|
|
|
| 1896 |
async def admin_update_config(path_prefix: str, accounts_data: list = Body(...), key: str = None, authorization: str = Header(None)):
|
| 1897 |
"""更新整个账户配置"""
|
| 1898 |
-
if path_prefix != PATH_PREFIX:
|
| 1899 |
-
raise HTTPException(404, "Not Found")
|
| 1900 |
-
admin_key = key or (authorization.replace("Bearer ", "") if authorization and authorization.startswith("Bearer ") else authorization)
|
| 1901 |
-
if admin_key != ADMIN_KEY:
|
| 1902 |
-
raise HTTPException(404, "Not Found")
|
| 1903 |
try:
|
| 1904 |
update_accounts_config(accounts_data)
|
| 1905 |
return {"status": "success", "message": "配置已更新", "account_count": len(multi_account_mgr.accounts)}
|
|
@@ -1908,13 +1882,9 @@ async def admin_update_config(path_prefix: str, accounts_data: list = Body(...),
|
|
| 1908 |
raise HTTPException(500, f"更新失败: {str(e)}")
|
| 1909 |
|
| 1910 |
@app.delete("/{path_prefix}/admin/accounts/{account_id}")
|
|
|
|
| 1911 |
async def admin_delete_account(path_prefix: str, account_id: str, key: str = None, authorization: str = Header(None)):
|
| 1912 |
"""删除单个账户"""
|
| 1913 |
-
if path_prefix != PATH_PREFIX:
|
| 1914 |
-
raise HTTPException(404, "Not Found")
|
| 1915 |
-
admin_key = key or (authorization.replace("Bearer ", "") if authorization and authorization.startswith("Bearer ") else authorization)
|
| 1916 |
-
if admin_key != ADMIN_KEY:
|
| 1917 |
-
raise HTTPException(404, "Not Found")
|
| 1918 |
try:
|
| 1919 |
delete_account(account_id)
|
| 1920 |
return {"status": "success", "message": f"账户 {account_id} 已删除", "account_count": len(multi_account_mgr.accounts)}
|
|
@@ -1923,6 +1893,7 @@ async def admin_delete_account(path_prefix: str, account_id: str, key: str = Non
|
|
| 1923 |
raise HTTPException(500, f"删除失败: {str(e)}")
|
| 1924 |
|
| 1925 |
@app.get("/{path_prefix}/admin/log")
|
|
|
|
| 1926 |
async def admin_get_logs(
|
| 1927 |
path_prefix: str,
|
| 1928 |
limit: int = 1500,
|
|
@@ -1943,15 +1914,6 @@ async def admin_get_logs(
|
|
| 1943 |
- start_time: 开始时间 (格式: 2025-12-17 10:00:00)
|
| 1944 |
- end_time: 结束时间 (格式: 2025-12-17 11:00:00)
|
| 1945 |
"""
|
| 1946 |
-
# 验证路径前缀
|
| 1947 |
-
if path_prefix != PATH_PREFIX:
|
| 1948 |
-
raise HTTPException(404, "Not Found")
|
| 1949 |
-
|
| 1950 |
-
# 验证管理员密钥
|
| 1951 |
-
admin_key = key or (authorization.replace("Bearer ", "") if authorization and authorization.startswith("Bearer ") else authorization)
|
| 1952 |
-
if admin_key != ADMIN_KEY:
|
| 1953 |
-
raise HTTPException(404, "Not Found")
|
| 1954 |
-
|
| 1955 |
with log_lock:
|
| 1956 |
logs = list(log_buffer)
|
| 1957 |
|
|
@@ -2015,6 +1977,7 @@ async def admin_get_logs(
|
|
| 2015 |
}
|
| 2016 |
|
| 2017 |
@app.delete("/{path_prefix}/admin/log")
|
|
|
|
| 2018 |
async def admin_clear_logs(path_prefix: str, confirm: str = None, key: str = None, authorization: str = Header(None)):
|
| 2019 |
"""
|
| 2020 |
清空所有日志(内存缓冲 + 文件)
|
|
@@ -2022,15 +1985,6 @@ async def admin_clear_logs(path_prefix: str, confirm: str = None, key: str = Non
|
|
| 2022 |
参数:
|
| 2023 |
- confirm: 必须传入 "yes" 才能清空
|
| 2024 |
"""
|
| 2025 |
-
# 验证路径前缀
|
| 2026 |
-
if path_prefix != PATH_PREFIX:
|
| 2027 |
-
raise HTTPException(404, "Not Found")
|
| 2028 |
-
|
| 2029 |
-
# 验证管理员密钥
|
| 2030 |
-
admin_key = key or (authorization.replace("Bearer ", "") if authorization and authorization.startswith("Bearer ") else authorization)
|
| 2031 |
-
if admin_key != ADMIN_KEY:
|
| 2032 |
-
raise HTTPException(404, "Not Found")
|
| 2033 |
-
|
| 2034 |
if confirm != "yes":
|
| 2035 |
raise HTTPException(
|
| 2036 |
status_code=400,
|
|
@@ -2051,17 +2005,9 @@ async def admin_clear_logs(path_prefix: str, confirm: str = None, key: str = Non
|
|
| 2051 |
}
|
| 2052 |
|
| 2053 |
@app.get("/{path_prefix}/admin/log/html")
|
|
|
|
| 2054 |
async def admin_logs_html(path_prefix: str, key: str = None, authorization: str = Header(None)):
|
| 2055 |
"""返回美化的 HTML 日志查看界面"""
|
| 2056 |
-
# 验证路径前缀
|
| 2057 |
-
if path_prefix != PATH_PREFIX:
|
| 2058 |
-
raise HTTPException(404, "Not Found")
|
| 2059 |
-
|
| 2060 |
-
# 验证管理员密钥
|
| 2061 |
-
admin_key = key or (authorization.replace("Bearer ", "") if authorization and authorization.startswith("Bearer ") else authorization)
|
| 2062 |
-
if admin_key != ADMIN_KEY:
|
| 2063 |
-
raise HTTPException(404, "Not Found")
|
| 2064 |
-
|
| 2065 |
html_content = r"""
|
| 2066 |
<!DOCTYPE html>
|
| 2067 |
<html>
|
|
@@ -2683,16 +2629,13 @@ async def admin_logs_html(path_prefix: str, key: str = None, authorization: str
|
|
| 2683 |
return HTMLResponse(content=html_content)
|
| 2684 |
|
| 2685 |
@app.post("/{path_prefix}/v1/chat/completions")
|
|
|
|
| 2686 |
async def chat(
|
| 2687 |
path_prefix: str,
|
| 2688 |
req: ChatRequest,
|
| 2689 |
request: Request,
|
| 2690 |
authorization: Optional[str] = Header(None)
|
| 2691 |
):
|
| 2692 |
-
# 0. 验证路径前缀
|
| 2693 |
-
if path_prefix != PATH_PREFIX:
|
| 2694 |
-
raise HTTPException(404, "Not Found")
|
| 2695 |
-
|
| 2696 |
# 1. API Key 验证
|
| 2697 |
verify_api_key(authorization)
|
| 2698 |
|
|
|
|
| 1813 |
return HTMLResponse(content=html_content)
|
| 1814 |
|
| 1815 |
@app.get("/{path_prefix}/v1/models")
|
| 1816 |
+
@require_path_prefix(PATH_PREFIX)
|
| 1817 |
async def list_models(path_prefix: str, authorization: str = Header(None)):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1818 |
# 验证 API Key
|
| 1819 |
verify_api_key(authorization)
|
| 1820 |
|
|
|
|
| 1831 |
return {"object": "list", "data": data}
|
| 1832 |
|
| 1833 |
@app.get("/{path_prefix}/v1/models/{model_id}")
|
| 1834 |
+
@require_path_prefix(PATH_PREFIX)
|
| 1835 |
async def get_model(path_prefix: str, model_id: str, authorization: str = Header(None)):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1836 |
# 验证 API Key
|
| 1837 |
verify_api_key(authorization)
|
| 1838 |
|
| 1839 |
return {"id": model_id, "object": "model"}
|
| 1840 |
|
| 1841 |
@app.get("/{path_prefix}/admin/health")
|
| 1842 |
+
@require_path_and_admin(PATH_PREFIX, ADMIN_KEY)
|
| 1843 |
async def admin_health(path_prefix: str, key: str = None, authorization: str = Header(None)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1844 |
return {"status": "ok", "time": datetime.utcnow().isoformat()}
|
| 1845 |
|
| 1846 |
@app.get("/{path_prefix}/admin/accounts")
|
| 1847 |
+
@require_path_and_admin(PATH_PREFIX, ADMIN_KEY)
|
| 1848 |
async def admin_get_accounts(path_prefix: str, key: str = None, authorization: str = Header(None)):
|
| 1849 |
"""获取所有账户的状态信息"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1850 |
accounts_info = []
|
| 1851 |
for account_id, account_manager in multi_account_mgr.accounts.items():
|
| 1852 |
config = account_manager.config
|
|
|
|
| 1871 |
}
|
| 1872 |
|
| 1873 |
@app.put("/{path_prefix}/admin/accounts-config")
|
| 1874 |
+
@require_path_and_admin(PATH_PREFIX, ADMIN_KEY)
|
| 1875 |
async def admin_update_config(path_prefix: str, accounts_data: list = Body(...), key: str = None, authorization: str = Header(None)):
|
| 1876 |
"""更新整个账户配置"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1877 |
try:
|
| 1878 |
update_accounts_config(accounts_data)
|
| 1879 |
return {"status": "success", "message": "配置已更新", "account_count": len(multi_account_mgr.accounts)}
|
|
|
|
| 1882 |
raise HTTPException(500, f"更新失败: {str(e)}")
|
| 1883 |
|
| 1884 |
@app.delete("/{path_prefix}/admin/accounts/{account_id}")
|
| 1885 |
+
@require_path_and_admin(PATH_PREFIX, ADMIN_KEY)
|
| 1886 |
async def admin_delete_account(path_prefix: str, account_id: str, key: str = None, authorization: str = Header(None)):
|
| 1887 |
"""删除单个账户"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1888 |
try:
|
| 1889 |
delete_account(account_id)
|
| 1890 |
return {"status": "success", "message": f"账户 {account_id} 已删除", "account_count": len(multi_account_mgr.accounts)}
|
|
|
|
| 1893 |
raise HTTPException(500, f"删除失败: {str(e)}")
|
| 1894 |
|
| 1895 |
@app.get("/{path_prefix}/admin/log")
|
| 1896 |
+
@require_path_and_admin(PATH_PREFIX, ADMIN_KEY)
|
| 1897 |
async def admin_get_logs(
|
| 1898 |
path_prefix: str,
|
| 1899 |
limit: int = 1500,
|
|
|
|
| 1914 |
- start_time: 开始时间 (格式: 2025-12-17 10:00:00)
|
| 1915 |
- end_time: 结束时间 (格式: 2025-12-17 11:00:00)
|
| 1916 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1917 |
with log_lock:
|
| 1918 |
logs = list(log_buffer)
|
| 1919 |
|
|
|
|
| 1977 |
}
|
| 1978 |
|
| 1979 |
@app.delete("/{path_prefix}/admin/log")
|
| 1980 |
+
@require_path_and_admin(PATH_PREFIX, ADMIN_KEY)
|
| 1981 |
async def admin_clear_logs(path_prefix: str, confirm: str = None, key: str = None, authorization: str = Header(None)):
|
| 1982 |
"""
|
| 1983 |
清空所有日志(内存缓冲 + 文件)
|
|
|
|
| 1985 |
参数:
|
| 1986 |
- confirm: 必须传入 "yes" 才能清空
|
| 1987 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1988 |
if confirm != "yes":
|
| 1989 |
raise HTTPException(
|
| 1990 |
status_code=400,
|
|
|
|
| 2005 |
}
|
| 2006 |
|
| 2007 |
@app.get("/{path_prefix}/admin/log/html")
|
| 2008 |
+
@require_path_and_admin(PATH_PREFIX, ADMIN_KEY)
|
| 2009 |
async def admin_logs_html(path_prefix: str, key: str = None, authorization: str = Header(None)):
|
| 2010 |
"""返回美化的 HTML 日志查看界面"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2011 |
html_content = r"""
|
| 2012 |
<!DOCTYPE html>
|
| 2013 |
<html>
|
|
|
|
| 2629 |
return HTMLResponse(content=html_content)
|
| 2630 |
|
| 2631 |
@app.post("/{path_prefix}/v1/chat/completions")
|
| 2632 |
+
@require_path_prefix(PATH_PREFIX)
|
| 2633 |
async def chat(
|
| 2634 |
path_prefix: str,
|
| 2635 |
req: ChatRequest,
|
| 2636 |
request: Request,
|
| 2637 |
authorization: Optional[str] = Header(None)
|
| 2638 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2639 |
# 1. API Key 验证
|
| 2640 |
verify_api_key(authorization)
|
| 2641 |
|