Skip to content

Commit

Permalink
Merge pull request #2715 from xiang90/version
Browse files Browse the repository at this point in the history
*: serve json version on both client and peer url
  • Loading branch information
xiang90 committed Apr 20, 2015
2 parents 9dd7c1c + 5ad559b commit f077092
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion etcdserver/etcdhttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func serveVersion(w http.ResponseWriter, r *http.Request) {
if !allowMethod(w, r.Method, "GET") {
return
}
w.Write([]byte("etcd " + version.Version))
w.Write(version.MarshalJSON())
}

// parseKeyRequest converts a received http.Request on keysPrefix to
Expand Down
7 changes: 3 additions & 4 deletions etcdserver/etcdhttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -1327,9 +1326,9 @@ func TestServeVersion(t *testing.T) {
if 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))
}
}

Expand Down
1 change: 1 addition & 0 deletions etcdserver/etcdhttp/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func NewPeerHandler(clusterInfo etcdserver.ClusterInfo, timer etcdserver.RaftTim
mux.Handle(rafthttp.RaftPrefix, raftHandler)
mux.Handle(rafthttp.RaftPrefix+"/", raftHandler)
mux.Handle(peerMembersPrefix, mh)
mux.HandleFunc(versionPath, serveVersion)
return mux
}

Expand Down
17 changes: 17 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package version

import (
"encoding/json"
"log"
"os"
"path"

Expand All @@ -37,6 +39,21 @@ const (
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) {
names, err := fileutil.ReadDir(dirpath)
if err != nil {
Expand Down

0 comments on commit f077092

Please sign in to comment.