Browse Source

*: serve json version on both client and peer url

Xiang Li 10 years ago
parent
commit
5ad559b503

+ 1 - 1
etcdserver/etcdhttp/client.go

@@ -358,7 +358,7 @@ func serveVersion(w http.ResponseWriter, r *http.Request) {
 	if !allowMethod(w, r.Method, "GET") {
 	if !allowMethod(w, r.Method, "GET") {
 		return
 		return
 	}
 	}
-	w.Write([]byte("etcd " + version.Version))
+	w.Write(version.MarshalJSON())
 }
 }
 
 
 // parseKeyRequest converts a received http.Request on keysPrefix to
 // parseKeyRequest converts a received http.Request on keysPrefix to

+ 3 - 4
etcdserver/etcdhttp/client_test.go

@@ -18,7 +18,6 @@ import (
 	"bytes"
 	"bytes"
 	"encoding/json"
 	"encoding/json"
 	"errors"
 	"errors"
-	"fmt"
 	"io/ioutil"
 	"io/ioutil"
 	"net/http"
 	"net/http"
 	"net/http/httptest"
 	"net/http/httptest"
@@ -1327,9 +1326,9 @@ func TestServeVersion(t *testing.T) {
 	if rw.Code != http.StatusOK {
 	if rw.Code != http.StatusOK {
 		t.Errorf("code=%d, want %d", rw.Code, http.StatusOK)
 		t.Errorf("code=%d, want %d", rw.Code, http.StatusOK)
 	}
 	}
-	w := fmt.Sprintf("etcd %s", version.Version)
-	if g := rw.Body.String(); g != w {
-		t.Fatalf("body = %q, want %q", g, w)
+	w := version.MarshalJSON()
+	if g := rw.Body.String(); g != string(w) {
+		t.Fatalf("body = %q, want %q", g, string(w))
 	}
 	}
 }
 }
 
 

+ 1 - 0
etcdserver/etcdhttp/peer.go

@@ -40,6 +40,7 @@ func NewPeerHandler(clusterInfo etcdserver.ClusterInfo, timer etcdserver.RaftTim
 	mux.Handle(rafthttp.RaftPrefix, raftHandler)
 	mux.Handle(rafthttp.RaftPrefix, raftHandler)
 	mux.Handle(rafthttp.RaftPrefix+"/", raftHandler)
 	mux.Handle(rafthttp.RaftPrefix+"/", raftHandler)
 	mux.Handle(peerMembersPrefix, mh)
 	mux.Handle(peerMembersPrefix, mh)
+	mux.HandleFunc(versionPath, serveVersion)
 	return mux
 	return mux
 }
 }
 
 

+ 17 - 0
version/version.go

@@ -15,6 +15,8 @@
 package version
 package version
 
 
 import (
 import (
+	"encoding/json"
+	"log"
 	"os"
 	"os"
 	"path"
 	"path"
 
 
@@ -37,6 +39,21 @@ const (
 	DataDir2_0_1    DataDirVersion = "2.0.1"
 	DataDir2_0_1    DataDirVersion = "2.0.1"
 )
 )
 
 
+type Versions struct {
+	Server string `json:"etcdserver"`
+	// TODO: etcdcluster version
+	// TODO: raft state machine version
+}
+
+// MarshalJSON returns the JSON encoding of Versions struct.
+func MarshalJSON() []byte {
+	b, err := json.Marshal(Versions{Server: Version})
+	if err != nil {
+		log.Panicf("version: cannot marshal versions to json (%v)", err)
+	}
+	return b
+}
+
 func DetectDataDir(dirpath string) (DataDirVersion, error) {
 func DetectDataDir(dirpath string) (DataDirVersion, error) {
 	names, err := fileutil.ReadDir(dirpath)
 	names, err := fileutil.ReadDir(dirpath)
 	if err != nil {
 	if err != nil {