Vlang:Global authentication middleware

module main

import veb

struct Context {
    veb.Context
}

struct App {
    veb.Middleware[Context]
}

fn (mut app App) index(mut ctx Context) veb.Result {
    return ctx.json('index succcess') //不正常
}

fn main() {
    port := 9009
    mut app := &App{}
    app.use(app.authority_middleware())
    veb.run[App, Context](mut app, port)
}

// 初始化中间件并设置 handler ,并返回中间件选项
fn (mut app App) authority_middleware() veb.MiddlewareOptions[Context] {
    return veb.MiddlewareOptions[Context]{
        handler: verify_app
        after:   false
    }
}

fn verify_app(mut ctx Context) bool {
    if true {
        send_error(mut ctx, 'err token')
    }
    return true
}

fn send_error(mut ctx Context, msg string) veb.Result {
    // ctx.res.set_status(.unauthorized)
    return ctx.text('error unauthorized')
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容