better error handling.

pull/6/head
Brendan Burns 2014-06-24 13:36:57 -07:00
parent 60e2d4b258
commit 9bd4e441b3
2 changed files with 9 additions and 24 deletions

View File

@ -18,7 +18,6 @@ package kubecfg
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@ -31,18 +30,18 @@ type ProxyServer struct {
Client *client.Client
}
func makeFileHandler(prefix, base string) http.Handler {
return http.StripPrefix(prefix, http.FileServer(http.Dir(base)))
}
func NewProxyServer(filebase, host string, auth *client.AuthInfo) *ProxyServer {
server := &ProxyServer{
Host: host,
Auth: auth,
Client: client.New(host, auth),
}
fileServer := &fileServer{
prefix: "/static/",
base: filebase,
}
http.Handle("/api/", server)
http.Handle("/static/", fileServer)
http.Handle("/static/", makeFileHandler("/static/", filebase))
return server
}
@ -74,15 +73,3 @@ func (s *ProxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
w.Write(data)
}
type fileServer struct {
prefix string
base string
}
func (f *fileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
filename := r.URL.Path[len(f.prefix):]
bytes, _ := ioutil.ReadFile(f.base + filename)
w.WriteHeader(http.StatusOK)
w.Write(bytes)
}

View File

@ -29,13 +29,11 @@ func TestFileServing(t *testing.T) {
expectNoError(t, err)
err = ioutil.WriteFile(dir+"/test.txt", []byte(data), 0755)
expectNoError(t, err)
handler := fileServer{
prefix: "/foo/",
base: dir,
}
server := httptest.NewServer(&handler)
prefix := "/foo/"
handler := makeFileHandler(prefix, dir)
server := httptest.NewServer(handler)
client := http.Client{}
req, err := http.NewRequest("GET", server.URL+handler.prefix+"/test.txt", nil)
req, err := http.NewRequest("GET", server.URL+prefix+"test.txt", nil)
expectNoError(t, err)
res, err := client.Do(req)
expectNoError(t, err)