web icon indicating copy to clipboard operation
web copied to clipboard

Extending web.Context

Open marconi opened this issue 12 years ago • 2 comments

I found myself repeating the following:

ctx.WriteHeader(400)

// or

ctx.WriteHeader(200)
ctx.Write([]byte(somedata))

so i ember the context instead:

type Context struct {
    *web.Context
}

func (ctx *web.Context) BadRequest() {
    ctx.WriteHeader(400)
}

func (ctx *web.Context) Ok(data []byte) {
    ctx.WriteHeader(200)
    ctx.Write(data)
}

so I can do like:

func (ctx *Context) index() {
    ctx.BadRequest()
}

but I'm getting the following errors:

2013/09/29 05:20:01 web.go serving localhost:9999
2013/09/29 05:20:05 Handler crashed with error reflect: Call with too few input arguments
2013/09/29 05:20:05 /usr/local/go/src/pkg/runtime/panic.c 248
2013/09/29 05:20:05 /usr/local/go/src/pkg/reflect/value.go 397
2013/09/29 05:20:05 /usr/local/go/src/pkg/reflect/value.go 345
2013/09/29 05:20:05 /Users/marconi/go/src/github.com/hoisie/web/server.go 216
2013/09/29 05:20:05 /Users/marconi/go/src/github.com/hoisie/web/server.go 361
2013/09/29 05:20:05 /Users/marconi/go/src/github.com/hoisie/web/server.go 94
2013/09/29 05:20:05 /Users/marconi/go/src/github.com/hoisie/web/server.go 89
2013/09/29 05:20:05 /usr/local/go/src/pkg/net/http/server.go 1494
2013/09/29 05:20:05 /usr/local/go/src/pkg/net/http/server.go 1595
2013/09/29 05:20:05 /usr/local/go/src/pkg/net/http/server.go 1165
2013/09/29 05:20:05 /usr/local/go/src/pkg/runtime/proc.c 1389

marconi avatar Sep 28 '13 21:09 marconi

I've started playing with web and I've seen that type of error when I did like this:

  func main() {
    web.Get("/hello", hello)
    web.Run("0.0.0.0:9999")
  }

  func hello(val string) string {
    return "hello " + val
  }

In the code above the handler hello expects one argument, but that is not provided since the regexp has not any defined group. To fix I've done so:

  func main() {
    web.Get("/(hello)", hello)
    web.Run("0.0.0.0:9999")
  }

I hope it helps.

kitto86 avatar Nov 03 '13 17:11 kitto86

@kitto86 we have different issues, my problem here involves creating custom context by embedding web.Context. Anyway, this framework doesn't seem active anymore. I've long since moved to GoWeb which is a more lightweight and flexible framework.

marconi avatar Nov 03 '13 20:11 marconi