-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnector.go
222 lines (201 loc) · 5.31 KB
/
connector.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"os"
"strings"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
var authToken string
var verificationHost string
var resetFlag string
var port string
var db *sql.DB
// Node struct
type Node struct {
Val string
}
func main() {
log.Println("main():starting up, setting env variables")
port = fmt.Sprintf(":%s", os.Args[1])
log.Println("main():port=" + port)
authToken = os.Args[2]
log.Println("main():authToken=" + authToken)
verificationHost = os.Args[3]
log.Println("main():verificationHost=" + verificationHost)
resetFlag = os.Args[4]
log.Println("main():resetFlag=" + resetFlag)
setupDB()
defer db.Close()
log.Println("main():starting server")
http.HandleFunc("/add", addReq)
http.HandleFunc("/remove", removeReq)
//http.HandleFunc("/reset", resetReq)
http.ListenAndServe(port, nil)
}
func setupDB() {
log.Println("setupDB():opening db connection")
var err error
db, err = sql.Open("sqlite3", "./local.db")
if err != nil {
log.Fatal(err)
}
if resetFlag == "y" {
log.Println("setupDB():reset db and migrate")
q := `DROP TABLE IF EXISTS nodes`
db.Exec(q)
q = `DROP TABLE IF EXISTS roots`
db.Exec(q)
addTables()
}
}
func addTables() {
sqlStmt := `
create table nodes (id integer not null primary key, val varchar(64) not null );
create table roots (id integer not null primary key, val varchar(64) not null );
`
_, err := db.Exec(sqlStmt)
if err != nil {
log.Fatalf("%q: %s\n", err, sqlStmt)
}
}
// POST /add
func addReq(w http.ResponseWriter, r *http.Request) {
log.Println("addReq():received a request to add hashes")
vals := parseRequest(r)
var badHashes []string
var nodes []Node
log.Println("addReq():inserting parsed values into database")
for _, val := range vals {
if len(val) != 64 {
badHashes = append(badHashes, val)
} else {
insertNode(val)
nodes = append(nodes, Node{Val: val})
}
}
addNodes(nodes)
if len(vals) == 0 || len(badHashes) != 0 {
http.Error(w, "Invalid hash values: "+strings.Join(badHashes, ","), http.StatusBadRequest)
} else {
js, err := json.Marshal(vals)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
}
// POST /remove
func removeReq(w http.ResponseWriter, r *http.Request) {
log.Println("addReq():received a request to add hashes")
vals := parseRequest(r)
var badHashes []string
var nodes []Node
log.Println("addReq():inserting parsed values into database")
for _, val := range vals {
if len(val) != 64 {
badHashes = append(badHashes, val)
} else {
insertNode(val)
nodes = append(nodes, Node{Val: val})
}
}
removeNodes(nodes)
if len(vals) == 0 || len(badHashes) != 0 {
http.Error(w, "Invalid hash values: "+strings.Join(badHashes, ","), http.StatusBadRequest)
} else {
js, err := json.Marshal(vals)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
}
func parseRequest(r *http.Request) []string {
var vals []string
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&vals)
if err != nil {
log.Fatal(err)
}
log.Println("parseRequest():parsing request body into json")
return vals
}
func addNodes(nodes []Node) {
log.Println("addNodes():making request to verification server")
url := verificationHost + "/add/"
log.Println("addNodes():making request to URL=" + url)
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(nodes)
req, err := http.NewRequest("POST", url, b)
log.Print("addNodes():setting request body=")
if err != nil {
log.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Access-Token", authToken)
log.Println("addNodes():making request to verification instance")
requestDump, err := httputil.DumpRequest(req, true)
if err != nil {
log.Println(err)
}
log.Println(string(requestDump))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
log.Print("addNodes():receieved response=" + string(body))
}
func removeNodes(nodes []Node) {
log.Println("addNodes():making request to verification server")
url := verificationHost + "/remove/"
log.Println("addNodes():making request to URL=" + url)
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(nodes)
req, err := http.NewRequest("POST", url, b)
log.Print("addNodes():setting request body=")
if err != nil {
log.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Access-Token", authToken)
log.Println("addNodes():making request to verification instance")
requestDump, err := httputil.DumpRequest(req, true)
if err != nil {
log.Println(err)
}
log.Println(string(requestDump))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
log.Print("addNodes():receieved response=" + string(body))
}
func insertNode(val string) {
q := `INSERT INTO nodes (val) VALUES ($1);`
db.Exec(q, val)
}
func deleteNode(val string) {
q := `DELETE FROM nodes WHERE val=$1;`
db.Exec(q, val)
}
// POST /reset
func resetReq(w http.ResponseWriter, r *http.Request) {
}