httpapi.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "go.etcd.io/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. defer r.Body.Close()
  30. switch {
  31. case r.Method == "PUT":
  32. v, err := ioutil.ReadAll(r.Body)
  33. if err != nil {
  34. log.Printf("Failed to read on PUT (%v)\n", err)
  35. http.Error(w, "Failed on PUT", http.StatusBadRequest)
  36. return
  37. }
  38. h.store.Propose(key, string(v))
  39. // Optimistic-- no waiting for ack from raft. Value is not yet
  40. // committed so a subsequent GET on the key may return old value
  41. w.WriteHeader(http.StatusNoContent)
  42. case r.Method == "GET":
  43. if v, ok := h.store.Lookup(key); ok {
  44. w.Write([]byte(v))
  45. } else {
  46. http.Error(w, "Failed to GET", http.StatusNotFound)
  47. }
  48. case r.Method == "POST":
  49. url, err := ioutil.ReadAll(r.Body)
  50. if err != nil {
  51. log.Printf("Failed to read on POST (%v)\n", err)
  52. http.Error(w, "Failed on POST", http.StatusBadRequest)
  53. return
  54. }
  55. nodeId, err := strconv.ParseUint(key[1:], 0, 64)
  56. if err != nil {
  57. log.Printf("Failed to convert ID for conf change (%v)\n", err)
  58. http.Error(w, "Failed on POST", http.StatusBadRequest)
  59. return
  60. }
  61. cc := raftpb.ConfChange{
  62. Type: raftpb.ConfChangeAddNode,
  63. NodeID: nodeId,
  64. Context: url,
  65. }
  66. h.confChangeC <- cc
  67. // As above, optimistic that raft will apply the conf change
  68. w.WriteHeader(http.StatusNoContent)
  69. case r.Method == "DELETE":
  70. nodeId, err := strconv.ParseUint(key[1:], 0, 64)
  71. if err != nil {
  72. log.Printf("Failed to convert ID for conf change (%v)\n", err)
  73. http.Error(w, "Failed on DELETE", http.StatusBadRequest)
  74. return
  75. }
  76. cc := raftpb.ConfChange{
  77. Type: raftpb.ConfChangeRemoveNode,
  78. NodeID: nodeId,
  79. }
  80. h.confChangeC <- cc
  81. // As above, optimistic that raft will apply the conf change
  82. w.WriteHeader(http.StatusNoContent)
  83. default:
  84. w.Header().Set("Allow", "PUT")
  85. w.Header().Add("Allow", "GET")
  86. w.Header().Add("Allow", "POST")
  87. w.Header().Add("Allow", "DELETE")
  88. http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
  89. }
  90. }
  91. // serveHttpKVAPI starts a key-value server with a GET/PUT API and listens.
  92. func serveHttpKVAPI(kv *kvstore, port int, confChangeC chan<- raftpb.ConfChange, errorC <-chan error) {
  93. srv := http.Server{
  94. Addr: ":" + strconv.Itoa(port),
  95. Handler: &httpKVAPI{
  96. store: kv,
  97. confChangeC: confChangeC,
  98. },
  99. }
  100. go func() {
  101. if err := srv.ListenAndServe(); err != nil {
  102. log.Fatal(err)
  103. }
  104. }()
  105. // exit when raft goes down
  106. if err, ok := <-errorC; ok {
  107. log.Fatal(err)
  108. }
  109. }