mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Refactor v2 API into server/v2.
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
package v1
|
||||
|
||||
func deleteKeyHandler(w http.ResponseWriter, req *http.Request, e *etcdServer) error {
|
||||
key := req.URL.Path[len("/v1/keys/"):]
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/coreos/etcd/store"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
debugf("[recv] DELETE %v/v1/keys/%s [%s]", e.url, key, req.RemoteAddr)
|
||||
|
||||
command := &DeleteCommand{
|
||||
Key: key,
|
||||
}
|
||||
|
||||
return dispatchEtcdCommandV1(command, w, req)
|
||||
// Removes a key from the store.
|
||||
func DeleteKeyHandler(w http.ResponseWriter, req *http.Request, s Server) error {
|
||||
vars := mux.Vars(req)
|
||||
key := "/" + vars["key"]
|
||||
command := &DeleteCommand{Key: key}
|
||||
return s.Dispatch(command, w, req)
|
||||
}
|
||||
|
||||
@@ -1,40 +1,40 @@
|
||||
package v1
|
||||
|
||||
// Dispatch the command to leader.
|
||||
func dispatchCommand(c Command, w http.ResponseWriter, req *http.Request) error {
|
||||
return dispatch(c, w, req, nameToEtcdURL)
|
||||
func dispatchCommand(c Command, w http.ResponseWriter, req *http.Request, s *server.Server) error {
|
||||
return dispatch(c, w, req, s, nameToEtcdURL)
|
||||
}
|
||||
|
||||
// Dispatches a command to a given URL.
|
||||
func dispatch(c Command, w http.ResponseWriter, req *http.Request, toURL func(name string) (string, bool)) error {
|
||||
r := e.raftServer
|
||||
func dispatch(c Command, w http.ResponseWriter, req *http.Request, s *server.Server, toURL func(name string) (string, bool)) error {
|
||||
r := s.raftServer
|
||||
if r.State() == raft.Leader {
|
||||
if event, err := r.Do(c); err != nil {
|
||||
event, err := r.Do(c)
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
if event == nil {
|
||||
return etcdErr.NewError(300, "Empty result from raft", store.UndefIndex, store.UndefTerm)
|
||||
}
|
||||
|
||||
event, _ := event.(*store.Event)
|
||||
|
||||
response := eventToResponse(event)
|
||||
bytes, _ := json.Marshal(response)
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(bytes)
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
if event == nil {
|
||||
return etcdErr.NewError(300, "Empty result from raft", store.UndefIndex, store.UndefTerm)
|
||||
}
|
||||
|
||||
event, _ := event.(*store.Event)
|
||||
response := eventToResponse(event)
|
||||
b, _ := json.Marshal(response)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(b)
|
||||
|
||||
return nil
|
||||
|
||||
} else {
|
||||
leader := r.Leader()
|
||||
// current no leader
|
||||
|
||||
// No leader available.
|
||||
if leader == "" {
|
||||
return etcdErr.NewError(300, "", store.UndefIndex, store.UndefTerm)
|
||||
}
|
||||
url, _ := toURL(leader)
|
||||
|
||||
url, _ := toURL(leader)
|
||||
redirect(url, w, req)
|
||||
|
||||
return nil
|
||||
|
||||
@@ -7,15 +7,12 @@ import (
|
||||
)
|
||||
|
||||
// Retrieves the value for a given key.
|
||||
func getKeyHandler(w http.ResponseWriter, req *http.Request, e *etcdServer) error {
|
||||
func GetKeyHandler(w http.ResponseWriter, req *http.Request, e *etcdServer) error {
|
||||
vars := mux.Vars(req)
|
||||
key := "/" + vars["key"]
|
||||
|
||||
debugf("[recv] GET %s/v1/keys/%s [%s]", e.url, key, req.RemoteAddr)
|
||||
|
||||
// Execute the command.
|
||||
command := &GetCommand{Key: key}
|
||||
event, err := command.Apply(e.raftServer.Server)
|
||||
// Retrieve the key from the store.
|
||||
event, err := s.Store().Get(key, false, false, s.CommitIndex(), s.Term())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"github.com/coreos/etcd/server"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// Installs the routes for version 1 of the API on to a server.
|
||||
func Install(s *server.Server) {
|
||||
s.HandleFunc("/v1/keys/{key:.*}", getKeyHandler).Methods("GET")
|
||||
s.HandleFunc("/v1/keys/{key:.*}", setKeyHandler).Methods("POST", "PUT")
|
||||
s.HandleFunc("/v1/keys/{key:.*}", deleteKeyHandler).Methods("DELETE")
|
||||
|
||||
s.HandleFunc("/v1/watch/{key:.*}", watchKeyHandler).Methods("GET", "POST")
|
||||
}
|
||||
@@ -7,12 +7,10 @@ import (
|
||||
)
|
||||
|
||||
// Sets the value for a given key.
|
||||
func setKeyHandler(w http.ResponseWriter, req *http.Request, e *etcdServer) error {
|
||||
func SetKeyHandler(w http.ResponseWriter, req *http.Request, s Server) error {
|
||||
vars := mux.Vars(req)
|
||||
key := "/" + vars["key"]
|
||||
|
||||
debugf("[recv] POST %v/v1/keys/%s [%s]", e.url, key, req.RemoteAddr)
|
||||
|
||||
req.ParseForm()
|
||||
|
||||
// Parse non-blank value.
|
||||
@@ -46,5 +44,5 @@ func setKeyHandler(w http.ResponseWriter, req *http.Request, e *etcdServer) erro
|
||||
}
|
||||
}
|
||||
|
||||
return dispatchEtcdCommand(command, w, req)
|
||||
return s.Dispatch(command, w, req)
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
package v1
|
||||
|
||||
// Converts an event object into a response object.
|
||||
func eventToResponse(event *store.Event) interface{} {
|
||||
if !event.Dir {
|
||||
response := &store.Response{
|
||||
Action: event.Action,
|
||||
Key: event.Key,
|
||||
Value: event.Value,
|
||||
PrevValue: event.PrevValue,
|
||||
Index: event.Index,
|
||||
TTL: event.TTL,
|
||||
Expiration: event.Expiration,
|
||||
}
|
||||
|
||||
if response.Action == store.Create || response.Action == store.Update {
|
||||
response.Action = "set"
|
||||
if response.PrevValue == "" {
|
||||
response.NewKey = true
|
||||
}
|
||||
}
|
||||
|
||||
return response
|
||||
} else {
|
||||
responses := make([]*store.Response, len(event.KVPairs))
|
||||
|
||||
for i, kv := range event.KVPairs {
|
||||
responses[i] = &store.Response{
|
||||
Action: event.Action,
|
||||
Key: kv.Key,
|
||||
Value: kv.Value,
|
||||
Dir: kv.Dir,
|
||||
Index: event.Index,
|
||||
}
|
||||
}
|
||||
return responses
|
||||
}
|
||||
}
|
||||
50
server/v1/v1.go
Normal file
50
server/v1/v1.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"github.com/coreos/etcd/server"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// The Server interface provides all the methods required for the v1 API.
|
||||
type Server interface {
|
||||
CommitIndex() uint64
|
||||
Term() uint64
|
||||
Dispatch(http.ResponseWriter, *http.Request, Command)
|
||||
}
|
||||
|
||||
// Converts an event object into a response object.
|
||||
func eventToResponse(event *store.Event) interface{} {
|
||||
if !event.Dir {
|
||||
response := &store.Response{
|
||||
Action: event.Action,
|
||||
Key: event.Key,
|
||||
Value: event.Value,
|
||||
PrevValue: event.PrevValue,
|
||||
Index: event.Index,
|
||||
TTL: event.TTL,
|
||||
Expiration: event.Expiration,
|
||||
}
|
||||
|
||||
if response.Action == store.Create || response.Action == store.Update {
|
||||
response.Action = "set"
|
||||
if response.PrevValue == "" {
|
||||
response.NewKey = true
|
||||
}
|
||||
}
|
||||
|
||||
return response
|
||||
} else {
|
||||
responses := make([]*store.Response, len(event.KVPairs))
|
||||
|
||||
for i, kv := range event.KVPairs {
|
||||
responses[i] = &store.Response{
|
||||
Action: event.Action,
|
||||
Key: kv.Key,
|
||||
Value: kv.Value,
|
||||
Dir: kv.Dir,
|
||||
Index: event.Index,
|
||||
}
|
||||
}
|
||||
return responses
|
||||
}
|
||||
}
|
||||
@@ -7,27 +7,26 @@ import (
|
||||
)
|
||||
|
||||
// Watches a given key prefix for changes.
|
||||
func watchKeyHandler(w http.ResponseWriter, req *http.Request, e *etcdServer) error {
|
||||
func WatchKeyHandler(w http.ResponseWriter, req *http.Request, s Server) error {
|
||||
var err error
|
||||
vars := mux.Vars(req)
|
||||
key := "/" + vars["key"]
|
||||
|
||||
debugf("[recv] %s %s/watch/%s [%s]", req.Method, e.url, key, req.RemoteAddr)
|
||||
|
||||
// Create a command to watch from a given index (default 0).
|
||||
command := &WatchCommand{Key: key}
|
||||
sinceIndex := 0
|
||||
if req.Method == "POST" {
|
||||
sinceIndex, err := strconv.ParseUint(string(req.FormValue("index")), 10, 64)
|
||||
sinceIndex, err = strconv.ParseUint(string(req.FormValue("index")), 10, 64)
|
||||
if err != nil {
|
||||
return etcdErr.NewError(203, "Watch From Index", store.UndefIndex, store.UndefTerm)
|
||||
}
|
||||
command.SinceIndex = sinceIndex
|
||||
}
|
||||
|
||||
// Apply the command and write the response.
|
||||
event, err := command.Apply(e.raftServer.Server)
|
||||
// Start the watcher on the store.
|
||||
c, err := s.Store().Watch(key, false, sinceIndex, s.CommitIndex(), s.Term())
|
||||
if err != nil {
|
||||
return etcdErr.NewError(500, key, store.UndefIndex, store.UndefTerm)
|
||||
}
|
||||
event := <-c
|
||||
|
||||
event, _ := event.(*store.Event)
|
||||
response := eventToResponse(event)
|
||||
|
||||
Reference in New Issue
Block a user