github.com/alpe/etcd@v0.1.2-0.20130915230056-09f31af88aeb/raft_handlers.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "net/http" 6 7 "github.com/coreos/go-raft" 8 ) 9 10 //------------------------------------------------------------- 11 // Handlers to handle raft related request via raft server port 12 //------------------------------------------------------------- 13 14 // Get all the current logs 15 func GetLogHttpHandler(w http.ResponseWriter, req *http.Request) { 16 debugf("[recv] GET %s/log", r.url) 17 w.Header().Set("Content-Type", "application/json") 18 w.WriteHeader(http.StatusOK) 19 json.NewEncoder(w).Encode(r.LogEntries()) 20 } 21 22 // Response to vote request 23 func VoteHttpHandler(w http.ResponseWriter, req *http.Request) { 24 rvreq := &raft.RequestVoteRequest{} 25 err := decodeJsonRequest(req, rvreq) 26 if err == nil { 27 debugf("[recv] POST %s/vote [%s]", r.url, rvreq.CandidateName) 28 if resp := r.RequestVote(rvreq); resp != nil { 29 w.WriteHeader(http.StatusOK) 30 json.NewEncoder(w).Encode(resp) 31 return 32 } 33 } 34 warnf("[vote] ERROR: %v", err) 35 w.WriteHeader(http.StatusInternalServerError) 36 } 37 38 // Response to append entries request 39 func AppendEntriesHttpHandler(w http.ResponseWriter, req *http.Request) { 40 aereq := &raft.AppendEntriesRequest{} 41 err := decodeJsonRequest(req, aereq) 42 43 if err == nil { 44 debugf("[recv] POST %s/log/append [%d]", r.url, len(aereq.Entries)) 45 46 r.serverStats.RecvAppendReq(aereq.LeaderName, int(req.ContentLength)) 47 48 if resp := r.AppendEntries(aereq); resp != nil { 49 w.WriteHeader(http.StatusOK) 50 json.NewEncoder(w).Encode(resp) 51 if !resp.Success { 52 debugf("[Append Entry] Step back") 53 } 54 return 55 } 56 } 57 warnf("[Append Entry] ERROR: %v", err) 58 w.WriteHeader(http.StatusInternalServerError) 59 } 60 61 // Response to recover from snapshot request 62 func SnapshotHttpHandler(w http.ResponseWriter, req *http.Request) { 63 aereq := &raft.SnapshotRequest{} 64 err := decodeJsonRequest(req, aereq) 65 if err == nil { 66 debugf("[recv] POST %s/snapshot/ ", r.url) 67 if resp := r.RequestSnapshot(aereq); resp != nil { 68 w.WriteHeader(http.StatusOK) 69 json.NewEncoder(w).Encode(resp) 70 return 71 } 72 } 73 warnf("[Snapshot] ERROR: %v", err) 74 w.WriteHeader(http.StatusInternalServerError) 75 } 76 77 // Response to recover from snapshot request 78 func SnapshotRecoveryHttpHandler(w http.ResponseWriter, req *http.Request) { 79 aereq := &raft.SnapshotRecoveryRequest{} 80 err := decodeJsonRequest(req, aereq) 81 if err == nil { 82 debugf("[recv] POST %s/snapshotRecovery/ ", r.url) 83 if resp := r.SnapshotRecoveryRequest(aereq); resp != nil { 84 w.WriteHeader(http.StatusOK) 85 json.NewEncoder(w).Encode(resp) 86 return 87 } 88 } 89 warnf("[Snapshot] ERROR: %v", err) 90 w.WriteHeader(http.StatusInternalServerError) 91 } 92 93 // Get the port that listening for etcd connecting of the server 94 func EtcdURLHttpHandler(w http.ResponseWriter, req *http.Request) { 95 debugf("[recv] Get %s/etcdURL/ ", r.url) 96 w.WriteHeader(http.StatusOK) 97 w.Write([]byte(argInfo.EtcdURL)) 98 } 99 100 // Response to the join request 101 func JoinHttpHandler(w http.ResponseWriter, req *http.Request) error { 102 103 command := &JoinCommand{} 104 105 if err := decodeJsonRequest(req, command); err == nil { 106 debugf("Receive Join Request from %s", command.Name) 107 return dispatch(command, w, req, false) 108 } else { 109 w.WriteHeader(http.StatusInternalServerError) 110 return nil 111 } 112 } 113 114 // Response to remove request 115 func RemoveHttpHandler(w http.ResponseWriter, req *http.Request) { 116 if req.Method != "DELETE" { 117 w.WriteHeader(http.StatusMethodNotAllowed) 118 return 119 } 120 121 nodeName := req.URL.Path[len("/remove/"):] 122 command := &RemoveCommand{ 123 Name: nodeName, 124 } 125 126 debugf("[recv] Remove Request [%s]", command.Name) 127 128 dispatch(command, w, req, false) 129 130 } 131 132 // Response to the name request 133 func NameHttpHandler(w http.ResponseWriter, req *http.Request) { 134 debugf("[recv] Get %s/name/ ", r.url) 135 w.WriteHeader(http.StatusOK) 136 w.Write([]byte(r.name)) 137 } 138 139 // Response to the name request 140 func RaftVersionHttpHandler(w http.ResponseWriter, req *http.Request) { 141 debugf("[recv] Get %s/version/ ", r.url) 142 w.WriteHeader(http.StatusOK) 143 w.Write([]byte(r.version)) 144 }