| return self.cache | return self.cache | ||||
| } | } | ||||
| // Use add the middleware module into the stack chain. | |||||
| func (self *middleware) Use(modules ...func(http.Handler) http.Handler) { | |||||
| self.stack = append(self.stack, modules...) | |||||
| } | |||||
| // Implements the net/http Handler interface and calls the middleware stack. | // Implements the net/http Handler interface and calls the middleware stack. | ||||
| func (self *middleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { | func (self *middleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||||
| self.build().ServeHTTP(w, r) | self.build().ServeHTTP(w, r) | ||||
| if !self.ready { | if !self.ready { | ||||
| self.ready = true | self.ready = true | ||||
| // * activate router's middleware modules. | // * activate router's middleware modules. | ||||
| self.middleware.Use(func(http.Handler) http.Handler { | |||||
| self.Use(func(http.Handler) http.Handler { | |||||
| return self.mux | return self.mux | ||||
| }) | }) | ||||
| // * activate subrouters's middleware modules. | // * activate subrouters's middleware modules. | ||||
| for index := 0; index < len(self.subrouters); index++ { | for index := 0; index < len(self.subrouters); index++ { | ||||
| sr := self.subrouters[index] | sr := self.subrouters[index] | ||||
| sr.middleware.Use(func(http.Handler) http.Handler { | |||||
| sr.Use(func(http.Handler) http.Handler { | |||||
| return sr.mux | return sr.mux | ||||
| }) | }) | ||||
| } | } | ||||
| // Use add the middleware module into the stack chain. | // Use add the middleware module into the stack chain. | ||||
| func (self *Router) Use(module func(http.Handler) http.Handler) { | func (self *Router) Use(module func(http.Handler) http.Handler) { | ||||
| self.middleware.Use(module) | |||||
| self.middleware.stack = append(self.middleware.stack, module) | |||||
| } | } | ||||
| // ServeHTTP dispatches the request to the handler whose | // ServeHTTP dispatches the request to the handler whose |