-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpapi.go
128 lines (111 loc) · 3.45 KB
/
httpapi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package quorum
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/pprof"
"github.com/julienschmidt/httprouter"
)
type HTTPApi struct {
quorumNode *QuorumNode
}
func NewHTTPApi(quorumNode *QuorumNode) *HTTPApi {
return &HTTPApi{quorumNode: quorumNode}
}
func wrapHandler(h http.Handler) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
h.ServeHTTP(w, r)
}
}
// Register any endpoints to the router
func (h *HTTPApi) Start(router *httprouter.Router) {
router.POST("/v1/join", h.Join)
router.POST("/v1/leave", h.Leave)
// TODO: options to enable/disable (or scope to just localhost)
router.GET("/v1/debug/pprof/", wrapHandler(http.HandlerFunc(pprof.Index)))
router.GET("/v1/debug/pprof/cmdline", wrapHandler(http.HandlerFunc(pprof.Cmdline)))
router.GET("/v1/debug/pprof/profile", wrapHandler(http.HandlerFunc(pprof.Profile)))
router.GET("/v1/debug/pprof/symbol", wrapHandler(http.HandlerFunc(pprof.Symbol)))
router.GET("/v1/debug/pprof/trace", wrapHandler(http.HandlerFunc(pprof.Trace)))
}
type JoinRequest struct {
ServerID string `json:"server_id"`
ServerAddress string `json:"server_address"`
}
type JoinResponse struct {
OK bool `json:"ok"`
Leader string `json:"leader"`
}
type LeaveRequest struct {
ServerID string `json:"server_id"`
}
type LeaveResponse struct {
OK bool `json:"ok"`
}
func (h *HTTPApi) Join(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
defer r.Body.Close()
bytes, _ := ioutil.ReadAll(r.Body)
var joinRequest JoinRequest
if err := json.Unmarshal(bytes, &joinRequest); err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
} else {
if joinRequest.ServerID == "" || joinRequest.ServerAddress == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Expecting a payload like {'server_id' : 'server_id_here', 'server_address' : 'server_address_here'}"))
return
}
ok := false
leader := ""
if err := h.quorumNode.Join(joinRequest.ServerID, joinRequest.ServerAddress); err != nil {
if qleader, err := h.quorumNode.GetLeader(); err == nil {
leader = qleader
} else {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
} else {
ok = true
}
if returnBytes, err := json.Marshal(JoinResponse{OK: ok, Leader: leader}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
} else {
w.Header().Set("Content-Type", "application/json")
w.Write(returnBytes)
}
}
}
func (h *HTTPApi) Leave(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
defer r.Body.Close()
bytes, _ := ioutil.ReadAll(r.Body)
var leaveRequest LeaveRequest
if err := json.Unmarshal(bytes, &leaveRequest); err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
} else {
if leaveRequest.ServerID == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Expecting a payload like {'server_id' : 'server_id_here'}"))
return
}
ok := false
if err := h.quorumNode.Remove(leaveRequest.ServerID); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
} else {
ok = true
}
if returnBytes, err := json.Marshal(LeaveResponse{OK: ok}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
} else {
w.Header().Set("Content-Type", "application/json")
w.Write(returnBytes)
}
}
}