Browse Source

change name to url in http handlers

Xiang Li 12 years ago
parent
commit
32d1681b6b
3 changed files with 32 additions and 35 deletions
  1. 5 20
      etcd_handlers.go
  2. 7 15
      raft_handlers.go
  3. 20 0
      util.go

+ 5 - 20
etcd_handlers.go

@@ -5,7 +5,6 @@ import (
 	"github.com/coreos/etcd/store"
 	"github.com/coreos/etcd/store"
 	"net/http"
 	"net/http"
 	"strconv"
 	"strconv"
-	"time"
 )
 )
 
 
 //-------------------------------------------------------------------
 //-------------------------------------------------------------------
@@ -45,7 +44,7 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
 		return
 		return
 	}
 	}
 
 
-	debugf("[recv] POST %v/v1/keys/%s", raftServer.Name(), key)
+	debugf("[recv] POST %v/v1/keys/%s", info.EtcdURL, key)
 
 
 	value := req.FormValue("value")
 	value := req.FormValue("value")
 
 
@@ -96,7 +95,7 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
 func DeleteHttpHandler(w *http.ResponseWriter, req *http.Request) {
 func DeleteHttpHandler(w *http.ResponseWriter, req *http.Request) {
 	key := req.URL.Path[len("/v1/keys/"):]
 	key := req.URL.Path[len("/v1/keys/"):]
 
 
-	debugf("[recv] DELETE %v/v1/keys/%s", raftServer.Name(), key)
+	debugf("[recv] DELETE %v/v1/keys/%s", info.EtcdURL, key)
 
 
 	command := &DeleteCommand{
 	command := &DeleteCommand{
 		Key: key,
 		Key: key,
@@ -251,7 +250,7 @@ func StatsHttpHandler(w http.ResponseWriter, req *http.Request) {
 func GetHttpHandler(w *http.ResponseWriter, req *http.Request) {
 func GetHttpHandler(w *http.ResponseWriter, req *http.Request) {
 	key := req.URL.Path[len("/v1/keys/"):]
 	key := req.URL.Path[len("/v1/keys/"):]
 
 
-	debugf("[recv] GET http://%v/v1/keys/%s", raftServer.Name(), key)
+	debugf("[recv] GET %s/v1/keys/%s", info.EtcdURL, key)
 
 
 	command := &GetCommand{
 	command := &GetCommand{
 		Key: key,
 		Key: key,
@@ -290,13 +289,13 @@ func WatchHttpHandler(w http.ResponseWriter, req *http.Request) {
 	}
 	}
 
 
 	if req.Method == "GET" {
 	if req.Method == "GET" {
-		debugf("[recv] GET http://%v/watch/%s", raftServer.Name(), key)
+		debugf("[recv] GET %s/watch/%s", info.EtcdURL, key)
 		command.SinceIndex = 0
 		command.SinceIndex = 0
 
 
 	} else if req.Method == "POST" {
 	} else if req.Method == "POST" {
 		// watch from a specific index
 		// watch from a specific index
 
 
-		debugf("[recv] POST http://%v/watch/%s", raftServer.Name(), key)
+		debugf("[recv] POST %s/watch/%s", info.EtcdURL, key)
 		content := req.FormValue("index")
 		content := req.FormValue("index")
 
 
 		sinceIndex, err := strconv.ParseUint(string(content), 10, 64)
 		sinceIndex, err := strconv.ParseUint(string(content), 10, 64)
@@ -340,17 +339,3 @@ func TestHttpHandler(w http.ResponseWriter, req *http.Request) {
 
 
 	w.WriteHeader(http.StatusBadRequest)
 	w.WriteHeader(http.StatusBadRequest)
 }
 }
-
-// Convert string duration to time format
-func durationToExpireTime(strDuration string) (time.Time, error) {
-	if strDuration != "" {
-		duration, err := strconv.Atoi(strDuration)
-
-		if err != nil {
-			return time.Unix(0, 0), err
-		}
-		return time.Now().Add(time.Second * (time.Duration)(duration)), nil
-	} else {
-		return time.Unix(0, 0), nil
-	}
-}

+ 7 - 15
raft_handlers.go

@@ -12,8 +12,7 @@ import (
 
 
 // Get all the current logs
 // Get all the current logs
 func GetLogHttpHandler(w http.ResponseWriter, req *http.Request) {
 func GetLogHttpHandler(w http.ResponseWriter, req *http.Request) {
-	u, _ := nameToRaftURL(raftServer.Name())
-	debugf("[recv] GET %s/log", u)
+	debugf("[recv] GET %s/log", info.RaftURL)
 	w.Header().Set("Content-Type", "application/json")
 	w.Header().Set("Content-Type", "application/json")
 	w.WriteHeader(http.StatusOK)
 	w.WriteHeader(http.StatusOK)
 	json.NewEncoder(w).Encode(raftServer.LogEntries())
 	json.NewEncoder(w).Encode(raftServer.LogEntries())
@@ -24,8 +23,7 @@ func VoteHttpHandler(w http.ResponseWriter, req *http.Request) {
 	rvreq := &raft.RequestVoteRequest{}
 	rvreq := &raft.RequestVoteRequest{}
 	err := decodeJsonRequest(req, rvreq)
 	err := decodeJsonRequest(req, rvreq)
 	if err == nil {
 	if err == nil {
-		u, _ := nameToRaftURL(raftServer.Name())
-		debugf("[recv] POST %s/vote [%s]", u, rvreq.CandidateName)
+		debugf("[recv] POST %s/vote [%s]", info.RaftURL, rvreq.CandidateName)
 		if resp := raftServer.RequestVote(rvreq); resp != nil {
 		if resp := raftServer.RequestVote(rvreq); resp != nil {
 			w.WriteHeader(http.StatusOK)
 			w.WriteHeader(http.StatusOK)
 			json.NewEncoder(w).Encode(resp)
 			json.NewEncoder(w).Encode(resp)
@@ -42,8 +40,7 @@ func AppendEntriesHttpHandler(w http.ResponseWriter, req *http.Request) {
 	err := decodeJsonRequest(req, aereq)
 	err := decodeJsonRequest(req, aereq)
 
 
 	if err == nil {
 	if err == nil {
-		u, _ := nameToRaftURL(raftServer.Name())
-		debugf("[recv] POST %s/log/append [%d]", u, len(aereq.Entries))
+		debugf("[recv] POST %s/log/append [%d]", info.RaftURL, len(aereq.Entries))
 		if resp := raftServer.AppendEntries(aereq); resp != nil {
 		if resp := raftServer.AppendEntries(aereq); resp != nil {
 			w.WriteHeader(http.StatusOK)
 			w.WriteHeader(http.StatusOK)
 			json.NewEncoder(w).Encode(resp)
 			json.NewEncoder(w).Encode(resp)
@@ -62,8 +59,7 @@ func SnapshotHttpHandler(w http.ResponseWriter, req *http.Request) {
 	aereq := &raft.SnapshotRequest{}
 	aereq := &raft.SnapshotRequest{}
 	err := decodeJsonRequest(req, aereq)
 	err := decodeJsonRequest(req, aereq)
 	if err == nil {
 	if err == nil {
-		u, _ := nameToRaftURL(raftServer.Name())
-		debugf("[recv] POST %s/snapshot/ ", u)
+		debugf("[recv] POST %s/snapshot/ ", info.RaftURL)
 		if resp := raftServer.RequestSnapshot(aereq); resp != nil {
 		if resp := raftServer.RequestSnapshot(aereq); resp != nil {
 			w.WriteHeader(http.StatusOK)
 			w.WriteHeader(http.StatusOK)
 			json.NewEncoder(w).Encode(resp)
 			json.NewEncoder(w).Encode(resp)
@@ -79,8 +75,7 @@ func SnapshotRecoveryHttpHandler(w http.ResponseWriter, req *http.Request) {
 	aereq := &raft.SnapshotRecoveryRequest{}
 	aereq := &raft.SnapshotRecoveryRequest{}
 	err := decodeJsonRequest(req, aereq)
 	err := decodeJsonRequest(req, aereq)
 	if err == nil {
 	if err == nil {
-		u, _ := nameToRaftURL(raftServer.Name())
-		debugf("[recv] POST %s/snapshotRecovery/ ", u)
+		debugf("[recv] POST %s/snapshotRecovery/ ", info.RaftURL)
 		if resp := raftServer.SnapshotRecoveryRequest(aereq); resp != nil {
 		if resp := raftServer.SnapshotRecoveryRequest(aereq); resp != nil {
 			w.WriteHeader(http.StatusOK)
 			w.WriteHeader(http.StatusOK)
 			json.NewEncoder(w).Encode(resp)
 			json.NewEncoder(w).Encode(resp)
@@ -93,8 +88,7 @@ func SnapshotRecoveryHttpHandler(w http.ResponseWriter, req *http.Request) {
 
 
 // Get the port that listening for etcd connecting of the server
 // Get the port that listening for etcd connecting of the server
 func EtcdURLHttpHandler(w http.ResponseWriter, req *http.Request) {
 func EtcdURLHttpHandler(w http.ResponseWriter, req *http.Request) {
-	u, _ := nameToRaftURL(raftServer.Name())
-	debugf("[recv] Get %s/etcdURL/ ", u)
+	debugf("[recv] Get %s/etcdURL/ ", info.RaftURL)
 	w.WriteHeader(http.StatusOK)
 	w.WriteHeader(http.StatusOK)
 	w.Write([]byte(argInfo.EtcdURL))
 	w.Write([]byte(argInfo.EtcdURL))
 }
 }
@@ -115,9 +109,7 @@ func JoinHttpHandler(w http.ResponseWriter, req *http.Request) {
 
 
 // Response to the name request
 // Response to the name request
 func NameHttpHandler(w http.ResponseWriter, req *http.Request) {
 func NameHttpHandler(w http.ResponseWriter, req *http.Request) {
-	u, _ := nameToRaftURL(raftServer.Name())
-
-	debugf("[recv] Get %s/name/ ", u)
+	debugf("[recv] Get %s/name/ ", info.RaftURL)
 	w.WriteHeader(http.StatusOK)
 	w.WriteHeader(http.StatusOK)
 	w.Write([]byte(raftServer.Name()))
 	w.Write([]byte(raftServer.Name()))
 }
 }

+ 20 - 0
util.go

@@ -8,8 +8,28 @@ import (
 	"log"
 	"log"
 	"net/http"
 	"net/http"
 	"os"
 	"os"
+	"strconv"
+	"time"
 )
 )
 
 
+//--------------------------------------
+// etcd http Helper
+//--------------------------------------
+
+// Convert string duration to time format
+func durationToExpireTime(strDuration string) (time.Time, error) {
+	if strDuration != "" {
+		duration, err := strconv.Atoi(strDuration)
+
+		if err != nil {
+			return time.Unix(0, 0), err
+		}
+		return time.Now().Add(time.Second * (time.Duration)(duration)), nil
+	} else {
+		return time.Unix(0, 0), nil
+	}
+}
+
 //--------------------------------------
 //--------------------------------------
 // Web Helper
 // Web Helper
 //--------------------------------------
 //--------------------------------------