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')
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容