diff --git a/httpd/handler.go b/httpd/handler.go index e48c26b79f..e3e63ae507 100644 --- a/httpd/handler.go +++ b/httpd/handler.go @@ -99,6 +99,14 @@ func NewHandler(s *influxdb.Server, requireAuthentication bool, version string) "process_continuous_queries", "POST", "/process_continuous_queries", false, false, h.serveProcessContinuousQueries, }, + route{ + "index-json", // Query serving route. + "GET", "/index.json", true, true, h.serveIndexJson, + }, + route{ + "index", // Query serving route. + "GET", "/", true, true, h.serveIndex, + }, ) for _, r := range h.routes { @@ -253,6 +261,31 @@ func (h *Handler) servePing(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) } +// serveIndex returns the current index of the node as the body of the response +func (h *Handler) serveIndex(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(fmt.Sprintf("%d", h.server.Index()))) +} + +// serveIndexJson returns the current index of the node as json +func (h *Handler) serveIndexJson(w http.ResponseWriter, r *http.Request) { + w.Header().Add("content-type", "application/json") + + pretty := r.URL.Query().Get("pretty") == "true" + + data := struct { + Index uint64 `json:"index"` + }{ + Index: h.server.Index(), + } + var b []byte + if pretty { + b, _ = json.MarshalIndent(data, "", " ") + } else { + b, _ = json.Marshal(data) + } + w.Write(b) +} + // serveDataNodes returns a list of all data nodes in the cluster. func (h *Handler) serveDataNodes(w http.ResponseWriter, r *http.Request) { // Generate a list of objects for encoding to the API. diff --git a/httpd/handler_test.go b/httpd/handler_test.go index 148537e82e..fff5b41e42 100644 --- a/httpd/handler_test.go +++ b/httpd/handler_test.go @@ -535,6 +535,45 @@ func TestHandler_GzipDisabled(t *testing.T) { t.Fatalf("unexpected Content-Encoding. expected %q, actual: %q", "", ce) } } +func TestHandler_Index(t *testing.T) { + srvr := OpenAuthlessServer(NewMessagingClient()) + s := NewHTTPServer(srvr) + defer s.Close() + + status, body := MustHTTP("GET", s.URL, nil, nil, "") + + if status != http.StatusOK { + t.Fatalf("unexpected status: %d", status) + } + + if body != "1" { + t.Fatalf("unexpected body. expected %q, actual %q", "1", body) + } +} + +func TestHandler_IndexJson(t *testing.T) { + srvr := OpenAuthlessServer(NewMessagingClient()) + s := NewHTTPServer(srvr) + defer s.Close() + + status, body := MustHTTP("GET", s.URL+`/index.json`, nil, nil, "") + + if status != http.StatusOK { + t.Fatalf("unexpected status: %d", status) + } + + var data = struct { + Id uint64 `json:"index"` + }{} + + if err := json.Unmarshal([]byte(body), &data); err != nil { + t.Error(err) + } + if data.Id != 1 { + t.Log("body: ", body) + t.Fatalf("unexpected index, expected 1, actual: %d", data.Id) + } +} func TestHandler_Ping(t *testing.T) { srvr := OpenAuthlessServer(NewMessagingClient())