refactor return http status 201 Created when creating a new node
parent
97987e72a7
commit
acbc0c8846
|
@ -9,7 +9,6 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
etcdErr "github.com/coreos/etcd/error"
|
etcdErr "github.com/coreos/etcd/error"
|
||||||
|
@ -344,53 +343,3 @@ func (s *PeerServer) monitorSnapshot() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PeerServer) dispatch(c raft.Command, w http.ResponseWriter, req *http.Request) error {
|
|
||||||
if s.raftServer.State() == raft.Leader {
|
|
||||||
result, err := s.raftServer.Do(c)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if result == nil {
|
|
||||||
return etcdErr.NewError(300, "Empty result from raft", store.UndefIndex, store.UndefTerm)
|
|
||||||
}
|
|
||||||
|
|
||||||
// response for raft related commands[join/remove]
|
|
||||||
if b, ok := result.([]byte); ok {
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write(b)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var b []byte
|
|
||||||
if strings.HasPrefix(req.URL.Path, "/v1") {
|
|
||||||
b, _ = json.Marshal(result.(*store.Event).Response())
|
|
||||||
} else {
|
|
||||||
b, _ = json.Marshal(result.(*store.Event))
|
|
||||||
}
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write(b)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
|
|
||||||
} else {
|
|
||||||
leader := s.raftServer.Leader()
|
|
||||||
|
|
||||||
// No leader available.
|
|
||||||
if leader == "" {
|
|
||||||
return etcdErr.NewError(300, "", store.UndefIndex, store.UndefTerm)
|
|
||||||
}
|
|
||||||
|
|
||||||
var url string
|
|
||||||
switch c.(type) {
|
|
||||||
case *JoinCommand, *RemoveCommand:
|
|
||||||
url, _ = s.registry.PeerURL(leader)
|
|
||||||
default:
|
|
||||||
url, _ = s.registry.ClientURL(leader)
|
|
||||||
}
|
|
||||||
redirect(url, w, req)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -10,20 +10,20 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Get all the current logs
|
// Get all the current logs
|
||||||
func (s *PeerServer) GetLogHttpHandler(w http.ResponseWriter, req *http.Request) {
|
func (ps *PeerServer) GetLogHttpHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
log.Debugf("[recv] GET %s/log", s.url)
|
log.Debugf("[recv] GET %s/log", ps.url)
|
||||||
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(s.raftServer.LogEntries())
|
json.NewEncoder(w).Encode(ps.raftServer.LogEntries())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response to vote request
|
// Response to vote request
|
||||||
func (s *PeerServer) VoteHttpHandler(w http.ResponseWriter, req *http.Request) {
|
func (ps *PeerServer) 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 {
|
||||||
log.Debugf("[recv] POST %s/vote [%s]", s.url, rvreq.CandidateName)
|
log.Debugf("[recv] POST %s/vote [%s]", ps.url, rvreq.CandidateName)
|
||||||
if resp := s.raftServer.RequestVote(rvreq); resp != nil {
|
if resp := ps.raftServer.RequestVote(rvreq); resp != nil {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode(resp)
|
json.NewEncoder(w).Encode(resp)
|
||||||
return
|
return
|
||||||
|
@ -34,16 +34,16 @@ func (s *PeerServer) VoteHttpHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response to append entries request
|
// Response to append entries request
|
||||||
func (s *PeerServer) AppendEntriesHttpHandler(w http.ResponseWriter, req *http.Request) {
|
func (ps *PeerServer) AppendEntriesHttpHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
aereq := &raft.AppendEntriesRequest{}
|
aereq := &raft.AppendEntriesRequest{}
|
||||||
err := decodeJsonRequest(req, aereq)
|
err := decodeJsonRequest(req, aereq)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Debugf("[recv] POST %s/log/append [%d]", s.url, len(aereq.Entries))
|
log.Debugf("[recv] POST %s/log/append [%d]", ps.url, len(aereq.Entries))
|
||||||
|
|
||||||
s.serverStats.RecvAppendReq(aereq.LeaderName, int(req.ContentLength))
|
ps.serverStats.RecvAppendReq(aereq.LeaderName, int(req.ContentLength))
|
||||||
|
|
||||||
if resp := s.raftServer.AppendEntries(aereq); resp != nil {
|
if resp := ps.raftServer.AppendEntries(aereq); resp != nil {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode(resp)
|
json.NewEncoder(w).Encode(resp)
|
||||||
if !resp.Success {
|
if !resp.Success {
|
||||||
|
@ -57,12 +57,12 @@ func (s *PeerServer) AppendEntriesHttpHandler(w http.ResponseWriter, req *http.R
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response to recover from snapshot request
|
// Response to recover from snapshot request
|
||||||
func (s *PeerServer) SnapshotHttpHandler(w http.ResponseWriter, req *http.Request) {
|
func (ps *PeerServer) 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 {
|
||||||
log.Debugf("[recv] POST %s/snapshot/ ", s.url)
|
log.Debugf("[recv] POST %s/snapshot/ ", ps.url)
|
||||||
if resp := s.raftServer.RequestSnapshot(aereq); resp != nil {
|
if resp := ps.raftServer.RequestSnapshot(aereq); resp != nil {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode(resp)
|
json.NewEncoder(w).Encode(resp)
|
||||||
return
|
return
|
||||||
|
@ -73,12 +73,12 @@ func (s *PeerServer) SnapshotHttpHandler(w http.ResponseWriter, req *http.Reques
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response to recover from snapshot request
|
// Response to recover from snapshot request
|
||||||
func (s *PeerServer) SnapshotRecoveryHttpHandler(w http.ResponseWriter, req *http.Request) {
|
func (ps *PeerServer) 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 {
|
||||||
log.Debugf("[recv] POST %s/snapshotRecovery/ ", s.url)
|
log.Debugf("[recv] POST %s/snapshotRecovery/ ", ps.url)
|
||||||
if resp := s.raftServer.SnapshotRecoveryRequest(aereq); resp != nil {
|
if resp := ps.raftServer.SnapshotRecoveryRequest(aereq); resp != nil {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode(resp)
|
json.NewEncoder(w).Encode(resp)
|
||||||
return
|
return
|
||||||
|
@ -89,20 +89,20 @@ func (s *PeerServer) SnapshotRecoveryHttpHandler(w http.ResponseWriter, req *htt
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the port that listening for etcd connecting of the server
|
// Get the port that listening for etcd connecting of the server
|
||||||
func (s *PeerServer) EtcdURLHttpHandler(w http.ResponseWriter, req *http.Request) {
|
func (ps *PeerServer) EtcdURLHttpHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
log.Debugf("[recv] Get %s/etcdURL/ ", s.url)
|
log.Debugf("[recv] Get %s/etcdURL/ ", ps.url)
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte(s.server.URL()))
|
w.Write([]byte(ps.server.URL()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response to the join request
|
// Response to the join request
|
||||||
func (s *PeerServer) JoinHttpHandler(w http.ResponseWriter, req *http.Request) {
|
func (ps *PeerServer) JoinHttpHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
command := &JoinCommand{}
|
command := &JoinCommand{}
|
||||||
|
|
||||||
// Write CORS header.
|
// Write CORS header.
|
||||||
if s.server.OriginAllowed("*") {
|
if ps.server.OriginAllowed("*") {
|
||||||
w.Header().Add("Access-Control-Allow-Origin", "*")
|
w.Header().Add("Access-Control-Allow-Origin", "*")
|
||||||
} else if s.server.OriginAllowed(req.Header.Get("Origin")) {
|
} else if ps.server.OriginAllowed(req.Header.Get("Origin")) {
|
||||||
w.Header().Add("Access-Control-Allow-Origin", req.Header.Get("Origin"))
|
w.Header().Add("Access-Control-Allow-Origin", req.Header.Get("Origin"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,7 +113,7 @@ func (s *PeerServer) JoinHttpHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("Receive Join Request from %s", command.Name)
|
log.Debugf("Receive Join Request from %s", command.Name)
|
||||||
err = s.dispatch(command, w, req)
|
err = ps.server.Dispatch(command, w, req)
|
||||||
|
|
||||||
// Return status.
|
// Return status.
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -127,7 +127,7 @@ func (s *PeerServer) JoinHttpHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response to remove request
|
// Response to remove request
|
||||||
func (s *PeerServer) RemoveHttpHandler(w http.ResponseWriter, req *http.Request) {
|
func (ps *PeerServer) RemoveHttpHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
if req.Method != "DELETE" {
|
if req.Method != "DELETE" {
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
|
@ -140,19 +140,19 @@ func (s *PeerServer) RemoveHttpHandler(w http.ResponseWriter, req *http.Request)
|
||||||
|
|
||||||
log.Debugf("[recv] Remove Request [%s]", command.Name)
|
log.Debugf("[recv] Remove Request [%s]", command.Name)
|
||||||
|
|
||||||
s.dispatch(command, w, req)
|
ps.server.Dispatch(command, w, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response to the name request
|
// Response to the name request
|
||||||
func (s *PeerServer) NameHttpHandler(w http.ResponseWriter, req *http.Request) {
|
func (ps *PeerServer) NameHttpHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
log.Debugf("[recv] Get %s/name/ ", s.url)
|
log.Debugf("[recv] Get %s/name/ ", ps.url)
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte(s.name))
|
w.Write([]byte(ps.name))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response to the name request
|
// Response to the name request
|
||||||
func (s *PeerServer) RaftVersionHttpHandler(w http.ResponseWriter, req *http.Request) {
|
func (ps *PeerServer) RaftVersionHttpHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
log.Debugf("[recv] Get %s/version/ ", s.url)
|
log.Debugf("[recv] Get %s/version/ ", ps.url)
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte(PeerVersion))
|
w.Write([]byte(PeerVersion))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -167,7 +168,57 @@ func (s *Server) ListenAndServe() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Dispatch(c raft.Command, w http.ResponseWriter, req *http.Request) error {
|
func (s *Server) Dispatch(c raft.Command, w http.ResponseWriter, req *http.Request) error {
|
||||||
return s.peerServer.dispatch(c, w, req)
|
ps := s.peerServer
|
||||||
|
if ps.raftServer.State() == raft.Leader {
|
||||||
|
result, err := ps.raftServer.Do(c)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if result == nil {
|
||||||
|
return etcdErr.NewError(300, "Empty result from raft", store.UndefIndex, store.UndefTerm)
|
||||||
|
}
|
||||||
|
|
||||||
|
// response for raft related commands[join/remove]
|
||||||
|
if b, ok := result.([]byte); ok {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write(b)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var b []byte
|
||||||
|
if strings.HasPrefix(req.URL.Path, "/v1") {
|
||||||
|
b, _ = json.Marshal(result.(*store.Event).Response())
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
} else {
|
||||||
|
e, _ := result.(*store.Event)
|
||||||
|
b, _ = json.Marshal(e)
|
||||||
|
w.WriteHeader(e.HttpStatusCode())
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Write(b)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
|
||||||
|
} else {
|
||||||
|
leader := ps.raftServer.Leader()
|
||||||
|
|
||||||
|
// No leader available.
|
||||||
|
if leader == "" {
|
||||||
|
return etcdErr.NewError(300, "", store.UndefIndex, store.UndefTerm)
|
||||||
|
}
|
||||||
|
|
||||||
|
var url string
|
||||||
|
switch c.(type) {
|
||||||
|
case *JoinCommand, *RemoveCommand:
|
||||||
|
url, _ = ps.registry.PeerURL(leader)
|
||||||
|
default:
|
||||||
|
url, _ = ps.registry.ClientURL(leader)
|
||||||
|
}
|
||||||
|
redirect(url, w, req)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sets a comma-delimited list of origins that are allowed.
|
// Sets a comma-delimited list of origins that are allowed.
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package store
|
package store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -42,6 +43,18 @@ func newEvent(action string, key string, index uint64, term uint64) *Event {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *Event) HttpStatusCode() int {
|
||||||
|
if e.Action == Create {
|
||||||
|
return http.StatusCreated
|
||||||
|
}
|
||||||
|
|
||||||
|
if e.Action == Set && e.PrevValue == "" {
|
||||||
|
return http.StatusCreated
|
||||||
|
}
|
||||||
|
|
||||||
|
return http.StatusOK
|
||||||
|
}
|
||||||
|
|
||||||
// Converts an event object into a response object.
|
// Converts an event object into a response object.
|
||||||
func (event *Event) Response() interface{} {
|
func (event *Event) Response() interface{} {
|
||||||
if !event.Dir {
|
if !event.Dir {
|
||||||
|
|
Loading…
Reference in New Issue