proxy.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. func NewHandler(t *http.Transport, addrs []string) (http.Handler, error) {
  18. scheme := "http"
  19. if t.TLSClientConfig != nil {
  20. scheme = "https"
  21. }
  22. d, err := newDirector(scheme, addrs)
  23. if err != nil {
  24. return nil, err
  25. }
  26. rp := reverseProxy{
  27. director: d,
  28. transport: t,
  29. }
  30. return &rp, nil
  31. }
  32. func readonlyHandlerFunc(next http.Handler) func(http.ResponseWriter, *http.Request) {
  33. return func(w http.ResponseWriter, req *http.Request) {
  34. if req.Method != "GET" {
  35. w.WriteHeader(http.StatusNotImplemented)
  36. return
  37. }
  38. next.ServeHTTP(w, req)
  39. }
  40. }
  41. func NewReadonlyHandler(hdlr http.Handler) http.Handler {
  42. readonly := readonlyHandlerFunc(hdlr)
  43. return http.HandlerFunc(readonly)
  44. }