mirror of https://github.com/k3s-io/k3s
better error handling.
parent
60e2d4b258
commit
9bd4e441b3
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue