mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 04:33:26 +00:00
Merge pull request #233 from brendandburns/demo
switch to built in golang FileServer
This commit is contained in:
commit
3dddb1694c
@ -18,7 +18,6 @@ package kubecfg
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
@ -31,18 +30,18 @@ type ProxyServer struct {
|
|||||||
Client *client.Client
|
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 {
|
func NewProxyServer(filebase, host string, auth *client.AuthInfo) *ProxyServer {
|
||||||
server := &ProxyServer{
|
server := &ProxyServer{
|
||||||
Host: host,
|
Host: host,
|
||||||
Auth: auth,
|
Auth: auth,
|
||||||
Client: client.New(host, auth),
|
Client: client.New(host, auth),
|
||||||
}
|
}
|
||||||
fileServer := &fileServer{
|
|
||||||
prefix: "/static/",
|
|
||||||
base: filebase,
|
|
||||||
}
|
|
||||||
http.Handle("/api/", server)
|
http.Handle("/api/", server)
|
||||||
http.Handle("/static/", fileServer)
|
http.Handle("/static/", makeFileHandler("/static/", filebase))
|
||||||
return server
|
return server
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,15 +73,3 @@ func (s *ProxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
w.Write(data)
|
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)
|
expectNoError(t, err)
|
||||||
err = ioutil.WriteFile(dir+"/test.txt", []byte(data), 0755)
|
err = ioutil.WriteFile(dir+"/test.txt", []byte(data), 0755)
|
||||||
expectNoError(t, err)
|
expectNoError(t, err)
|
||||||
handler := fileServer{
|
prefix := "/foo/"
|
||||||
prefix: "/foo/",
|
handler := makeFileHandler(prefix, dir)
|
||||||
base: dir,
|
server := httptest.NewServer(handler)
|
||||||
}
|
|
||||||
server := httptest.NewServer(&handler)
|
|
||||||
client := http.Client{}
|
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)
|
expectNoError(t, err)
|
||||||
res, err := client.Do(req)
|
res, err := client.Do(req)
|
||||||
expectNoError(t, err)
|
expectNoError(t, err)
|
||||||
|
Loading…
Reference in New Issue
Block a user