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.

44 line
1.7KB

  1. /* ----------------------------------------------------------------------
  2. * ______ ___ __
  3. * / ____/___ / | ____ __ ___ __/ /_ ___ ________
  4. * / / __/ __ \/ /| | / __ \/ / / / | /| / / __ \/ _ \/ ___/ _ \
  5. * / /_/ / /_/ / ___ |/ / / / /_/ /| |/ |/ / / / / __/ / / __/
  6. * \____/\____/_/ |_/_/ /_/\__. / |__/|__/_/ /_/\___/_/ \___/
  7. * /____/
  8. *
  9. * (C) Copyright 2015 GoAnywhere (http://goanywhere.io).
  10. * ----------------------------------------------------------------------
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. * ----------------------------------------------------------------------*/
  23. package rex
  24. import "net/http"
  25. type middleware struct {
  26. cache http.Handler
  27. stack []func(http.Handler) http.Handler
  28. }
  29. // Implements the net/http Handler interface and calls the middleware stack.
  30. func (self *middleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  31. if self.cache == nil {
  32. // setup the whole middleware modules in a FIFO chain.
  33. var next http.Handler = http.DefaultServeMux
  34. for index := len(self.stack) - 1; index >= 0; index-- {
  35. next = self.stack[index](next)
  36. }
  37. self.cache = next
  38. }
  39. self.cache.ServeHTTP(w, r)
  40. }