martini-contrib / web

hoisie web.go's Context for Martini.
MIT License
6 stars 6 forks source link

PANIC Value not found for type *web.Context #2

Open xyproto opened 10 years ago

xyproto commented 10 years ago

Hi,

The "martini" branch in one of my repositories has an example application that panics when visited, that did not panic with hoisie/web:

https://github.com/xyproto/browserspeak/tree/martini

One way of trying it out

go get github.com/martini-contrib/web
go get github.com/go-martini/martini
mkdir -p $GOPATH/src/github.com/xyproto
cd $GOPATH/src/github.com/xyproto
git clone https://github.com/xyproto/browserspeak.git -b martini
cd browserspeak/example
go build
PORT=8080 ./example

Visiting http://localhost:8080/test.svg works, but http://localhost:8080/ gives the following backtrace:

PANIC

Value not found for type *web.Context

/uio/kant/usit-uait-u1/rodseth/go/src/github.com/go-martini/martini/router.go:352 (0x43dd08)
    (*routeContext).run: panic(err)
/uio/kant/usit-uait-u1/rodseth/go/src/github.com/go-martini/martini/router.go:229 (0x43d102)
    (*route).Handle: context.run()
/uio/kant/usit-uait-u1/rodseth/go/src/github.com/go-martini/martini/router.go:112 (0x43c13c)
    (*router).Handle: route.Handle(context, res)
/uio/kant/usit-uait-u1/rodseth/go/src/github.com/go-martini/martini/martini.go:111 (0x43e5d0)
    Router.Handle.fm: m.Action(r.Handle)
/opt/go/src/pkg/runtime/asm_amd64.s:340 (0x4242b2)
    call64: CALLFN(call64, 64)
/opt/go/src/pkg/reflect/value.go:474 (0x464fbb)
    Value.call: call(fn, ptr, uint32(size))
/opt/go/src/pkg/reflect/value.go:345 (0x4640ad)
    Value.Call: return v.call("Call", in)
/uio/kant/usit-uait-u1/rodseth/go/src/github.com/codegangsta/inject/inject.go:102 (0x4b0ff4)
    (*injector).Invoke: return reflect.ValueOf(f).Call(in), nil
/uio/kant/usit-uait-u1/rodseth/go/src/github.com/go-martini/martini/martini.go:165 (0x43a742)
    (*context).run: _, err := c.Invoke(c.handler())
/uio/kant/usit-uait-u1/rodseth/go/src/github.com/go-martini/martini/martini.go:156 (0x43a69b)
    (*context).Next: c.run()
/uio/kant/usit-uait-u1/rodseth/go/src/github.com/go-martini/martini/recovery.go:140 (0x43eaf6)
    func.004: c.Next()
/opt/go/src/pkg/runtime/asm_amd64.s:339 (0x424252)
    call32: CALLFN(call32, 32)
/opt/go/src/pkg/reflect/value.go:474 (0x464fbb)
    Value.call: call(fn, ptr, uint32(size))
/opt/go/src/pkg/reflect/value.go:345 (0x4640ad)
    Value.Call: return v.call("Call", in)
/uio/kant/usit-uait-u1/rodseth/go/src/github.com/codegangsta/inject/inject.go:102 (0x4b0ff4)
    (*injector).Invoke: return reflect.ValueOf(f).Call(in), nil
/uio/kant/usit-uait-u1/rodseth/go/src/github.com/go-martini/martini/martini.go:165 (0x43a742)
    (*context).run: _, err := c.Invoke(c.handler())
/uio/kant/usit-uait-u1/rodseth/go/src/github.com/go-martini/martini/martini.go:156 (0x43a69b)
    (*context).Next: c.run()
/uio/kant/usit-uait-u1/rodseth/go/src/github.com/go-martini/martini/logger.go:25 (0x43e392)
    func.001: c.Next()
/opt/go/src/pkg/runtime/asm_amd64.s:340 (0x4242b2)
    call64: CALLFN(call64, 64)
/opt/go/src/pkg/reflect/value.go:474 (0x464fbb)
    Value.call: call(fn, ptr, uint32(size))
/opt/go/src/pkg/reflect/value.go:345 (0x4640ad)
    Value.Call: return v.call("Call", in)
/uio/kant/usit-uait-u1/rodseth/go/src/github.com/codegangsta/inject/inject.go:102 (0x4b0ff4)
    (*injector).Invoke: return reflect.ValueOf(f).Call(in), nil
/uio/kant/usit-uait-u1/rodseth/go/src/github.com/go-martini/martini/martini.go:165 (0x43a742)
    (*context).run: _, err := c.Invoke(c.handler())
/uio/kant/usit-uait-u1/rodseth/go/src/github.com/go-martini/martini/martini.go:69 (0x439a63)
    (*Martini).ServeHTTP: m.createContext(res, req).run()
/opt/go/src/pkg/net/http/server.go:1597 (0x48a26e)
    serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/opt/go/src/pkg/net/http/server.go:1167 (0x488277)
    (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/opt/go/src/pkg/runtime/proc.c:1394 (0x417ab0)
    goexit: runtime·goexit(void)

I can not see any references to my own code, so I'm having a hard time pinpointing where this error lies. Any help is appreciated.

Best regards, Alexander Rødseth @gissues:{"order":50,"status":"done"}

mickelsonm commented 10 years ago

Hi @xyproto ! I think you may have overlooked a couple things with your code.

Here's how you have to go about using *web.Context, which in your case didn't have value because you didn't use it! :)

See example below:

    func svgGenerator(ctx *web.Context){
        page := svgPage()
        ctx.WriteString(page.String())
    }

    func defaultHandler(ctx *web.Context){
        ctx.WriteString("TODO: Implement me!")
    }

    func main() {
        fmt.Println("BrowserSpeak Version:", browserspeak.Version)

        m := martini.Classic()
        m.Use(web.ContextWithCookieSecret(""))

        // Connect /test.svg with svgGenerator
        m.Get("/test", svgGenerator)

        //TODO: implement a root handler
        m.Get("/", defaultHandler)

        m.Run()
    }

Also, make sure to implement a root handler! It doesn't know how to handle / on its own...

Hope this helps!

xyproto commented 10 years ago

Thank you! I will look into this.

insionng commented 10 years ago

cannot get req.URL.Query() 's any value from ctx.Params ? I mean martini.Params.