| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- package main
- import (
- "github.com/coreos/etcd/store"
- "net/http"
- "strconv"
- "time"
- )
- //-------------------------------------------------------------------
- // Handlers to handle etcd-store related request via raft client port
- //-------------------------------------------------------------------
- // Multiplex GET/POST/DELETE request to corresponding handlers
- func Multiplexer(w http.ResponseWriter, req *http.Request) {
- 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)
- return
- }
- }
- //--------------------------------------
- // State sensitive handlers
- // Set/Delete will dispatch to leader
- //--------------------------------------
- // Set Command Handler
- func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
- key := req.URL.Path[len("/v1/keys/"):]
- if store.CheckKeyword(key) {
- (*w).WriteHeader(http.StatusBadRequest)
- (*w).Write(newJsonError(400, "Set"))
- return
- }
- debugf("[recv] POST http://%v/v1/keys/%s", raftServer.Name(), key)
- value := req.FormValue("value")
- if len(value) == 0 {
- (*w).WriteHeader(http.StatusBadRequest)
- (*w).Write(newJsonError(200, "Set"))
- return
- }
- prevValue := req.FormValue("prevValue")
- strDuration := req.FormValue("ttl")
- expireTime, err := durationToExpireTime(strDuration)
- if err != nil {
- (*w).WriteHeader(http.StatusBadRequest)
- (*w).Write(newJsonError(202, "Set"))
- return
- }
- if len(prevValue) != 0 {
- command := &TestAndSetCommand{}
- command.Key = key
- command.Value = value
- command.PrevValue = prevValue
- command.ExpireTime = expireTime
- dispatch(command, w, req, true)
- } else {
- command := &SetCommand{}
- command.Key = key
- command.Value = value
- command.ExpireTime = expireTime
- dispatch(command, w, req, true)
- }
- }
- // Delete Handler
- func DeleteHttpHandler(w *http.ResponseWriter, req *http.Request) {
- key := req.URL.Path[len("/v1/keys/"):]
- debugf("[recv] DELETE http://%v/v1/keys/%s", raftServer.Name(), key)
- command := &DeleteCommand{}
- command.Key = key
- dispatch(command, w, req, true)
- }
- // Dispatch the command to leader
- func dispatch(c Command, w *http.ResponseWriter, req *http.Request, client bool) {
- if raftServer.State() == "leader" {
- if body, err := raftServer.Do(c); err != nil {
- if _, ok := err.(store.NotFoundError); ok {
- (*w).WriteHeader(http.StatusNotFound)
- (*w).Write(newJsonError(100, err.Error()))
- return
- }
- if _, ok := err.(store.TestFail); ok {
- (*w).WriteHeader(http.StatusBadRequest)
- (*w).Write(newJsonError(101, err.Error()))
- return
- }
- if _, ok := err.(store.NotFile); ok {
- (*w).WriteHeader(http.StatusBadRequest)
- (*w).Write(newJsonError(102, err.Error()))
- return
- }
- if err.Error() == errors[103] {
- (*w).WriteHeader(http.StatusBadRequest)
- (*w).Write(newJsonError(103, ""))
- return
- }
- (*w).WriteHeader(http.StatusInternalServerError)
- (*w).Write(newJsonError(300, err.Error()))
- return
- } else {
- if body == nil {
- (*w).WriteHeader(http.StatusNotFound)
- (*w).Write(newJsonError(300, "Empty result from raft"))
- } else {
- body, ok := body.([]byte)
- // this should not happen
- if !ok {
- panic("wrong type")
- }
- (*w).WriteHeader(http.StatusOK)
- (*w).Write(body)
- }
- return
- }
- } else {
- // current no leader
- if raftServer.Leader() == "" {
- (*w).WriteHeader(http.StatusInternalServerError)
- (*w).Write(newJsonError(300, ""))
- return
- }
- // tell the client where is the leader
- path := req.URL.Path
- var scheme string
- if scheme = req.URL.Scheme; scheme == "" {
- scheme = "http://"
- }
- var url string
- if client {
- clientAddr, _ := getClientAddr(raftServer.Leader())
- url = scheme + clientAddr + path
- } else {
- url = scheme + raftServer.Leader() + path
- }
- debugf("Redirect to %s", url)
- http.Redirect(*w, req, url, http.StatusTemporaryRedirect)
- return
- }
- (*w).WriteHeader(http.StatusInternalServerError)
- (*w).Write(newJsonError(300, ""))
- return
- }
- //--------------------------------------
- // 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) {
- leader := raftServer.Leader()
- if leader != "" {
- w.WriteHeader(http.StatusOK)
- w.Write([]byte(raftServer.Leader()))
- } else {
- // not likely, but it may happen
- w.WriteHeader(http.StatusInternalServerError)
- w.Write(newJsonError(301, ""))
- }
- }
- // Handler to return all the known machines in the current cluster
- func MachinesHttpHandler(w http.ResponseWriter, req *http.Request) {
- peers := raftServer.Peers()
- // Add itself to the machine list first
- // Since peer map does not contain the server itself
- machines, _ := getClientAddr(raftServer.Name())
- // Add all peers to the list and separate by comma
- // We do not use json here since we accept machines list
- // in the command line separate by comma.
- for peerName, _ := range peers {
- if addr, ok := getClientAddr(peerName); ok {
- machines = machines + "," + addr
- }
- }
- w.WriteHeader(http.StatusOK)
- w.Write([]byte(machines))
- }
- // Handler to return the current version of etcd
- func VersionHttpHandler(w http.ResponseWriter, req *http.Request) {
- w.WriteHeader(http.StatusOK)
- w.Write([]byte(releaseVersion))
- }
- // Handler to return the basic stats of etcd
- func StatsHttpHandler(w http.ResponseWriter, req *http.Request) {
- w.WriteHeader(http.StatusOK)
- w.Write(etcdStore.Stats())
- }
- // Get Handler
- func GetHttpHandler(w *http.ResponseWriter, req *http.Request) {
- key := req.URL.Path[len("/v1/keys/"):]
- debugf("[recv] GET http://%v/v1/keys/%s", raftServer.Name(), key)
- command := &GetCommand{}
- command.Key = key
- if body, err := command.Apply(raftServer); err != nil {
- if _, ok := err.(store.NotFoundError); ok {
- (*w).WriteHeader(http.StatusNotFound)
- (*w).Write(newJsonError(100, err.Error()))
- return
- }
- (*w).WriteHeader(http.StatusInternalServerError)
- (*w).Write(newJsonError(300, ""))
- } else {
- body, ok := body.([]byte)
- if !ok {
- panic("wrong type")
- }
- (*w).WriteHeader(http.StatusOK)
- (*w).Write(body)
- }
- }
- // Watch handler
- func WatchHttpHandler(w http.ResponseWriter, req *http.Request) {
- key := req.URL.Path[len("/v1/watch/"):]
- command := &WatchCommand{}
- command.Key = key
- if req.Method == "GET" {
- debugf("[recv] GET http://%v/watch/%s", raftServer.Name(), key)
- command.SinceIndex = 0
- } else if req.Method == "POST" {
- // watch from a specific index
- debugf("[recv] POST http://%v/watch/%s", raftServer.Name(), key)
- content := req.FormValue("index")
- sinceIndex, err := strconv.ParseUint(string(content), 10, 64)
- if err != nil {
- w.WriteHeader(http.StatusBadRequest)
- w.Write(newJsonError(203, "Watch From Index"))
- }
- command.SinceIndex = sinceIndex
- } else {
- w.WriteHeader(http.StatusMethodNotAllowed)
- return
- }
- if body, err := command.Apply(raftServer); err != nil {
- w.WriteHeader(http.StatusInternalServerError)
- w.Write(newJsonError(500, key))
- } else {
- w.WriteHeader(http.StatusOK)
- body, ok := body.([]byte)
- if !ok {
- panic("wrong type")
- }
- w.Write(body)
- }
- }
- // 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
- }
- }
|