etcd/client_handlers.go

274 lines
5.9 KiB
Go
Raw Normal View History

package main
import (
"net/http"
"strconv"
2013-06-21 02:59:23 +04:00
"time"
)
2013-06-13 22:01:06 +04:00
//-------------------------------------------------------------------
// Handlers to handle etcd-store related request via raft client port
//-------------------------------------------------------------------
// Multiplex GET/POST/DELETE request to corresponding handlers
2013-07-01 22:16:30 +04:00
func Multiplexer(w http.ResponseWriter, req *http.Request) {
2013-06-13 22:01:06 +04:00
2013-07-01 22:16:30 +04:00
if req.Method == "GET" {
GetHttpHandler(&w, req)
} else if req.Method == "POST" {
SetHttpHandler(&w, req)
} else if req.Method == "DELETE" {
DeleteHttpHandler(&w, req)
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
2013-06-21 02:59:23 +04:00
return
}
2013-07-01 22:16:30 +04:00
}
//--------------------------------------
2013-07-10 04:31:24 +04:00
// State sensitive handlers
// Set/Delte will dispatch to leader
//--------------------------------------
2013-07-10 01:55:45 +04:00
// Set Command Handler
2013-07-01 22:16:30 +04:00
func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
key := req.URL.Path[len("/v1/keys/"):]
2013-07-10 01:55:45 +04:00
debug("[recv] POST http://%v/v1/keys/%s", raftServer.Name(), key)
command := &SetCommand{}
command.Key = key
2013-07-01 22:16:30 +04:00
command.Value = req.FormValue("value")
strDuration := req.FormValue("ttl")
var err error
command.ExpireTime, err = durationToExpireTime(strDuration)
if err != nil {
warn("The given duration is not a number: %v", err)
(*w).WriteHeader(http.StatusInternalServerError)
}
dispatch(command, w, req)
}
// TestAndSet handler
2013-07-08 22:00:10 +04:00
func TestAndSetHttpHandler(w http.ResponseWriter, req *http.Request) {
key := req.URL.Path[len("/v1/testAndSet/"):]
2013-07-10 01:55:45 +04:00
debug("[recv] POST http://%v/v1/testAndSet/%s", raftServer.Name(), key)
2013-07-08 22:00:10 +04:00
command := &TestAndSetCommand{}
command.Key = key
command.PrevValue = req.FormValue("prevValue")
command.Value = req.FormValue("value")
strDuration := req.FormValue("ttl")
var err error
2013-07-08 22:00:10 +04:00
command.ExpireTime, err = durationToExpireTime(strDuration)
2013-07-08 22:00:10 +04:00
if err != nil {
warn("The given duration is not a number: %v", err)
w.WriteHeader(http.StatusInternalServerError)
}
2013-07-10 04:31:24 +04:00
dispatch(command, &w, req)
2013-07-08 22:00:10 +04:00
}
// Delete Handler
2013-07-01 22:16:30 +04:00
func DeleteHttpHandler(w *http.ResponseWriter, req *http.Request) {
key := req.URL.Path[len("/v1/keys/"):]
2013-06-09 22:12:32 +04:00
2013-07-10 01:55:45 +04:00
debug("[recv] DELETE http://%v/v1/keys/%s", raftServer.Name(), key)
2013-06-09 22:12:32 +04:00
command := &DeleteCommand{}
command.Key = key
2013-06-09 22:12:32 +04:00
dispatch(command, w, req)
2013-06-09 22:12:32 +04:00
}
// Dispatch the command to leader
func dispatch(c Command, w *http.ResponseWriter, req *http.Request) {
2013-07-10 01:55:45 +04:00
if raftServer.State() == "leader" {
if body, err := raftServer.Do(c); err != nil {
warn("Commit failed %v", err)
(*w).WriteHeader(http.StatusInternalServerError)
return
} else {
(*w).WriteHeader(http.StatusOK)
if body == nil {
return
}
body, ok := body.([]byte)
if !ok {
panic("wrong type")
}
(*w).Write(body)
return
2013-06-13 22:01:06 +04:00
}
} else {
// current no leader
2013-07-10 01:55:45 +04:00
if raftServer.Leader() == "" {
(*w).WriteHeader(http.StatusInternalServerError)
return
}
// tell the client where is the leader
2013-06-28 22:26:17 +04:00
path := req.URL.Path
2013-06-28 22:29:11 +04:00
var scheme string
2013-06-28 22:29:11 +04:00
if scheme = req.URL.Scheme; scheme == "" {
scheme = "http://"
}
2013-06-28 22:29:11 +04:00
2013-07-10 01:55:45 +04:00
url := scheme + raftTransporter.GetLeaderClientAddress() + path
2013-06-28 22:29:11 +04:00
debug("Redirect to %s", url)
2013-07-06 21:21:39 +04:00
http.Redirect(*w, req, url, http.StatusTemporaryRedirect)
return
}
2013-06-21 02:59:23 +04:00
(*w).WriteHeader(http.StatusInternalServerError)
return
2013-06-21 02:59:23 +04:00
}
//--------------------------------------
2013-07-10 04:31:24 +04:00
// State non-sensitive handlers
// will not dispatch to leader
// TODO: add sensitive version for these
// command?
//--------------------------------------
// Handler to return the current leader name
func LeaderHttpHandler(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
2013-07-10 01:55:45 +04:00
w.Write([]byte(raftServer.Leader()))
}
// Get Handler
2013-07-01 22:16:30 +04:00
func GetHttpHandler(w *http.ResponseWriter, req *http.Request) {
key := req.URL.Path[len("/v1/keys/"):]
2013-06-20 08:03:28 +04:00
2013-07-10 01:55:45 +04:00
debug("[recv] GET http://%v/v1/keys/%s", raftServer.Name(), key)
command := &GetCommand{}
command.Key = key
2013-06-13 22:01:06 +04:00
2013-07-10 01:55:45 +04:00
if body, err := command.Apply(raftServer); err != nil {
warn("raftd: Unable to write file: %v", err)
2013-07-01 22:16:30 +04:00
(*w).WriteHeader(http.StatusInternalServerError)
return
} else {
2013-07-01 22:16:30 +04:00
(*w).WriteHeader(http.StatusOK)
body, ok := body.([]byte)
if !ok {
panic("wrong type")
}
2013-07-01 22:16:30 +04:00
(*w).Write(body)
return
}
2013-06-13 22:01:06 +04:00
}
2013-06-13 22:01:06 +04:00
// List Handler
2013-07-06 03:22:45 +04:00
func ListHttpHandler(w http.ResponseWriter, req *http.Request) {
prefix := req.URL.Path[len("/v1/list/"):]
2013-07-10 01:55:45 +04:00
debug("[recv] GET http://%v/v1/list/%s", raftServer.Name(), prefix)
2013-07-06 03:22:45 +04:00
command := &ListCommand{}
command.Prefix = prefix
2013-07-10 01:55:45 +04:00
if body, err := command.Apply(raftServer); err != nil {
2013-07-10 00:14:12 +04:00
warn("Unable to write file: %v", err)
2013-07-06 03:22:45 +04:00
w.WriteHeader(http.StatusInternalServerError)
return
} else {
w.WriteHeader(http.StatusOK)
body, ok := body.([]byte)
if !ok {
panic("wrong type")
}
w.Write(body)
return
}
}
// Watch handler
func WatchHttpHandler(w http.ResponseWriter, req *http.Request) {
2013-07-01 22:16:30 +04:00
key := req.URL.Path[len("/v1/watch/"):]
command := &WatchCommand{}
command.Key = key
2013-06-13 22:01:06 +04:00
2013-07-01 03:30:41 +04:00
if req.Method == "GET" {
2013-07-10 01:55:45 +04:00
debug("[recv] GET http://%v/watch/%s", raftServer.Name(), key)
2013-07-01 03:30:41 +04:00
command.SinceIndex = 0
} else if req.Method == "POST" {
// watch from a specific index
2013-07-10 01:55:45 +04:00
debug("[recv] POST http://%v/watch/%s", raftServer.Name(), key)
2013-07-07 02:54:21 +04:00
content := req.FormValue("index")
2013-07-01 03:30:41 +04:00
sinceIndex, err := strconv.ParseUint(string(content), 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
}
command.SinceIndex = sinceIndex
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
2013-07-10 01:55:45 +04:00
if body, err := command.Apply(raftServer); err != nil {
warn("Unable to do watch command: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
} else {
w.WriteHeader(http.StatusOK)
body, ok := body.([]byte)
if !ok {
panic("wrong type")
}
w.Write(body)
return
}
2013-06-13 22:01:06 +04:00
}
// Convert string duration to time format
2013-07-10 04:31:24 +04:00
func durationToExpireTime(strDuration string) (time.Time, error) {
if strDuration != "" {
duration, err := strconv.Atoi(strDuration)
if err != nil {
2013-07-10 04:31:24 +04:00
return time.Unix(0, 0), err
}
return time.Now().Add(time.Second * (time.Duration)(duration)), nil
} else {
return time.Unix(0, 0), nil
}
2013-07-10 04:31:24 +04:00
}