Created
September 16, 2014 22:13
-
-
Save hydrogen18/0523d40f6983923bf0cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gocraft_issue_35.go | |
package main | |
import "github.com/gocraft/web" | |
import "net/http" | |
import "fmt" | |
import "io" | |
type Context struct { | |
} | |
type AdminContext struct { | |
*Context | |
} | |
func (ac *AdminContext) Handler(rw web.ResponseWriter, req *web.Request) { | |
rw.WriteHeader(http.StatusOK) | |
io.WriteString(rw, "Hello from admin") | |
} | |
type Middleware struct { | |
Name string | |
} | |
func (mw *Middleware) Middleware(rw web.ResponseWriter, req *web.Request, next web.NextMiddlewareFunc) { | |
fmt.Printf("%s %s %s\n", mw.Name, req.Method, req.URL.Path) | |
next(rw, req) | |
} | |
func main() { | |
mwa := new(Middleware) | |
mwa.Name = "A" | |
rootRouter := web.New(Context{}).Middleware(mwa.Middleware) | |
subrouter := rootRouter.Subrouter(AdminContext{}, "/admin") | |
mwb := new(Middleware) | |
mwb.Name = "B" | |
subrouter.Middleware(mwb.Middleware) | |
subrouter.Get("/", (*AdminContext).Handler) | |
http.ListenAndServe("localhost:3000", rootRouter) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment