From 5756189f0db828829603d6e101d99254a0c3a9ed Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Mon, 23 Jun 2014 19:57:54 -0700 Subject: [PATCH] Added a proxy server to cloudcfg --- cmd/cloudcfg/cloudcfg.go | 11 ++++++++ hooks/boilerplate.sh | 1 + pkg/cloudcfg/proxy_server.go | 49 ++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 pkg/cloudcfg/proxy_server.go diff --git a/cmd/cloudcfg/cloudcfg.go b/cmd/cloudcfg/cloudcfg.go index 8675ebec7f..10d5e3b17a 100644 --- a/cmd/cloudcfg/cloudcfg.go +++ b/cmd/cloudcfg/cloudcfg.go @@ -21,6 +21,7 @@ import ( "fmt" "io/ioutil" "log" + "net/http" "net/url" "os" "strconv" @@ -46,6 +47,7 @@ var ( json = flag.Bool("json", false, "If true, print raw JSON for responses") yaml = flag.Bool("yaml", false, "If true, print raw YAML for responses") verbose = flag.Bool("verbose", false, "If true, print extra information") + proxy = flag.Bool("proxy", false, "If true, run a proxy to the api server") ) func usage() { @@ -118,6 +120,15 @@ func main() { } } + if *proxy { + server := cloudcfg.ProxyServer{ + Host: *httpServer, + Client: &http.Client{}, + } + http.Handle("/api/", &server) + log.Fatal(http.ListenAndServe(":8001", nil)) + } + matchFound := executeAPIRequest(method, auth) || executeControllerRequest(method, auth) if matchFound == false { log.Fatalf("Unknown command %s", method) diff --git a/hooks/boilerplate.sh b/hooks/boilerplate.sh index 33c31cbf5b..17d049b9cf 100755 --- a/hooks/boilerplate.sh +++ b/hooks/boilerplate.sh @@ -6,6 +6,7 @@ LINES=$(cat "$(dirname $0)/boilerplate.txt" | wc -l) DIFFER=$(head -$LINES "${FILE}" | diff -q - "$(dirname $0)/boilerplate.txt") if [[ -z "${DIFFER}" ]]; then + echo "${DIFFER}" echo "1" exit 0 fi diff --git a/pkg/cloudcfg/proxy_server.go b/pkg/cloudcfg/proxy_server.go new file mode 100644 index 0000000000..77270edad7 --- /dev/null +++ b/pkg/cloudcfg/proxy_server.go @@ -0,0 +1,49 @@ +/* +Copyright 2014 Google Inc. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cloudcfg + +import ( + "io/ioutil" + "log" + "net/http" +) + +type ProxyServer struct { + Host string + Client *http.Client +} + +func (s *ProxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { + req, err := http.NewRequest(r.Method, s.Host+r.URL.Path, r.Body) + if err != nil { + log.Printf("Error: %#v", err) + return + } + res, err := s.Client.Do(req) + if err != nil { + log.Printf("Error: %#v", err) + return + } + w.WriteHeader(res.StatusCode) + defer res.Body.Close() + data, err := ioutil.ReadAll(res.Body) + if err != nil { + log.Printf("Error: %#v", err) + return + } + w.Write(data) +}