diff --git a/command/agent/http.go b/command/agent/http.go index fdc6eae520..39e463b80c 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -98,6 +98,11 @@ func (s *HTTPServer) registerHandlers(enableDebug bool) { s.mux.HandleFunc("/debug/pprof/profile", pprof.Profile) s.mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) } + + // Enable the UI + special endpoints + if s.uiDir != "" { + s.mux.HandleFunc("/ui/", s.UiIndex) + } } // wrap is used to wrap functions to make them more convenient diff --git a/command/agent/http_test.go b/command/agent/http_test.go index 5986fd0d9f..128b91d431 100644 --- a/command/agent/http_test.go +++ b/command/agent/http_test.go @@ -9,6 +9,7 @@ import ( "net/http" "net/http/httptest" "os" + "path/filepath" "strconv" "testing" "time" @@ -17,8 +18,10 @@ import ( func makeHTTPServer(t *testing.T) (string, *HTTPServer) { conf := nextConfig() dir, agent := makeAgent(t, conf) + uiDir := filepath.Join(dir, "ui") + println(uiDir) addr, _ := agent.config.ClientListener(agent.config.Ports.HTTP) - server, err := NewHTTPServer(agent, "", true, agent.logOutput, addr.String()) + server, err := NewHTTPServer(agent, uiDir, true, agent.logOutput, addr.String()) if err != nil { t.Fatalf("err: %v", err) } diff --git a/command/agent/ui_endpoint.go b/command/agent/ui_endpoint.go new file mode 100644 index 0000000000..a76fc16e9e --- /dev/null +++ b/command/agent/ui_endpoint.go @@ -0,0 +1,24 @@ +package agent + +import ( + "net/http" + "path/filepath" + "strings" + "time" +) + +// UiIndex serves files in the /ui/ prefix from a preconfigured directory +func (s *HTTPServer) UiIndex(resp http.ResponseWriter, req *http.Request) { + // Invoke the handler + start := time.Now() + defer func() { + s.logger.Printf("[DEBUG] http: Request %v (%v)", req.URL, time.Now().Sub(start)) + }() + + file := strings.TrimPrefix(req.URL.Path, "/ui/") + if file == "" { + file = "index.html" + } + path := filepath.Join(s.uiDir, file) + http.ServeFile(resp, req, path) +} diff --git a/command/agent/ui_endpoint_test.go b/command/agent/ui_endpoint_test.go new file mode 100644 index 0000000000..1db1a8b1be --- /dev/null +++ b/command/agent/ui_endpoint_test.go @@ -0,0 +1,45 @@ +package agent + +import ( + "bytes" + "io" + "io/ioutil" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "testing" +) + +func TestUiIndex(t *testing.T) { + dir, srv := makeHTTPServer(t) + defer os.RemoveAll(dir) + defer srv.Shutdown() + defer srv.agent.Shutdown() + + // Create file + path := filepath.Join(srv.uiDir, "my-file") + ioutil.WriteFile(path, []byte("test"), 777) + + // Register node + req, err := http.NewRequest("GET", "/ui/my-file", nil) + if err != nil { + t.Fatalf("err: %v", err) + } + + // Make the request + resp := httptest.NewRecorder() + srv.UiIndex(resp, req) + + // Verify teh response + if resp.Code != 200 { + t.Fatalf("bad: %v", resp) + } + + // Verify the body + out := bytes.NewBuffer(nil) + io.Copy(out, resp.Body) + if string(out.Bytes()) != "test" { + t.Fatalf("bad: %s", out.Bytes()) + } +}