✨ Feature: Add support for completely disabling the database.
Browse files- README.md +2 -1
- README_CN.md +4 -1
- main.py +27 -11
README.md
CHANGED
@@ -170,6 +170,7 @@ yym68686/uni-api:latest
|
|
170 |
|
171 |
- CONFIG_URL: The download address of the configuration file, which can be a local file or a remote file, optional
|
172 |
- TIMEOUT: Request timeout, default is 100 seconds. The timeout can control the time needed to switch to the next channel when one channel does not respond. Optional
|
|
|
173 |
|
174 |
## Get statistical data
|
175 |
|
@@ -189,7 +190,7 @@ There are other statistical data that you can query yourself by writing SQL in t
|
|
189 |
|
190 |
## Vercel Deployment
|
191 |
|
192 |
-
[](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fyym68686%2Funi-api%2Ftree%2Fmain&project-name=uni-api-vercel&repository-name=uni-api-vercel)
|
193 |
|
194 |
## Docker local deployment
|
195 |
|
|
|
170 |
|
171 |
- CONFIG_URL: The download address of the configuration file, which can be a local file or a remote file, optional
|
172 |
- TIMEOUT: Request timeout, default is 100 seconds. The timeout can control the time needed to switch to the next channel when one channel does not respond. Optional
|
173 |
+
- DISABLE_DATABASE: Whether to disable the database, default is false, optional
|
174 |
|
175 |
## Get statistical data
|
176 |
|
|
|
190 |
|
191 |
## Vercel Deployment
|
192 |
|
193 |
+
[](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fyym68686%2Funi-api%2Ftree%2Fmain&env=CONFIG_URL,DISABLE_DATABASE&project-name=uni-api-vercel&repository-name=uni-api-vercel)
|
194 |
|
195 |
## Docker local deployment
|
196 |
|
README_CN.md
CHANGED
@@ -170,6 +170,7 @@ yym68686/uni-api:latest
|
|
170 |
|
171 |
- CONFIG_URL: 配置文件的下载地址,可以是本地文件,也可以是远程文件,选填
|
172 |
- TIMEOUT: 请求超时时间,默认为 100 秒,超时时间可以控制当一个渠道没有响应时,切换下一个渠道需要的时间。选填
|
|
|
173 |
|
174 |
## 获取统计数据
|
175 |
|
@@ -189,7 +190,9 @@ yym68686/uni-api:latest
|
|
189 |
|
190 |
## Vercel 部署
|
191 |
|
192 |
-
[](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fyym68686%2Funi-api%2Ftree%2Fmain&project-name=uni-api-vercel&repository-name=uni-api-vercel)
|
|
|
|
|
193 |
|
194 |
## Docker 本地部署
|
195 |
|
|
|
170 |
|
171 |
- CONFIG_URL: 配置文件的下载地址,可以是本地文件,也可以是远程文件,选填
|
172 |
- TIMEOUT: 请求超时时间,默认为 100 秒,超时时间可以控制当一个渠道没有响应时,切换下一个渠道需要的时间。选填
|
173 |
+
- DISABLE_DATABASE: 是否禁用数据库,默认为 false,选填
|
174 |
|
175 |
## 获取统计数据
|
176 |
|
|
|
190 |
|
191 |
## Vercel 部署
|
192 |
|
193 |
+
[](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fyym68686%2Funi-api%2Ftree%2Fmain&env=CONFIG_URL,DISABLE_DATABASE&project-name=uni-api-vercel&repository-name=uni-api-vercel)
|
194 |
+
|
195 |
+
点击上面的一键部署按钮后,设置环境变量 `CONFIG_URL` 为配置文件的直链,然后点击 Create 创建项目。
|
196 |
|
197 |
## Docker 本地部署
|
198 |
|
main.py
CHANGED
@@ -34,8 +34,13 @@ is_debug = bool(os.getenv("DEBUG", False))
|
|
34 |
from sqlalchemy import inspect, text
|
35 |
from sqlalchemy.sql import sqltypes
|
36 |
|
|
|
|
|
|
|
37 |
async def create_tables():
|
38 |
-
|
|
|
|
|
39 |
await conn.run_sync(Base.metadata.create_all)
|
40 |
|
41 |
# 检查并添加缺失的列
|
@@ -85,7 +90,8 @@ async def lifespan(app: FastAPI):
|
|
85 |
# for route in frontend_router.routes:
|
86 |
# print(f"Route: {route.path}, methods: {route.methods}")
|
87 |
# 启动时的代码
|
88 |
-
|
|
|
89 |
|
90 |
TIMEOUT = float(os.getenv("TIMEOUT", 100))
|
91 |
timeout = httpx.Timeout(connect=15.0, read=TIMEOUT, write=30.0, pool=30.0)
|
@@ -178,17 +184,19 @@ class ChannelStat(Base):
|
|
178 |
success = Column(Boolean, default=False)
|
179 |
timestamp = Column(DateTime(timezone=True), server_default=func.now())
|
180 |
|
181 |
-
# 获取数据库路径
|
182 |
-
db_path = os.getenv('DB_PATH', './data/stats.db')
|
183 |
|
184 |
-
|
185 |
-
|
186 |
-
os.
|
|
|
|
|
|
|
|
|
187 |
|
188 |
-
# 创建异步引擎和会话
|
189 |
-
#
|
190 |
-
|
191 |
-
async_session = sessionmaker(
|
192 |
|
193 |
from starlette.types import Scope, Receive, Send
|
194 |
from starlette.responses import Response
|
@@ -260,6 +268,8 @@ class LoggingStreamingResponse(Response):
|
|
260 |
async def update_stats(self):
|
261 |
# 这里添加更新数据库的逻辑
|
262 |
# print("current_info2")
|
|
|
|
|
263 |
async with async_session() as session:
|
264 |
async with session.begin():
|
265 |
try:
|
@@ -426,6 +436,8 @@ class StatsMiddleware(BaseHTTPMiddleware):
|
|
426 |
request_info.reset(current_request_info)
|
427 |
|
428 |
async def update_stats(self, current_info):
|
|
|
|
|
429 |
# 这里添加更新数据库的逻辑
|
430 |
async with async_session() as session:
|
431 |
async with session.begin():
|
@@ -440,6 +452,8 @@ class StatsMiddleware(BaseHTTPMiddleware):
|
|
440 |
logger.error(f"Error updating stats: {str(e)}")
|
441 |
|
442 |
async def update_channel_stats(self, request_id, provider, model, api_key, success):
|
|
|
|
|
443 |
async with async_session() as session:
|
444 |
async with session.begin():
|
445 |
try:
|
@@ -958,6 +972,8 @@ async def get_stats(
|
|
958 |
token: str = Depends(verify_admin_api_key),
|
959 |
hours: int = Query(default=24, ge=1, le=720, description="Number of hours to look back for stats (1-720)")
|
960 |
):
|
|
|
|
|
961 |
async with async_session() as session:
|
962 |
# 计算指定时间范围的开始时间
|
963 |
start_time = datetime.now(timezone.utc) - timedelta(hours=hours)
|
|
|
34 |
from sqlalchemy import inspect, text
|
35 |
from sqlalchemy.sql import sqltypes
|
36 |
|
37 |
+
# 添加新的环境变量检查
|
38 |
+
DISABLE_DATABASE = os.getenv("DISABLE_DATABASE", "false").lower() == "true"
|
39 |
+
|
40 |
async def create_tables():
|
41 |
+
if DISABLE_DATABASE:
|
42 |
+
return
|
43 |
+
async with db_engine.begin() as conn:
|
44 |
await conn.run_sync(Base.metadata.create_all)
|
45 |
|
46 |
# 检查并添加缺失的列
|
|
|
90 |
# for route in frontend_router.routes:
|
91 |
# print(f"Route: {route.path}, methods: {route.methods}")
|
92 |
# 启动时的代码
|
93 |
+
if not DISABLE_DATABASE:
|
94 |
+
await create_tables()
|
95 |
|
96 |
TIMEOUT = float(os.getenv("TIMEOUT", 100))
|
97 |
timeout = httpx.Timeout(connect=15.0, read=TIMEOUT, write=30.0, pool=30.0)
|
|
|
184 |
success = Column(Boolean, default=False)
|
185 |
timestamp = Column(DateTime(timezone=True), server_default=func.now())
|
186 |
|
|
|
|
|
187 |
|
188 |
+
if not DISABLE_DATABASE:
|
189 |
+
# 获取数据库路径
|
190 |
+
db_path = os.getenv('DB_PATH', './data/stats.db')
|
191 |
+
|
192 |
+
# 确保 data 目录存在
|
193 |
+
data_dir = os.path.dirname(db_path)
|
194 |
+
os.makedirs(data_dir, exist_ok=True)
|
195 |
|
196 |
+
# 创建异步引擎和会话
|
197 |
+
# db_engine = create_async_engine('sqlite+aiosqlite:///' + db_path, echo=False)
|
198 |
+
db_engine = create_async_engine('sqlite+aiosqlite:///' + db_path, echo=is_debug)
|
199 |
+
async_session = sessionmaker(db_engine, class_=AsyncSession, expire_on_commit=False)
|
200 |
|
201 |
from starlette.types import Scope, Receive, Send
|
202 |
from starlette.responses import Response
|
|
|
268 |
async def update_stats(self):
|
269 |
# 这里添加更新数据库的逻辑
|
270 |
# print("current_info2")
|
271 |
+
if DISABLE_DATABASE:
|
272 |
+
return
|
273 |
async with async_session() as session:
|
274 |
async with session.begin():
|
275 |
try:
|
|
|
436 |
request_info.reset(current_request_info)
|
437 |
|
438 |
async def update_stats(self, current_info):
|
439 |
+
if DISABLE_DATABASE:
|
440 |
+
return
|
441 |
# 这里添加更新数据库的逻辑
|
442 |
async with async_session() as session:
|
443 |
async with session.begin():
|
|
|
452 |
logger.error(f"Error updating stats: {str(e)}")
|
453 |
|
454 |
async def update_channel_stats(self, request_id, provider, model, api_key, success):
|
455 |
+
if DISABLE_DATABASE:
|
456 |
+
return
|
457 |
async with async_session() as session:
|
458 |
async with session.begin():
|
459 |
try:
|
|
|
972 |
token: str = Depends(verify_admin_api_key),
|
973 |
hours: int = Query(default=24, ge=1, le=720, description="Number of hours to look back for stats (1-720)")
|
974 |
):
|
975 |
+
if DISABLE_DATABASE:
|
976 |
+
return JSONResponse(content={"stats": {}})
|
977 |
async with async_session() as session:
|
978 |
# 计算指定时间范围的开始时间
|
979 |
start_time = datetime.now(timezone.utc) - timedelta(hours=hours)
|