V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ryanking8215
V2EX  ›  程序员

beego 里如何处理 http 自定义头并加在 controller 里

  •  
  •   ryanking8215 ·
    ryanking8215 · 2016-08-11 11:43:59 +08:00 · 3592 次点击
    这是一个创建于 2786 天前的主题,其中的信息可能已经有所发展或是发生改变。

    应用的请求带有一个 http 扩展头, 例如x-my-app: 1234,类似于这样的. 想通过 beego 的 filter, 将扩展头解析出来, 然后传给 controller, 这样就不需要在每个 controller 里处理了.

    controller 定义成:

    type BaseController struct {
        my-app-value string   // 这里想 hold 住扩展头里的值
        beego.Controller
    }
    
    type MyController1 struct {
        BaseController
    }
    
    type MyController2 struct {
        BaseController
    }
    
    

    但是 filter 的回调参数类型是*context.Context, 无法使用controller, 这个怎么解?

    如果不是这样的方法, 还有什么更好的方法, 谢谢各位!

    2 条回复    2016-08-12 11:26:10 +08:00
    ryanking8215
        1
    ryanking8215  
    OP
       2016-08-11 14:46:06 +08:00
    自问自答了, 不使用 filter, 实现 func (c *BaseController) Prepare() , 在里面取 header 参数赋值.
    yanyuan2046
        2
    yanyuan2046  
       2016-08-12 11:26:10 +08:00
    远离 beego
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1043 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 22:24 · PVG 06:24 · LAX 15:24 · JFK 18:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.