proxy.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package proxy
  14. import (
  15. "net/http"
  16. )
  17. // GetProxyURLs is a function which should return the current set of URLs to
  18. // which client requests should be proxied. This function will be queried
  19. // periodically by the proxy Handler to refresh the set of available
  20. // backends.
  21. type GetProxyURLs func() []string
  22. func NewHandler(t *http.Transport, urlsFunc GetProxyURLs) http.Handler {
  23. return &reverseProxy{
  24. director: newDirector(urlsFunc),
  25. transport: t,
  26. }
  27. }
  28. func readonlyHandlerFunc(next http.Handler) func(http.ResponseWriter, *http.Request) {
  29. return func(w http.ResponseWriter, req *http.Request) {
  30. if req.Method != "GET" {
  31. w.WriteHeader(http.StatusNotImplemented)
  32. return
  33. }
  34. next.ServeHTTP(w, req)
  35. }
  36. }
  37. func NewReadonlyHandler(hdlr http.Handler) http.Handler {
  38. readonly := readonlyHandlerFunc(hdlr)
  39. return http.HandlerFunc(readonly)
  40. }