Skip to content

Commit aa35833

Browse files
feat(api && monaco): 新增运行拦截and代码编辑器loading (#37)
Co-authored-by: wangbochao.vendor <15830881993@163.com>
1 parent 89d07ae commit aa35833

42 files changed

Lines changed: 1681 additions & 586 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
back/src/parts/data/common_datasets/common_datasets.zip
2+
docker/volumes/

README.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,47 @@ docker compose up -d
9292
# 修改 docker-compose.yml 取消对 cloud-service 服务的注释
9393
```
9494

95+
#### 2.1 使用 HTTPS(可选)
96+
97+
> **安全提示**
98+
> - 仅通过 localhost/127.0.0.1 访问时可以使用 HTTP
99+
> - 通过局域网IP或公网IP访问时必须使用 HTTPS,以保护数据传输安全
100+
101+
如需启用 HTTPS 访问,执行以下步骤:
102+
103+
```bash
104+
# 1. 生成自签SSL证书
105+
cd docker/nginx
106+
chmod +x generate-ssl-cert.sh
107+
./generate-ssl-cert.sh
108+
109+
# 2. 取消注释HTTPS相关配置
110+
# 2.1 编辑 docker/nginx/conf.d/default.conf,取消注释HTTPS server块(从 # server { 到对应的 # })
111+
# 2.2 编辑 docker/docker-compose.yml,取消注释SSL证书目录挂载(找到 # - ./nginx/ssl:/etc/nginx/ssl 这一行)
112+
113+
# 3. 设置环境变量(使用HTTPS端口)
114+
cd ..
115+
export WEB_CONSOLE_ENDPOINT="https://127.0.0.1:30383"
116+
export PORT=30382 # HTTP端口
117+
export HTTPS_PORT=30383 # HTTPS端口
118+
119+
# 4. 启动服务
120+
docker compose up -d
121+
```
122+
123+
**注意**
124+
- 本配置提供的是自签证书的示例配置,浏览器会显示安全警告,点击"继续访问"即可
125+
- 如需使用 Let's Encrypt 或商业 CA 证书,请自行申请证书后,修改 `docker/nginx/conf.d/default.conf` 中的 SSL 证书路径配置
126+
95127
### 3. 访问服务
96128

97129
```bash
130+
# HTTP访问
98131
http://127.0.0.1:30382
132+
133+
# HTTPS访问(如已配置)
134+
https://127.0.0.1:30383
135+
99136
默认账号:admin
100137
默认密码:LazyCraft@2025
101138
```
@@ -140,12 +177,44 @@ docker build --build-arg COMMIT_SHA=$(git rev-parse HEAD) -t lazycraft-front:lat
140177
### 4. 启动服务
141178

142179
```bash
180+
# HTTP 方式(默认)
143181
# 设置环境变量为平台登录地址,例如 http://127.0.0.1:30382
144182
export WEB_CONSOLE_ENDPOINT="http://your-console-url"
145-
146183
export BACK_IMAGE="lazycraft-back:latest"
147184
export FRONT_IMAGE="lazycraft-front:latest"
148185

149186
cd docker
150187
docker compose up -d
151188
```
189+
190+
#### 4.1 使用 HTTPS(可选)
191+
192+
> **安全提示**
193+
> - 仅通过 localhost/127.0.0.1 访问时可以使用 HTTP
194+
> - 通过局域网IP或公网IP访问时必须使用 HTTPS,以保护数据传输安全
195+
196+
```bash
197+
# 1. 生成自签SSL证书
198+
cd docker/nginx
199+
chmod +x generate-ssl-cert.sh
200+
./generate-ssl-cert.sh
201+
202+
# 2. 取消注释HTTPS相关配置
203+
# 2.1 编辑 docker/nginx/conf.d/default.conf,取消注释HTTPS server块(从 # server { 到对应的 # })
204+
# 2.2 编辑 docker/docker-compose.yml,取消注释SSL证书目录挂载(找到 # - ./nginx/ssl:/etc/nginx/ssl 这一行)
205+
206+
# 3. 设置环境变量(使用HTTPS)
207+
cd ..
208+
export WEB_CONSOLE_ENDPOINT="https://127.0.0.1:30383"
209+
export PORT=30382 # HTTP端口
210+
export HTTPS_PORT=30383 # HTTPS端口
211+
export BACK_IMAGE="lazycraft-back:latest"
212+
export FRONT_IMAGE="lazycraft-front:latest"
213+
214+
# 4. 启动服务
215+
docker compose up -d
216+
```
217+
218+
**注意**
219+
- 本配置提供的是自签证书的示例配置,浏览器会显示安全警告,点击"继续访问"即可
220+
- 如需使用 Let's Encrypt 或商业 CA 证书,请自行申请证书后,修改 `docker/nginx/conf.d/default.conf` 中的 SSL 证书路径配置

back/src/appcmd.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -751,19 +751,19 @@ def init_ams_models():
751751
model_icon="/app/upload/online.jpg",
752752
model_type=model_data["model_type"],
753753
model_name=model_data["name"],
754-
model_key=model_data["name"],
754+
model_key=model_data["key"],
755755
description="",
756756
model_path="",
757-
model_from="localModel",
757+
model_from=model_data["model_from"],
758758
model_kind=model_data["model_kind"],
759-
model_brand="",
760-
model_status=3,
759+
model_brand=model_data["model_brand"],
760+
model_status=model_data["model_status"],
761+
prompt_keys="",
761762
is_finetune_model=model_data["is_finetune_model"],
762763
can_finetune_model=model_data["can_finetune_model"],
763764
builtin_flag=True,
764765
user_id=admin_account.id,
765766
tenant_id=admin_account.current_tenant_id,
766-
prompt_keys="",
767767
model_url="",
768768
model_dir="",
769769
deleted_flag=0,

back/src/libs/feature_gate.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# Copyright (c) 2025 SenseTime. All Rights Reserved.
2+
# Author: LazyLLM Team, https://github.com/LazyAGI/LazyLLM
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
"""功能开关模块(Feature Gate)
17+
18+
该模块提供统一的功能开关机制,用于控制互联网部署时的功能可用性。
19+
20+
主要功能:
21+
1. 环境变量控制:通过 INTERNET_FEATURES_ENABLED 环境变量控制功能开关
22+
2. 装饰器拦截:提供装饰器在功能执行前进行权限检查
23+
3. 统一响应:返回标准的错误码和友好的提示信息
24+
25+
使用示例:
26+
@require_internet_feature("应用发布")
27+
def publish_app(self):
28+
# 业务逻辑
29+
pass
30+
31+
环境变量:
32+
INTERNET_FEATURES_ENABLED: "true" 或 "false"(默认),控制功能是否可用
33+
"""
34+
35+
import os
36+
from functools import wraps
37+
38+
39+
class FeatureNotAvailableError(Exception):
40+
"""功能不可用异常
41+
42+
当某个功能被功能开关禁用时抛出此异常。
43+
44+
Attributes:
45+
feature_name (str): 被禁用的功能名称
46+
message (str): 错误消息
47+
"""
48+
49+
def __init__(self, feature_name: str, message: str = None):
50+
"""初始化功能不可用异常
51+
52+
Args:
53+
feature_name (str): 被禁用的功能名称
54+
message (str, optional): 自定义错误消息。默认为None,使用标准消息。
55+
"""
56+
self.feature_name = feature_name
57+
self.message = message or f"{feature_name}功能当前不可用,如需使用请私有化部署"
58+
super().__init__(self.message)
59+
60+
61+
def is_internet_feature_enabled() -> bool:
62+
"""检查互联网功能是否启用
63+
64+
从环境变量 INTERNET_FEATURES_ENABLED 读取配置。
65+
默认为 false (关闭状态),确保部署到互联网时的安全性。
66+
67+
Returns:
68+
bool: True 表示功能启用,False 表示功能禁用
69+
70+
Examples:
71+
>>> os.environ['INTERNET_FEATURES_ENABLED'] = 'true'
72+
>>> is_internet_feature_enabled()
73+
True
74+
>>> os.environ['INTERNET_FEATURES_ENABLED'] = 'false'
75+
>>> is_internet_feature_enabled()
76+
False
77+
"""
78+
enabled = os.getenv("INTERNET_FEATURES_ENABLED", "false").lower()
79+
return enabled in ("true", "1", "yes", "on")
80+
81+
82+
def require_internet_feature(feature_name: str, custom_message: str = None):
83+
"""功能开关装饰器
84+
85+
用于装饰需要进行功能开关控制的API方法。
86+
当功能被禁用时,会拦截请求并返回统一的错误响应。
87+
88+
Args:
89+
feature_name (str): 功能名称,用于错误提示
90+
custom_message (str, optional): 自定义错误消息。默认为None,使用标准消息。
91+
92+
Returns:
93+
function: 装饰器函数
94+
95+
Examples:
96+
```python
97+
class MyApi(Resource):
98+
@login_required
99+
@require_internet_feature("应用发布")
100+
def post(self):
101+
# 只有当 INTERNET_FEATURES_ENABLED=true 时才会执行
102+
return {"result": "success"}
103+
```
104+
105+
Response Format:
106+
当功能被禁用时,返回:
107+
- HTTP Status Code: 423 (Locked)
108+
- Response Body: {
109+
"code": 423,
110+
"message": "XXX功能当前不可用,如需使用请私有化部署"
111+
}
112+
"""
113+
114+
def decorator(func):
115+
@wraps(func)
116+
def wrapper(*args, **kwargs):
117+
if not is_internet_feature_enabled():
118+
error_message = custom_message or f"{feature_name}功能当前不可用,如需使用请私有化部署"
119+
return {
120+
"code": 423,
121+
"message": error_message
122+
}, 423
123+
124+
return func(*args, **kwargs)
125+
126+
return wrapper
127+
return decorator
128+
129+
130+
def check_internet_feature(feature_name: str, custom_message: str = None):
131+
"""检查功能是否可用,不可用则抛出异常
132+
133+
用于在业务逻辑中进行功能检查,而不是作为装饰器使用。
134+
135+
Args:
136+
feature_name (str): 功能名称
137+
custom_message (str, optional): 自定义错误消息
138+
139+
Raises:
140+
FeatureNotAvailableError: 当功能被禁用时抛出
141+
142+
Examples:
143+
```python
144+
def some_business_logic():
145+
check_internet_feature("数据导出")
146+
# 继续执行业务逻辑
147+
export_data()
148+
```
149+
"""
150+
if not is_internet_feature_enabled():
151+
raise FeatureNotAvailableError(feature_name, custom_message)
152+

0 commit comments

Comments
 (0)