Skip to content

Commit

Permalink
Added a proxy server to cloudcfg
Browse files Browse the repository at this point in the history
  • Loading branch information
brendandburns committed Jun 24, 2014
1 parent b94ceff commit 5756189
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cmd/cloudcfg/cloudcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions hooks/boilerplate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions pkg/cloudcfg/proxy_server.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 5756189

Please sign in to comment.