首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在马提尼中注射appengine.Context

如何在马提尼中注射appengine.Context
EN

Stack Overflow用户
提问于 2014-05-13 10:48:21
回答 1查看 586关注 0票数 2

当我尝试从中间件注入一个appengine.Context时,这样做:

代码语言:javascript
复制
//Share Context
m.Use(func(r *http.Request) {
    c := appengine.NewContext(r)
    c, err := appengine.Namespace(c, namespace)
    if err != nil {
        c.Debugf("[Namespace] %s", err)
    }
    m.Map(c)
})

我听到一种恐慌的说法,说显然没有appengine.Context可以注射:

惊慌

类型appengine.Context的值未找到

代码语言:javascript
复制
<pre>github.com/go-martini/martini/router.go:320 (0xc3731d)
    (*routeContext).run: panic(err)
github.com/go-martini/martini/router.go:221 (0xc36729)
    (*route).Handle: context.run()
github.com/go-martini/martini/router.go:112 (0xc35628)
    (*router).Handle: route.Handle(context, res)
app/nc_backend.go:37 (0xc30fe0)
    Router.Handle.fm: m.Action(r.Handle)
go/src/pkg/runtime/asm_amd64.s:340 (0xc23b82)
go/src/pkg/reflect/value.go:474 (0xd41bd3)
go/src/pkg/reflect/value.go:345 (0xd40c65)
github.com/codegangsta/inject/inject.go:102 (0xd8449a)
    (*injector).Invoke: return reflect.ValueOf(f).Call(in), nil
github.com/go-martini/martini/martini.go:165 (0xc33607)
    (*context).run: _, err := c.Invoke(c.handler())
github.com/go-martini/martini/martini.go:156 (0xc33500)
    (*context).Next: c.run()
github.com/go-martini/martini/recovery.go:140 (0xc37a4b)
    func.004: c.Next()
go/src/pkg/runtime/asm_amd64.s:339 (0xc23b22)
go/src/pkg/reflect/value.go:474 (0xd41bd3)
go/src/pkg/reflect/value.go:345 (0xd40c65)
github.com/codegangsta/inject/inject.go:102 (0xd8449a)
    (*injector).Invoke: return reflect.ValueOf(f).Call(in), nil
github.com/go-martini/martini/martini.go:165 (0xc33607)
    (*context).run: _, err := c.Invoke(c.handler())
github.com/go-martini/martini/martini.go:69 (0xc32b08)
    (*Martini).ServeHTTP: m.createContext(res, req).run()
go/src/pkg/net/http/server.go:1496 (0xc98dda)
go/src/pkg/appengine_internal/api_prod.go:246 (0xc26e3f)
go/src/pkg/appengine_internal/api_prod.go:212 (0xc268c5)
go/src/pkg/runtime/asm_amd64.s:340 (0xc23b82)
go/src/pkg/reflect/value.go:474 (0xd41bd3)
go/src/pkg/reflect/value.go:345 (0xd40c65)
_:410 (0xcf6255)
go/src/pkg/runtime/proc.c:279 (0xc170a0)

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-13 22:05:05

appengine.Context是一个接口,所以您必须使用替代的MapTo。此外,根据docs,映射应该在马提尼上下文上执行,而不是在martini对象本身上执行。

所以你的代码应该是:

代码语言:javascript
复制
m.Use(func(c martini.Context, req *http.Request) {
    ctx := appengine.NewContext(req)
    ctx, err := appengine.Namespace(ctx, namespace)
    if err != nil {
        ctx.Debugf("[Namespace] %s", err)
    }
    c.MapTo(ctx, (*appengine.Context)(nil))
})
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23629143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档