Update app.py
Browse files199 unmodified lines20020120220320420537 unmodified lines243244245246247248249250199 unmodified lineskey = available_keys[int(time.time() * 1000) % len(available_keys)]return key# 创建一个后台调度器scheduler = BackgroundScheduler()37 unmodified lines@app.route('/handsome/v1/chat/completions', methods=['POST'])def handsome_chat_completions():"""处理 /handsome/v1/chat/completions 路由的请求。"""data = request.get_json()if not data or 'model' not in data:return jsonify({"error": "Invalid request data"}), 400199 unmodified lines20020120220320420520620720820921021121221321421521621721821922022122222322422537 unmodified lines263264265266267268269270271272273199 unmodified lineskey = available_keys[int(time.time() * 1000) % len(available_keys)]return keydef check_authorization(request):"""检查请求头中的 Authorization 字段是否匹配环境变量 AUTHORIZATION_KEY。"""authorization_key = os.environ.get("AUTHORIZATION_KEY")if not authorization_key:logging.warning("环境变量 AUTHORIZATION_KEY 未设置,请设置后重试。")return Falseauth_header = request.headers.get('Authorization')if not auth_header:logging.warning("请求头中缺少 Authorization 字段。")return Falseif auth_header != f"Bearer {authorization_key}":logging.warning(f"无效的 Authorization 密钥:{auth_header}")return Falsereturn True# 创建一个后台调度器scheduler = BackgroundScheduler()37 unmodified lines@app.route('/handsome/v1/chat/completions', methods=['POST'])def handsome_chat_completions():"""处理 /handsome/v1/chat/completions 路由的请求,添加鉴权。"""if not check_authorization(request):return jsonify({"error": "Unauthorized"}), 401data = request.get_json()if not data or 'model' not in data:return jsonify({"error": "Invalid request data"}), 400
app.py+24-1