yym68686 commited on
Commit
79bd233
·
1 Parent(s): a37c1d1

🐛 Bug: Fix the bug that prevents the host machine from synchronizing database files.

Browse files
Files changed (4) hide show
  1. README.md +3 -3
  2. README_CN.md +3 -3
  3. docker-compose.yml +1 -1
  4. main.py +8 -1
README.md CHANGED
@@ -135,7 +135,7 @@ Start the container
135
  docker run --user root -p 8001:8000 --name uni-api -dit \
136
  -e CONFIG_URL=http://file_url/api.yaml \ # If the local configuration file is already mounted, you do not need to set CONFIG_URL
137
  -v ./api.yaml:/home/api.yaml \ # If CONFIG_URL is already set, you do not need to mount the configuration file
138
- -v ./stats.db:/stats.db \ # If you do not want to save statistical data, you do not need to mount the stats.db file
139
  yym68686/uni-api:latest
140
  ```
141
 
@@ -152,7 +152,7 @@ services:
152
  - 8001:8000
153
  volumes:
154
  - ./api.yaml:/home/api.yaml # If CONFIG_URL is already set, there is no need to mount the configuration file
155
- - ./stats.db:/stats.db # If you do not want to save statistical data, there is no need to mount the stats.db file
156
  ```
157
 
158
  CONFIG_URL is used to automatically download remote configuration files. For example, if it is inconvenient to modify the configuration file on a certain platform, you can upload the configuration file to a hosting service and provide a direct link for uni-api to download. CONFIG_URL is this direct link. If you are using a locally mounted configuration file, you do not need to set CONFIG_URL. CONFIG_URL is used in situations where it is inconvenient to mount the configuration file.
@@ -181,7 +181,7 @@ docker rm -f uni-api
181
  docker run --user root -p 8001:8000 -dit --name uni-api \
182
  -e CONFIG_URL=http://file_url/api.yaml \
183
  -v ./api.yaml:/home/api.yaml \
184
- -v ./stats.db:/stats.db \
185
  yym68686/uni-api:latest
186
  docker logs -f uni-api
187
  ```
 
135
  docker run --user root -p 8001:8000 --name uni-api -dit \
136
  -e CONFIG_URL=http://file_url/api.yaml \ # If the local configuration file is already mounted, you do not need to set CONFIG_URL
137
  -v ./api.yaml:/home/api.yaml \ # If CONFIG_URL is already set, you do not need to mount the configuration file
138
+ -v ./uniapi_db:/home/data \ # If you do not want to save statistical data, you do not need to mount the stats.db file
139
  yym68686/uni-api:latest
140
  ```
141
 
 
152
  - 8001:8000
153
  volumes:
154
  - ./api.yaml:/home/api.yaml # If CONFIG_URL is already set, there is no need to mount the configuration file
155
+ - ./uniapi_db:/home/data # If you do not want to save statistical data, there is no need to mount the stats.db file
156
  ```
157
 
158
  CONFIG_URL is used to automatically download remote configuration files. For example, if it is inconvenient to modify the configuration file on a certain platform, you can upload the configuration file to a hosting service and provide a direct link for uni-api to download. CONFIG_URL is this direct link. If you are using a locally mounted configuration file, you do not need to set CONFIG_URL. CONFIG_URL is used in situations where it is inconvenient to mount the configuration file.
 
181
  docker run --user root -p 8001:8000 -dit --name uni-api \
182
  -e CONFIG_URL=http://file_url/api.yaml \
183
  -v ./api.yaml:/home/api.yaml \
184
+ -v ./uniapi_db:/home/data \
185
  yym68686/uni-api:latest
186
  docker logs -f uni-api
187
  ```
README_CN.md CHANGED
@@ -135,7 +135,7 @@ Start the container
135
  docker run --user root -p 8001:8000 --name uni-api -dit \
136
  -e CONFIG_URL=http://file_url/api.yaml \ # 如果已经挂载了本地配置文件,不需要设置 CONFIG_URL
137
  -v ./api.yaml:/home/api.yaml \ # 如果已经设置 CONFIG_URL,不需要挂载配置文件
138
- -v ./stats.db:/stats.db \ # 如果不想保存统计数据,不需要挂载 stats.db 文件
139
  yym68686/uni-api:latest
