yym68686 commited on
Commit
810662a
·
1 Parent(s): a1d3f78

Support downloading configuration files from external URLs.

Browse files
Files changed (4) hide show
  1. README.md +3 -0
  2. docker-compose.yml +2 -0
  3. main.py +16 -1
  4. utils.py +17 -14
README.md CHANGED
@@ -92,6 +92,8 @@ services:
92
  uni-api:
93
  container_name: uni-api
94
  image: yym68686/uni-api:latest
 
 
95
  ports:
96
  - 8001:8000
97
  volumes:
@@ -120,6 +122,7 @@ set -eu
120
  docker pull yym68686/uni-api:latest
121
  docker rm -f uni-api
122
  docker run --user root -p 8001:8000 -dit --name uni-api \
 
123
  -v ./api.yaml:/home/api.yaml \
124
  yym68686/uni-api:latest
125
  docker logs -f uni-api
 
92
  uni-api:
93
  container_name: uni-api
94
  image: yym68686/uni-api:latest
95
+ environment:
96
+ - CONFIG_URL=http://file_url/api.yaml
97
  ports:
98
  - 8001:8000
99
  volumes:
 
122
  docker pull yym68686/uni-api:latest
123
  docker rm -f uni-api
124
  docker run --user root -p 8001:8000 -dit --name uni-api \
125
+ -e CONFIG_URL=http://file_url/api.yaml \
126
  -v ./api.yaml:/home/api.yaml \
127
  yym68686/uni-api:latest
128
  docker logs -f uni-api
docker-compose.yml CHANGED
@@ -2,6 +2,8 @@ services:
2
  uni-api:
3
  container_name: uni-api
4
  image: yym68686/uni-api:latest
 
 
5
  ports:
6
  - 8001:8000
7
  volumes:
 
2
  uni-api:
3
  container_name: uni-api
4
  image: yym68686/uni-api:latest
5
+ environment:
6
+ - CONFIG_URL=http://file_url/api.yaml
7
  ports:
8
  - 8001:8000
9
  volumes:
main.py CHANGED
@@ -7,7 +7,7 @@ from contextlib import asynccontextmanager
7
  from fastapi import FastAPI, HTTPException, Depends
8
 
9
  from models import RequestModel
10
- from utils import config, api_keys_db, api_list, error_handling_wrapper, get_all_models, verify_api_key, post_all_models
11
  from request import get_payload
12
  from response import fetch_response, fetch_response_stream
13
 
@@ -21,6 +21,21 @@ async def lifespan(app: FastAPI):
21
  # 启动时的代码
22
  timeout = httpx.Timeout(connect=15.0, read=10.0, write=30.0, pool=30.0)
23
  app.state.client = httpx.AsyncClient(timeout=timeout)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  yield
25
  # 关闭时的代码
26
  await app.state.client.aclose()
 
7
  from fastapi import FastAPI, HTTPException, Depends
8
 
9
  from models import RequestModel
10
+ from utils import config, api_keys_db, api_list, error_handling_wrapper, get_all_models, verify_api_key, post_all_models, update_config
11
  from request import get_payload
12
  from response import fetch_response, fetch_response_stream
13
 
 
21
  # 启动时的代码
22
  timeout = httpx.Timeout(connect=15.0, read=10.0, write=30.0, pool=30.0)
23
  app.state.client = httpx.AsyncClient(timeout=timeout)
24
+ import os
25
+ import json
26
+ # 新增: 从环境变量获取配置URL并拉取配置
27
+ config_url = os.environ.get('CONFIG_URL')
28
+ if config_url:
29
+ try:
30
+ response = await app.state.client.get(config_url)
31
+ response.raise_for_status()
32
+ config_data = json.loads(response.text)
33
+ # 更新配置
34
+ global config, api_keys_db, api_list
35
+ config, api_keys_db, api_list = update_config(config_data)
36
+ except Exception as e:
37
+ logger.error(f"Error fetching or parsing config from {config_url}: {str(e)}")
38
+
39
  yield
40
  # 关闭时的代码
41
  await app.state.client.aclose()
utils.py CHANGED
@@ -5,25 +5,28 @@ from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
5
 
6
  from log_config import logger
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  # 读取YAML配置文件
9
  def load_config():
10
  try:
11
  with open('./api.yaml', 'r') as f:
12
  conf = yaml.safe_load(f)
13
- for index, provider in enumerate(conf['providers']):
14
- model_dict = {}
15
- for model in provider['model']:
16
- if type(model) == str:
17
- model_dict[model] = model
18
- if type(model) == dict:
19
- model_dict.update({new: old for old, new in model.items()})
20
- model_dict.update({old: old for old, new in model.items()})
21
- provider['model'] = model_dict
22
- conf['providers'][index] = provider
23
- api_keys_db = conf['api_keys']
24
- api_list = [item["api"] for item in api_keys_db]
25
- # logger.info(json.dumps(conf, indent=4, ensure_ascii=False))
26
- return conf, api_keys_db, api_list
27
  except FileNotFoundError:
28
  logger.error("配置文件 'api.yaml' 未找到。请确保文件存在于正确的位置。")
29
  return [], [], []
 
5
 
6
  from log_config import logger
7
 
8
+ def update_config(config_data):
9
+ for index, provider in enumerate(config_data['providers']):
10
+ model_dict = {}
11
+ for model in provider['model']:
12
+ if type(model) == str:
13
+ model_dict[model] = model
14
+ if type(model) == dict:
15
+ model_dict.update({new: old for old, new in model.items()})
16
+ model_dict.update({old: old for old, new in model.items()})
17
+ provider['model'] = model_dict
18
+ config_data['providers'][index] = provider
19
+ api_keys_db = config_data['api_keys']
20
+ api_list = [item["api"] for item in api_keys_db]
21
+ # logger.info(json.dumps(conf, indent=4, ensure_ascii=False))
22
+ return config_data, api_keys_db, api_list
23
+
24
  # 读取YAML配置文件
25
  def load_config():
26
  try:
27
  with open('./api.yaml', 'r') as f:
28
  conf = yaml.safe_load(f)
29
+ return update_config(conf)
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  except FileNotFoundError:
31
  logger.error("配置文件 'api.yaml' 未找到。请确保文件存在于正确的位置。")
32
  return [], [], []