Merge pull request #61 from Mistobaan/master

some go cleanups
This commit is contained in:
Xiang Li 2013-08-06 16:53:52 -07:00
commit 115dbdb927

View File

@ -14,13 +14,14 @@ import (
// Multiplex GET/POST/DELETE request to corresponding handlers // Multiplex GET/POST/DELETE request to corresponding handlers
func Multiplexer(w http.ResponseWriter, req *http.Request) { func Multiplexer(w http.ResponseWriter, req *http.Request) {
if req.Method == "GET" { switch req.Method {
case "GET":
GetHttpHandler(&w, req) GetHttpHandler(&w, req)
} else if req.Method == "POST" { case "POST":
SetHttpHandler(&w, req) SetHttpHandler(&w, req)
} else if req.Method == "DELETE" { case "DELETE":
DeleteHttpHandler(&w, req) DeleteHttpHandler(&w, req)
} else { default:
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)
return return
} }
@ -69,18 +70,22 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
} }
if len(prevValue) != 0 { if len(prevValue) != 0 {
command := &TestAndSetCommand{} command := &TestAndSetCommand{
command.Key = key Key: key,
command.Value = value Value: value,
command.PrevValue = prevValue PrevValue: prevValue,
command.ExpireTime = expireTime ExpireTime: expireTime,
}
dispatch(command, w, req, true) dispatch(command, w, req, true)
} else { } else {
command := &SetCommand{} command := &SetCommand{
command.Key = key Key: key,
command.Value = value Value: value,
command.ExpireTime = expireTime ExpireTime: expireTime,
}
dispatch(command, w, req, true) dispatch(command, w, req, true)
} }
@ -92,8 +97,9 @@ func DeleteHttpHandler(w *http.ResponseWriter, req *http.Request) {
debugf("[recv] DELETE http://%v/v1/keys/%s", raftServer.Name(), key) debugf("[recv] DELETE http://%v/v1/keys/%s", raftServer.Name(), key)
command := &DeleteCommand{} command := &DeleteCommand{
command.Key = key Key: key,
}
dispatch(command, w, req, true) dispatch(command, w, req, true)
} }
@ -243,8 +249,9 @@ func GetHttpHandler(w *http.ResponseWriter, req *http.Request) {
debugf("[recv] GET http://%v/v1/keys/%s", raftServer.Name(), key) debugf("[recv] GET http://%v/v1/keys/%s", raftServer.Name(), key)
command := &GetCommand{} command := &GetCommand{
command.Key = key Key: key,
}
if body, err := command.Apply(raftServer); err != nil { if body, err := command.Apply(raftServer); err != nil {
@ -274,8 +281,9 @@ func GetHttpHandler(w *http.ResponseWriter, req *http.Request) {
func WatchHttpHandler(w http.ResponseWriter, req *http.Request) { func WatchHttpHandler(w http.ResponseWriter, req *http.Request) {
key := req.URL.Path[len("/v1/watch/"):] key := req.URL.Path[len("/v1/watch/"):]
command := &WatchCommand{} command := &WatchCommand{
command.Key = key Key: key,
}
if req.Method == "GET" { if req.Method == "GET" {
debugf("[recv] GET http://%v/watch/%s", raftServer.Name(), key) debugf("[recv] GET http://%v/watch/%s", raftServer.Name(), key)