httpapi.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "io/ioutil"
  17. "log"
  18. "net/http"
  19. "strconv"
  20. "github.com/coreos/etcd/raft/raftpb"
  21. )
  22. // Handler for a http based key-value store backed by raft
  23. type httpKVAPI struct {
  24. store *kvstore
  25. confChangeC chan<- raftpb.ConfChange
  26. }
  27. func (h *httpKVAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  28. key := r.RequestURI
  29. switch {
  30. case r.Method == "PUT":
  31. v, err := ioutil.ReadAll(r.Body)
  32. if err != nil {
  33. log.Printf("Failed to read on PUT (%v)\n", err)
  34. http.Error(w, "Failed on PUT", http.StatusBadRequest)
  35. return
  36. }
  37. h.store.Propose(key, string(v))
  38. // Optimistic-- no waiting for ack from raft. Value is not yet
  39. // committed so a subsequent GET on the key may return old value
  40. w.WriteHeader(http.StatusNoContent)
  41. case r.Method == "GET":
  42. if v, ok := h.store.Lookup(key); ok {
  43. w.Write([]byte(v))
  44. } else {
  45. http.Error(w, "Failed to GET", http.StatusNotFound)
  46. }
  47. case r.Method == "POST":
  48. url, err := ioutil.ReadAll(r.Body)
  49. if err != nil {
  50. log.Printf("Failed to read on POST (%v)\n", err)
  51. http.Error(w, "Failed on POST", http.StatusBadRequest)
  52. return
  53. }
  54. nodeId, err := strconv.ParseUint(key[1:], 0, 64)
  55. if err != nil {
  56. log.Printf("Failed to convert ID for conf change (%v)\n", err)
  57. http.Error(w, "Failed on POST", http.StatusBadRequest)
  58. return
  59. }
  60. cc := raftpb.ConfChange{
  61. Type: raftpb.ConfChangeAddNode,
  62. NodeID: nodeId,
  63. Context: url,
  64. }
  65. h.confChangeC <- cc
  66. // As above, optimistic that raft will apply the conf change
  67. w.WriteHeader(http.StatusNoContent)
  68. case r.Method == "DELETE":
  69. nodeId, err := strconv.ParseUint(key[1:], 0, 64)
  70. if err != nil {
  71. log.Printf("Failed to convert ID for conf change (%v)\n", err)
  72. http.Error(w, "Failed on DELETE", http.StatusBadRequest)
  73. return
  74. }
  75. cc := raftpb.ConfChange{
  76. Type: raftpb.ConfChangeRemoveNode,
  77. NodeID: nodeId,
  78. }
  79. h.confChangeC <- cc
  80. // As above, optimistic that raft will apply the conf change
  81. w.WriteHeader(http.StatusNoContent)
  82. default:
  83. w.Header().Set("Allow", "PUT")
  84. w.Header().Add("Allow", "GET")
  85. w.Header().Add("Allow", "POST")
  86. w.Header().Add("Allow", "DELETE")
  87. http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
  88. }
  89. }
  90. // serveHttpKVAPI starts a key-value server with a GET/PUT API and listens.
  91. func serveHttpKVAPI(kv *kvstore, port int, confChangeC chan<- raftpb.ConfChange, errorC <-chan error) {
  92. srv := http.Server{
  93. Addr: ":" + strconv.Itoa(port),
  94. Handler: &httpKVAPI{
  95. store: kv,
  96. confChangeC: confChangeC,
  97. },
  98. }
  99. go func() {
  100. if err := srv.ListenAndServe(); err != nil {
  101. log.Fatal(err)
  102. }
  103. }()
  104. // exit when raft goes down
  105. if err, ok := <-errorC; ok {
  106. log.Fatal(err)
  107. }
  108. }