You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
551B

  1. package rex
  2. import "net/http"
  3. type middleware struct {
  4. cache http.Handler
  5. stack []func(http.Handler) http.Handler
  6. }
  7. // Implements the net/http Handler interface and calls the middleware stack.
  8. func (self *middleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  9. if self.cache == nil {
  10. // setup the whole middleware modules in a FIFO chain.
  11. var next http.Handler = http.DefaultServeMux
  12. for index := len(self.stack) - 1; index >= 0; index-- {
  13. next = self.stack[index](next)
  14. }
  15. self.cache = next
  16. }
  17. self.cache.ServeHTTP(w, r)
  18. }