Middleware exception
shawnye opened this issue · 3 comments
shawnye commented
I just write a middleware toy:
var router *macaron.Macaron
func init(){
router = macaron.Classic()
router.Use(session.Sessioner())
router.Use(PresetCtx)
router.Use(pongo2.Pongoer())
router.Route("/goto_login", "POST,GET", func(ctx *macaron.Context, sess session.Store) {
// Use PresetCtx
// ctx.Data["AppName"] = GetAppName(sess)
// ctx.Data["IP"] = ctx.RemoteAddr
ctx.HTML(200, "sys/login")
})
log.Fatal(http.ListenAndServe(":8080", router))
}
func PresetCtx() macaron.Handler {
fmt.Println("PresetCtx")
return func( /*req *http.Request,*/ ctx *macaron.Context /*, sess session.Store*/) {
fmt.Println("$$$$before a request")
// ctx.Data["AppName"] = GetAppName(sess)
ctx.Data["IP"] = ctx.RemoteAddr
ctx.Next()
}
}
when server started, it just show following message on the page, what's wrong with it , thank you!
<func(*macaron.Context) Value>
humaidq commented
Hello.
Please replace `router.Use(PresetCtx)` with `router.Use(PresetCtx())`.
You need to call the function to return `macaron.Handler` and not the
pointer to the function.
shawnye commented
It worked, thanks all of you!