kubectl proxy: make static prefix configurable

This commit is contained in:
Jimmi Dyson 2015-02-04 09:31:39 +00:00
parent 9384f01d56
commit e2baf049c0
3 changed files with 12 additions and 5 deletions

View File

@ -75,7 +75,8 @@ Usage:
--v=0: log level for V logs --v=0: log level for V logs
--validate=false: If true, use a schema to validate the input before sending it --validate=false: If true, use a schema to validate the input before sending it
--vmodule=: comma-separated list of pattern=N settings for file-filtered logging --vmodule=: comma-separated list of pattern=N settings for file-filtered logging
-w, --www="": Also serve static files from the given directory under the prefix /static -w, --www="": Also serve static files from the given directory under the specified prefix
-P, --www-prefix="/static/": Prefix to serve static files under, if static file dir is specified
``` ```

View File

@ -18,6 +18,7 @@ package cmd
import ( import (
"io" "io"
"strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/golang/glog" "github.com/golang/glog"
@ -36,12 +37,17 @@ func (f *Factory) NewCmdProxy(out io.Writer) *cobra.Command {
clientConfig, err := f.ClientConfig(cmd) clientConfig, err := f.ClientConfig(cmd)
checkErr(err) checkErr(err)
server, err := kubectl.NewProxyServer(GetFlagString(cmd, "www"), clientConfig) staticPrefix := GetFlagString(cmd, "www-prefix")
if !strings.HasSuffix(staticPrefix, "/") {
staticPrefix += "/"
}
server, err := kubectl.NewProxyServer(GetFlagString(cmd, "www"), staticPrefix, clientConfig)
checkErr(err) checkErr(err)
glog.Fatal(server.Serve(port)) glog.Fatal(server.Serve(port))
}, },
} }
cmd.Flags().StringP("www", "w", "", "Also serve static files from the given directory under the prefix /static") cmd.Flags().StringP("www", "w", "", "Also serve static files from the given directory under the specified prefix")
cmd.Flags().StringP("www-prefix", "P", "/static/", "Prefix to serve static files under, if static file dir is specified")
cmd.Flags().IntP("port", "p", 8001, "The port on which to run the proxy") cmd.Flags().IntP("port", "p", 8001, "The port on which to run the proxy")
return cmd return cmd
} }

View File

@ -33,7 +33,7 @@ type ProxyServer struct {
// NewProxyServer creates and installs a new ProxyServer. // NewProxyServer creates and installs a new ProxyServer.
// It automatically registers the created ProxyServer to http.DefaultServeMux. // It automatically registers the created ProxyServer to http.DefaultServeMux.
func NewProxyServer(filebase string, cfg *client.Config) (*ProxyServer, error) { func NewProxyServer(filebase string, staticPrefix string, cfg *client.Config) (*ProxyServer, error) {
prefix := cfg.Prefix prefix := cfg.Prefix
if prefix == "" { if prefix == "" {
prefix = "/api" prefix = "/api"
@ -47,7 +47,7 @@ func NewProxyServer(filebase string, cfg *client.Config) (*ProxyServer, error) {
return nil, err return nil, err
} }
http.Handle("/api/", http.StripPrefix("/api/", proxy)) http.Handle("/api/", http.StripPrefix("/api/", proxy))
http.Handle("/static/", newFileHandler("/static/", filebase)) http.Handle(staticPrefix, newFileHandler(staticPrefix, filebase))
return proxy, nil return proxy, nil
} }