140
  ```
141
 
@@ -152,7 +152,7 @@ services:
152
  - 8001:8000
153
  volumes:
154
  - ./api.yaml:/home/api.yaml # 如果已经设置 CONFIG_URL,不需要挂载配置文件
155
- - ./stats.db:/stats.db # 如果不想保存统计数据,不需要挂载 stats.db 文件
156
  ```
157
 
158
  CONFIG_URL 就是可以自动下载远程的配置文件。比如你在某个平台不方便修改配置文件,可以把配置文件传到某个托管服务,可以提供直链给 uni-api 下载,CONFIG_URL 就是这个直链。如果使用本地挂载的配置文件,不需要设置 CONFIG_URL。CONFIG_URL 是在不方便挂载配置文件的情况下使用。
@@ -181,7 +181,7 @@ docker rm -f uni-api
181
  docker run --user root -p 8001:8000 -dit --name uni-api \
182
  -e CONFIG_URL=http://file_url/api.yaml \
183
  -v ./api.yaml:/home/api.yaml \
184
- -v ./stats.db:/stats.db \
185
  yym68686/uni-api:latest
186
  docker logs -f uni-api
187
  ```
 
135
  docker run --user root -p 8001:8000 --name uni-api -dit \
136
  -e CONFIG_URL=http://file_url/api.yaml \ # 如果已经挂载了本地配置文件,不需要设置 CONFIG_URL
137
  -v ./api.yaml:/home/api.yaml \ # 如果已经设置 CONFIG_URL,不需要挂载配置文件
138
+ -v ./uniapi_db:/home/data \ # 如果不想保存统计数据,不需要挂载该文件夹
139
  yym68686/uni-api:latest
140
  ```
141
 
 
152
  - 8001:8000
153
  volumes:
154
  - ./api.yaml:/home/api.yaml # 如果已经设置 CONFIG_URL,不需要挂载配置文件
155
+ - ./uniapi_db:/home/data # 如果不想保存统计数据,不需要挂载该文件夹
156
  ```
157
 
158
  CONFIG_URL 就是可以自动下载远程的配置文件。比如你在某个平台不方便修改配置文件,可以把配置文件传到某个托管服务,可以提供直链给 uni-api 下载,CONFIG_URL 就是这个直链。如果使用本地挂载的配置文件,不需要设置 CONFIG_URL。CONFIG_URL 是在不方便挂载配置文件的情况下使用。
 
181
  docker run --user root -p 8001:8000 -dit --name uni-api \
182
  -e CONFIG_URL=http://file_url/api.yaml \
183
  -v ./api.yaml:/home/api.yaml \
184
+ -v ./uniapi_db:/home/data \
185
  yym68686/uni-api:latest
186
  docker logs -f uni-api
187
  ```
docker-compose.yml CHANGED
@@ -8,4 +8,4 @@ services:
8
  - 8001:8000
9
  volumes:
10
  - ./api.yaml:/home/api.yaml
11
- - ./stats.db:/stats.db
 
8
  - 8001:8000
9
  volumes:
10
  - ./api.yaml:/home/api.yaml
11
+ - ./uniapi_db:/home/data
main.py CHANGED
@@ -105,8 +105,15 @@ class ChannelStat(Base):
105
  first_response_time = Column(Float) # 新增: 记录首次响应时间
106
  timestamp = Column(DateTime(timezone=True), server_default=func.now())
107
 
 
 
 
 
 
 
 
108
  # 创建异步引擎和会话
109
- engine = create_async_engine('sqlite+aiosqlite:///stats.db', echo=is_debug)
110
  async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
111
 
112
  class StatsMiddleware(BaseHTTPMiddleware):
 
105
  first_response_time = Column(Float) # 新增: 记录首次响应时间
106
  timestamp = Column(DateTime(timezone=True), server_default=func.now())
107
 
108
+ # 获取数据库路径
109
+ db_path = os.getenv('DB_PATH', './data/stats.db')
110
+
111
+ # 确保 data 目录存在
112
+ data_dir = os.path.dirname(db_path)
113
+ os.makedirs(data_dir, exist_ok=True)
114
+
115
  # 创建异步引擎和会话
116
+ engine = create_async_engine('sqlite+aiosqlite:///' + db_path, echo=is_debug)
117
  async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
118
 
119
  class StatsMiddleware(BaseHTTPMiddleware):