1
|
|
|
package httpserver |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"bytes" |
5
|
|
|
"encoding/json" |
6
|
|
|
"github.com/arazmj/gerdu/cache" |
7
|
|
|
"github.com/arazmj/gerdu/raftproxy" |
8
|
|
|
"github.com/gorilla/mux" |
9
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp" |
10
|
|
|
log "github.com/sirupsen/logrus" |
11
|
|
|
"net/http" |
12
|
|
|
) |
13
|
|
|
|
14
|
|
|
func newRouter(gerdu cache.UnImplementedCache) (router *mux.Router) { |
15
|
|
|
router = mux.NewRouter() |
16
|
|
|
router.HandleFunc("/cache/{key}", func(w http.ResponseWriter, r *http.Request) { |
17
|
|
|
getHandler(w, r, gerdu) |
18
|
|
|
}).Methods(http.MethodGet) |
19
|
|
|
router.HandleFunc("/cache/{key}", func(w http.ResponseWriter, r *http.Request) { |
20
|
|
|
putHandler(w, r, gerdu) |
21
|
|
|
}).Methods(http.MethodPut) |
22
|
|
|
router.HandleFunc("/cache/{key}", func(w http.ResponseWriter, r *http.Request) { |
23
|
|
|
deleteHandler(w, r, gerdu) |
24
|
|
|
}).Methods(http.MethodDelete) |
25
|
|
|
router.HandleFunc("/join", func(w http.ResponseWriter, r *http.Request) { |
26
|
|
|
joinHandler(w, r, gerdu) |
27
|
|
|
}).Methods(http.MethodPost) |
28
|
|
|
router.HandleFunc("/leave", func(w http.ResponseWriter, r *http.Request) { |
29
|
|
|
leaveHandler(w, r, gerdu) |
30
|
|
|
}).Methods(http.MethodPost) |
31
|
|
|
router.Handle("/metrics", promhttp.Handler()) |
32
|
|
|
return router |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
//HTTPServe start http server in plain text |
36
|
|
|
func HTTPServe(host string, gerdu cache.UnImplementedCache) { |
37
|
|
|
router := newRouter(gerdu) |
38
|
|
|
log.Infof("Gerdu started listening HTTP at %s\n", host) |
39
|
|
|
log.Fatal(http.ListenAndServe(host, router)) |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
//HTTPServeTLS start HTTP server in secure mode |
43
|
|
|
func HTTPServeTLS(host string, tlsCert, tlsKey string, gerdu cache.UnImplementedCache) { |
44
|
|
|
router := newRouter(gerdu) |
45
|
|
|
log.Printf("Gerdu started listening HTTPS TLS at %s\n", host) |
46
|
|
|
log.Fatal(http.ListenAndServeTLS(host, tlsCert, tlsKey, router)) |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
func putHandler(w http.ResponseWriter, r *http.Request, gerdu cache.UnImplementedCache) { |
50
|
|
|
vars := mux.Vars(r) |
51
|
|
|
key := vars["key"] |
52
|
|
|
buf := new(bytes.Buffer) |
53
|
|
|
_, err := buf.ReadFrom(r.Body) |
54
|
|
|
if err != nil { |
55
|
|
|
log.Fatal(err.Error()) |
56
|
|
|
return |
57
|
|
|
} |
58
|
|
|
value := buf.String() |
59
|
|
|
|
60
|
|
|
created := gerdu.Put(key, value) |
61
|
|
|
if !created { |
62
|
|
|
log.Printf("HTTP UPDATE Key: %s Value: %s\n", key, value) |
63
|
|
|
} else { |
64
|
|
|
log.Printf("HTTP INSERT Key: %s Value: %s\n", key, value) |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if created { |
68
|
|
|
w.WriteHeader(http.StatusCreated) |
69
|
|
|
} else { |
70
|
|
|
w.WriteHeader(http.StatusNoContent) |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
func getHandler(w http.ResponseWriter, r *http.Request, gerdu cache.UnImplementedCache) { |
75
|
|
|
vars := mux.Vars(r) |
76
|
|
|
key := vars["key"] |
77
|
|
|
if value, ok := gerdu.Get(key); ok { |
78
|
|
|
log.Printf("HTTP RETREIVED Key: %s Value: %s\n", key, value) |
79
|
|
|
w.WriteHeader(http.StatusOK) |
80
|
|
|
_, _ = w.Write([]byte(value)) |
81
|
|
|
} else { |
82
|
|
|
log.Printf("HTTP MISSED Key: %s \n", key) |
83
|
|
|
w.WriteHeader(http.StatusNotFound) |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
func deleteHandler(w http.ResponseWriter, r *http.Request, gerdu cache.UnImplementedCache) { |
88
|
|
|
vars := mux.Vars(r) |
89
|
|
|
key := vars["key"] |
90
|
|
|
if ok := gerdu.Delete(key); ok { |
91
|
|
|
log.Printf("HTTP DELETED Key: %s\n", key) |
92
|
|
|
w.WriteHeader(http.StatusOK) |
93
|
|
|
} else { |
94
|
|
|
log.Printf("HTTP MISSED Key: %s \n", key) |
95
|
|
|
w.WriteHeader(http.StatusNotFound) |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
func joinHandler(w http.ResponseWriter, r *http.Request, gerdu cache.UnImplementedCache) { |
100
|
|
|
raftCache := gerdu.(*raftproxy.RaftProxy) |
101
|
|
|
m := map[string]string{} |
102
|
|
|
if err := json.NewDecoder(r.Body).Decode(&m); err != nil { |
103
|
|
|
w.WriteHeader(http.StatusBadRequest) |
104
|
|
|
return |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if len(m) != 2 { |
108
|
|
|
w.WriteHeader(http.StatusBadRequest) |
109
|
|
|
return |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
remoteAddr, ok := m["addr"] |
113
|
|
|
if !ok { |
114
|
|
|
w.WriteHeader(http.StatusBadRequest) |
115
|
|
|
return |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
nodeID, ok := m["id"] |
119
|
|
|
if !ok { |
120
|
|
|
w.WriteHeader(http.StatusBadRequest) |
121
|
|
|
return |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if err := raftCache.Join(nodeID, remoteAddr); err != nil { |
125
|
|
|
w.WriteHeader(http.StatusInternalServerError) |
126
|
|
|
return |
127
|
|
|
} |
128
|
|
|
log.Infof("Node %s, remoteAddr %s joined", nodeID, remoteAddr) |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
func leaveHandler(w http.ResponseWriter, r *http.Request, gerdu cache.UnImplementedCache) { |
132
|
|
|
raftCache := gerdu.(*raftproxy.RaftProxy) |
133
|
|
|
buf := new(bytes.Buffer) |
134
|
|
|
buf.ReadFrom(r.Body) |
135
|
|
|
nodeId := buf.String() |
136
|
|
|
|
137
|
|
|
if nodeId == "" { |
138
|
|
|
w.WriteHeader(http.StatusBadRequest) |
139
|
|
|
return |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if err := raftCache.Leave(nodeId); err != nil { |
143
|
|
|
w.WriteHeader(http.StatusInternalServerError) |
144
|
|
|
return |
145
|
|
|
} |
146
|
|
|
log.Infof("Node %s has left", nodeId) |
147
|
|
|
} |
148
|
|
|
|