-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
151 lines (128 loc) · 2.69 KB
/
service.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
package main
import (
"encoding/json"
"io"
"log"
"net"
"net/http"
"strings"
)
// Service provides HTTP service.
type Service struct {
addr string
ln net.Listener
store Store
}
// NewService returns an uninitialized HTTP service.
func NewService(addr string, store Store) *Service {
return &Service{
addr: addr,
store: store,
}
}
// Start starts the service.
func (s *Service) Start() error {
server := http.Server{
Handler: s,
}
ln, err := net.Listen("tcp", s.addr)
if err != nil {
return err
}
s.ln = ln
http.Handle("/", s)
go func() {
err := server.Serve(s.ln)
if err != nil {
log.Fatalf("HTTP serve: %s", err)
}
}()
return nil
}
// Close closes the service.
func (s *Service) Close() {
s.ln.Close()
return
}
// ServeHTTP allows Service to serve HTTP requests.
func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/key") {
s.handleKeyRequest(w, r)
} else if r.URL.Path == "/join" {
s.handleJoin(w, r)
} else {
w.WriteHeader(http.StatusNotFound)
}
}
func (s *Service) handleJoin(w http.ResponseWriter, r *http.Request) {
m := map[string]string{}
if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
if len(m) != 2 {
w.WriteHeader(http.StatusBadRequest)
return
}
remoteAddr, ok := m["addr"]
if !ok {
w.WriteHeader(http.StatusBadRequest)
return
}
nodeID, ok := m["id"]
if !ok {
w.WriteHeader(http.StatusBadRequest)
return
}
if err := s.store.Join(nodeID, remoteAddr); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
func (s *Service) handleKeyRequest(w http.ResponseWriter, r *http.Request) {
getKey := func() string {
parts := strings.Split(r.URL.Path, "/")
if len(parts) != 3 {
return ""
}
return parts[2]
}
switch r.Method {
case "GET":
k := getKey()
if k == "" {
w.WriteHeader(http.StatusBadRequest)
}
v, err := s.store.Get(k)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
b, err := json.Marshal(map[string]string{k: v})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
io.WriteString(w, string(b))
case "POST":
// Read the value from the POST body.
m := map[string]string{}
if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
for k, v := range m {
if err := s.store.Set(k, v); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
return
}
// Addr returns the address on which the Service is listening
func (s *Service) Addr() net.Addr {
return s.ln.Addr()
}