0
点赞
收藏
分享

微信扫一扫

Rust :源代码的分层模块结构组织

b91bff6ffdb5 2024-05-15 阅读 13
import os
 
from flask import Flask, request, session
 
app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(24)  # 生成24位的随机数种子,用于产生SESSION ID
 
 
@app.route('/article/<int:article_id>')
def test(article_id):
    """
    路由地址参数, 必须同步定义在接口函数里,作为形参
    """
    return f'你正在访问编号为:{article_id}的文章'
 
 
@app.route('/sess')
def sess():
    session['is_login'] = 'true'
    return 'done'
 
 
@app.before_request
def before():
    """
    针对app实例定义全局拦截器
    """
    url = request.path  # 读取到当前接口的地址
    if url == '/sess':
        pass
    elif session.get('is_login', '') != 'true':
        return '你还没有登录'
    else:
        pass
    
 
if '__name__' == '__main__':
    app.run()

模块拦截器

也叫视图拦截器,只针对某一个模块进行拦截,应用于Blueprint模块中。

把需要事前拦截的接口和拦截器一起放在一个视图模板里

 

comment = Blueprint('comment', __name__)

# #评论操作前通过模块拦截器判断--用户是否登录
@comment.before_request
def before_comment():
    #未登录情况下允许获取评论
    if session.get('islogin') is None or session.get('islogin') != 'true':
        # return '你还没有登录,不能发表评论'
        return 'not-login'
 

举报

相关推荐

0 条评论