|
| 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