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

2016-08-11 11:43:59 +08:00
 ryanking8215

应用的请求带有一个 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, 这个怎么解?

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

3625 次点击
所在节点    程序员
2 条回复
ryanking8215
2016-08-11 14:46:06 +08:00
自问自答了, 不使用 filter, 实现 func (c *BaseController) Prepare() , 在里面取 header 参数赋值.
yanyuan2046
2016-08-12 11:26:10 +08:00
远离 beego

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/298592

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX