Added a proxy server to cloudcfg

pull/6/head
Brendan Burns 2014-06-23 19:57:54 -07:00
parent b94ceffbed
commit 5756189f0d
3 changed files with 61 additions and 0 deletions

View File

@ -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)

View File

@ -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

View File

@ -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)
}