python - 如何在烧瓶中创建私有端点
问题描述
在我的烧瓶应用程序中,每当用户单击按钮时,我都需要从数据库中获取数据,我知道我可以使用烧瓶 restful 模块或普通路由来制作烧瓶路由。但我的问题是我是否可以使该路由/端点对用户不可见。
@app.route("/api/fetch/")
def fetch_data():
return some_data
我不希望用户直接访问此端点,我只希望 Web 应用程序能够使用它。不确定是否有可能或在哪里看。
我发现也许使用Flask-CORS会有所帮助。任何帮助或指导将不胜感激。
解决方案
在烧瓶上,您可以使用上述代码段中的蓝图示例
from flask import Blueprint
sample_bp = Blueprint("sample_bp", __name__)
@sample_bp.before_request
def restrict_with_token():
# Do something here on checking header or token
@sample_bp.route("/api/fetch/")
def fetch_api():
# Your logic
否则你可以在这里有一些参考:https ://flask.palletsprojects.com/en/1.1.x/tutorial/views/