Browse Source

Revert "Remove web.HandlerFunc"

This reverts commit aee21eb0bd.

Fixes #119.
Carl Jackson 11 years ago
parent
commit
0276f3f3fb
2 changed files with 22 additions and 2 deletions
  1. +6
    -2
      web/router_test.go
  2. +16
    -0
      web/web.go

+ 6
- 2
web/router_test.go View File

@ -88,7 +88,8 @@ var testHandlerTable = map[string]string{
"/a": "http fn", "/a": "http fn",
"/b": "http handler", "/b": "http handler",
"/c": "web fn", "/c": "web fn",
"/d": "httpc",
"/d": "web handler",
"/e": "httpc",
} }
func TestHandlerTypes(t *testing.T) { func TestHandlerTypes(t *testing.T) {
@ -105,7 +106,10 @@ func TestHandlerTypes(t *testing.T) {
m.Get("/c", func(c C, w http.ResponseWriter, r *http.Request) { m.Get("/c", func(c C, w http.ResponseWriter, r *http.Request) {
ch <- "web fn" ch <- "web fn"
}) })
m.Get("/d", testHandler(ch))
m.Get("/d", HandlerFunc(func(c C, w http.ResponseWriter, r *http.Request) {
ch <- "web handler"
}))
m.Get("/e", testHandler(ch))
for route, response := range testHandlerTable { for route, response := range testHandlerTable {
r, _ := http.NewRequest("GET", route, nil) r, _ := http.NewRequest("GET", route, nil)


+ 16
- 0
web/web.go View File

@ -38,6 +38,22 @@ type Handler interface {
ServeHTTPC(C, http.ResponseWriter, *http.Request) ServeHTTPC(C, http.ResponseWriter, *http.Request)
} }
// HandlerFunc is similar to net/http's http.HandlerFunc, but supports a context
// object. Implements both http.Handler and Handler.
type HandlerFunc func(C, http.ResponseWriter, *http.Request)
// ServeHTTP implements http.Handler, allowing HandlerFunc's to be used with
// net/http and other compliant routers. When used in this way, the underlying
// function will be passed an empty context.
func (h HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h(C{}, w, r)
}
// ServeHTTPC implements Handler.
func (h HandlerFunc) ServeHTTPC(c C, w http.ResponseWriter, r *http.Request) {
h(c, w, r)
}
/* /*
PatternType is the type denoting Patterns and types that Goji internally PatternType is the type denoting Patterns and types that Goji internally
converts to Pattern (via the ParsePattern function). In order to provide an converts to Pattern (via the ParsePattern function). In order to provide an


Loading…
Cancel
Save