This repository has been archived by the owner on Dec 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttpapi.go
178 lines (153 loc) · 4.08 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strconv"
"sync"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"go.etcd.io/etcd/raft/raftpb"
)
type httpChatAPI struct {
store *chatStore
confChangeC chan<- raftpb.ConfChange
wsCommitsC <-chan roomPost
mu sync.RWMutex
wsConns []*websocket.Conn
}
func checkOrigin(r *http.Request) bool {
return true
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: checkOrigin,
}
func (h *httpChatAPI) WebsocketHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("Failed to upgrade (%v)\n", err)
http.Error(w, "Failed on upgrade", http.StatusBadRequest)
return
}
h.mu.Lock()
h.wsConns = append(h.wsConns, conn)
h.mu.Unlock()
}
func (h *httpChatAPI) readWSCommits() {
for rp := range h.wsCommitsC {
h.mu.RLock()
for _, conn := range h.wsConns {
conn.WriteJSON(rp)
}
h.mu.RUnlock()
}
}
func (h *httpChatAPI) RoomsHandler(w http.ResponseWriter, r *http.Request) {
log.Println("servings list of rooms")
rooms := h.store.Rooms()
enc := json.NewEncoder(w)
enc.Encode(rooms)
}
func (h *httpChatAPI) RoomMessagesHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
roomName := vars["roomID"]
if r.Method == "GET" {
log.Println("servings list of messages in ", roomName)
room, roomFound := h.store.Lookup(roomName)
if roomFound {
enc := json.NewEncoder(w)
enc.Encode(room)
} else {
enc := json.NewEncoder(w)
enc.Encode([]string{})
}
} else {
log.Println("adding message in ", roomName)
var post Post
enc := json.NewDecoder(r.Body)
err := enc.Decode(&post)
if err != nil {
log.Printf("Failed to read on POST (%v)\n", err)
http.Error(w, "Failed on POST", http.StatusBadRequest)
return
}
h.store.Propose(roomName, post)
w.WriteHeader(http.StatusCreated)
}
}
func (h *httpChatAPI) DefaultHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
nodeName := vars["nodeID"]
switch {
case r.Method == "POST":
url, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("Failed to read on POST (%v)\n", err)
http.Error(w, "Failed on POST", http.StatusBadRequest)
return
}
nodeID, err := strconv.ParseUint(nodeName, 0, 64)
if err != nil {
log.Printf("Failed to convert ID for conf change (%v)\n", err)
http.Error(w, "Failed on POST", http.StatusBadRequest)
return
}
cc := raftpb.ConfChange{
Type: raftpb.ConfChangeAddNode,
NodeID: nodeID,
Context: url,
}
h.confChangeC <- cc
// As above, optimistic that raft will apply the conf change
w.WriteHeader(http.StatusNoContent)
case r.Method == "DELETE":
nodeID, err := strconv.ParseUint(nodeName, 0, 64)
if err != nil {
log.Printf("Failed to convert ID for conf change (%v)\n", err)
http.Error(w, "Failed on DELETE", http.StatusBadRequest)
return
}
cc := raftpb.ConfChange{
Type: raftpb.ConfChangeRemoveNode,
NodeID: nodeID,
}
h.confChangeC <- cc
// As above, optimistic that raft will apply the conf change
w.WriteHeader(http.StatusNoContent)
default:
w.Header().Set("Allow", "POST")
w.Header().Add("Allow", "DELETE")
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
func serveHTTPChatAPI(store *chatStore, port int, wsCommitsC <-chan roomPost, confChangeC chan<- raftpb.ConfChange, errorC <-chan error) {
api := &httpChatAPI{
store: store,
confChangeC: confChangeC,
wsCommitsC: wsCommitsC,
wsConns: []*websocket.Conn{},
}
go api.readWSCommits()
r := mux.NewRouter()
r.HandleFunc("/rooms", api.RoomsHandler)
r.HandleFunc("/rooms/{roomID}", api.RoomMessagesHandler)
r.HandleFunc("/ws", api.WebsocketHandler)
r.HandleFunc("/raft/{nodeID}", api.DefaultHandler)
srv := http.Server{
Addr: ":" + strconv.Itoa(port),
Handler: handlers.CORS()(r),
}
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Fatal(err)
}
}()
// exit when raft goes down
if err, ok := <-errorC; ok {
log.Fatal(err)
}
}