web api添加拦截功能

This commit is contained in:
zhiyong 2025-05-15 21:12:29 +08:00
parent b1a291b632
commit 8684d4f52d

View File

@ -250,6 +250,13 @@ except Exception as e:
app = Flask(__name__) app = Flask(__name__)
# 添加中间件,拦截所有不以"/yu"开头的请求并返回404
@app.before_request
def check_path_prefix():
if not request.path.startswith('/yu'):
logger.warning(f"拦截非法请求: {request.path}")
abort(404)
@app.route("/yu/healthcheck", methods=["GET"]) @app.route("/yu/healthcheck", methods=["GET"])
def health_check(): def health_check():
if is_real_mode() and _trader_instance and not _trader_instance.is_available(): if is_real_mode() and _trader_instance and not _trader_instance.is_available():
@ -549,6 +556,12 @@ def clear_strategy(strategy_name):
abort(500, description="服务器内部错误") abort(500, description="服务器内部错误")
@app.route("/yu/test", methods=["GET"])
def test_route():
"""测试路由,用于验证中间件是否正常工作"""
return jsonify({"success": True, "message": "API路由前缀验证成功"}), 200
if __name__ == "__main__": if __name__ == "__main__":
logger.info(f"Server starting on {Config.HOST}:{Config.PORT}") logger.info(f"Server starting on {Config.HOST}:{Config.PORT}")
app.run(debug=Config.DEBUG, host=Config.HOST, port=Config.PORT) app.run(debug=Config.DEBUG, host=Config.HOST, port=Config.PORT)