-
Notifications
You must be signed in to change notification settings - Fork 4
/
http.go
163 lines (148 loc) · 3.75 KB
/
http.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
package PaxiBFT
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
"github.com/salemmohammed/PaxiBFT/log"
)
// http request header names
const (
HTTPClientID = "Id"
HTTPCommandID = "Cid"
HTTPTimestamp = "Timestamp"
HTTPNodeID = "Id"
)
// serve serves the http REST API request from clients
func (n *node) http() {
mux := http.NewServeMux()
mux.HandleFunc("/", n.handleRoot)
mux.HandleFunc("/history", n.handleHistory)
mux.HandleFunc("/crash", n.handleCrash)
mux.HandleFunc("/drop", n.handleDrop)
// http string should be in form of ":8080"
url, err := url.Parse(config.HTTPAddrs[n.id])
if err != nil {
log.Fatal("http url parse error: ", err)
}
port := ":" + url.Port()
n.server = &http.Server{
Addr: port,
Handler: mux,
}
log.Info("http server starting on ", port)
log.Fatal(n.server.ListenAndServe())
}
func (n *node) handleRoot(w http.ResponseWriter, r *http.Request) {
var req Request
var cmd Command
var err error
// get all http headers
req.Properties = make(map[string]string)
for k := range r.Header {
if k == HTTPClientID {
cmd.ClientID = ID(r.Header.Get(HTTPClientID))
continue
}
if k == HTTPCommandID {
cmd.CommandID, err = strconv.Atoi(r.Header.Get(HTTPCommandID))
if err != nil {
log.Error(err)
}
continue
}
req.Properties[k] = r.Header.Get(k)
}
// get command key and value
if len(r.URL.Path) > 1 {
i, err := strconv.Atoi(r.URL.Path[1:])
if err != nil {
http.Error(w, "invalid path", http.StatusBadRequest)
log.Error(err)
return
}
cmd.Key = Key(i)
if r.Method == http.MethodPut || r.Method == http.MethodPost {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Error("error reading body: ", err)
http.Error(w, "cannot read body", http.StatusBadRequest)
return
}
cmd.Value = Value(body)
}
} else {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Error("error reading body: ", err)
http.Error(w, "cannot read body", http.StatusBadRequest)
return
}
json.Unmarshal(body, &cmd)
}
req.Command = cmd
req.Timestamp = time.Now().UnixNano()
req.NodeID = n.id // TODO does this work when forward twice
req.c = make(chan Reply, 1)
log.Debugf("I am in http file %v ",req.Command)
n.MessageChan <- req
reply := <-req.c
if reply.Err != nil {
http.Error(w, reply.Err.Error(), http.StatusInternalServerError)
return
}
// set all http headers
w.Header().Set(HTTPClientID, string(reply.Command.ClientID))
w.Header().Set(HTTPCommandID, strconv.Itoa(reply.Command.CommandID))
for k, v := range reply.Properties {
w.Header().Set(k, v)
}
_, err = io.WriteString(w, string(reply.Value))
if err != nil {
log.Error(err)
}
log.Debugf("<------- done from http -------> ")
}
func (n *node) handleHistory(w http.ResponseWriter, r *http.Request) {
w.Header().Set(HTTPNodeID, string(n.id))
k, err := strconv.Atoi(r.URL.Query().Get("key"))
if err != nil {
log.Error(err)
http.Error(w, "invalide key", http.StatusBadRequest)
return
}
h := n.Database.History(Key(k))
b, _ := json.Marshal(h)
_, err = w.Write(b)
if err != nil {
log.Error(err)
}
}
func (n *node) handleCrash(w http.ResponseWriter, r *http.Request) {
t, err := strconv.Atoi(r.URL.Query().Get("t"))
if err != nil {
log.Error(err)
http.Error(w, "invalide time", http.StatusBadRequest)
return
}
n.Socket.Crash(t)
// timer := time.NewTimer(time.Duration(t) * time.Second)
// go func() {
// n.server.Close()
// <-timer.C
// log.Error(n.server.ListenAndServe())
// }()
}
func (n *node) handleDrop(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
t, err := strconv.Atoi(r.URL.Query().Get("t"))
if err != nil {
log.Error(err)
http.Error(w, "invalide time", http.StatusBadRequest)
return
}
n.Drop(ID(id), t)
}