/
api-server.go
249 lines (202 loc) · 6.63 KB
/
api-server.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"sync"
"time"
"os"
"path/filepath"
"github.com/nsqio/go-nsq"
)
// Incoming REST API requests
type apiRequest struct {
Addresses []string `json:"addresses"`
Callback string `json:"callback"` // Will be empty for /address/blocking
}
// Outgoing REST API Reply, for calls to /address/blocking
type apiReplyBlocking struct {
Results []VerifyRes // FIXME: Include the other type
}
// Outgoing REST API Reply, for calls to /address/callback
type apiReplyCallback struct {
StatusOK bool `json:"status-ok"`
Message string `json:"message"`
}
// The actual results of running a job,
// delivered either in the response to calls to /address/blocking or as callbacks when using /address/callback
type apiResult struct {
Results []VerifyRes
}
var producer *nsq.Producer // FIXME ??
var nsqdHost string
var httpClient http.Client
func main() {
// Check arguments
if len(os.Args) != 2 {
fmt.Println("Usage:\n\t", filepath.Base(os.Args[0]), " nsqd host\n(Just 1 nsqd, so no HA atm, fixme)")
os.Exit(0)
}
nsqdHost = os.Args[1]
// Initialize the nsq config
cfg := nsq.NewConfig()
// Make a http client with a timeout. Can be used everywere.
httpClient = http.Client{Timeout: time.Duration(time.Second * 60)}
// Create a Producer to send request to the workers
var err error
producer, err = nsq.NewProducer(nsqdHost+":"+nsqdPort, cfg)
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/address/blocking", func(w http.ResponseWriter, r *http.Request) {
log.Printf("Got %q request to %q\n", r.Method, r.URL.Path)
// Decode the JSON request - FIXME: duplicated code
var apiReq apiRequest
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&apiReq); err != nil {
log.Printf("Decoding api request as json failed:%v\n", err)
return
}
log.Printf("Got API req: +%v\n", apiReq)
var result apiReplyBlocking
result.Results, err = runJob(apiReq) // this blocks.. for a long time
if err != nil {
log.Printf("Failed running Job: %q\n", err)
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err := json.NewEncoder(w).Encode(result); err != nil {
log.Println("Encoding JSON reply failed:", err)
return
}
})
http.HandleFunc("/address/callback", func(w http.ResponseWriter, r *http.Request) {
log.Printf("Got %q request to %q\n", r.Method, r.URL.Path)
// Decode the JSON request
var apiReq apiRequest
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&apiReq); err != nil {
log.Printf("Decoding api request as json failed:%v\n", err)
return
}
log.Printf("Got API req: +%v\n", apiReq)
if apiReq.Callback == "" {
log.Printf("Inavlid API Request, missing callback: %+v", apiReq)
return
}
go func() {
// Send the requests to the workers and wait for the results
var result apiResult
result.Results, err = runJob(apiReq) // this blocks.. for a long time
if err != nil {
log.Printf("Failed running Job: %q\n", err)
return
}
// Encode it as JSON
resJSON, err := json.Marshal(result)
if err != nil {
log.Printf("api-server: Encoing job result as json for post-back failed: %+v, err: %q\n", result, err)
return
}
// Post the results back to the callback url
//log.Printf("POSTING:\n%+v\n\nTo:\n%q\n", result, apiReq.Callback)
log.Printf("POSTING to: %q\n", apiReq.Callback)
postRes, err := httpClient.Post(apiReq.Callback, "application/json", bytes.NewReader(resJSON))
if err != nil {
log.Printf("api-server: Posting to the callback failed: %v\n", err)
return
}
defer postRes.Body.Close()
postResBody, err := ioutil.ReadAll(postRes.Body)
if err != nil {
log.Printf("api-server: Reading the reply from Posting to the callback failed: %v\n", err)
return
}
fmt.Println("Ok, posting to callback succeeded. Server said:%q", postResBody)
}()
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err := json.NewEncoder(w).Encode(apiReplyCallback{StatusOK: true, Message: "Results will be POSTed to: " + apiReq.Callback}); err != nil {
log.Println("Encoding JSON reply failed:", err)
return
}
})
log.Println("Listening on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func runJob(apiReq apiRequest) ([]VerifyRes, error) {
if len(apiReq.Addresses) == 0 {
// Empty request
return nil, nil
}
// Create a NSQ consumer to pick up replies from the workers
// Is this the most effective / best way to do this?
// Does it connect once per http api call?
// Initialize the nsq config
cfg := nsq.NewConfig()
// Create a random topic to use for the results - Based on nsq_tail
rand.Seed(time.Now().UnixNano())
resTopic := fmt.Sprintf(resTopicBase, rand.Int()%999999)
consumer, err := nsq.NewConsumer(resTopic, managerChannel, cfg)
if err != nil {
log.Fatal(err)
}
consumer.ChangeMaxInFlight(nsqMaxInFlight) // cfg.Set() did not work for some reason
// Make sure we gather all responses.. or timeout
// Nice example: http://stackoverflow.com/questions/32840687/timeout-for-waitgroup-wait
wgResults := sync.WaitGroup{}
var result []VerifyRes
// Handle the results
consumer.AddHandler(nsq.HandlerFunc(func(m *nsq.Message) error {
var res VerifyRes
err := json.Unmarshal(m.Body, &res)
if err != nil {
// log.Printf("Decoding verify result as json failed:%v\n", err)
return fmt.Errorf("Decoding verify result as json failed: %v\n", err)
}
log.Printf("Got result: %q\n", m.Body)
result = append(result, res)
wgResults.Done()
return nil
}))
// err = consumer.ConnectToNSQLookupd(nsqLookupdHost + ":" + nsqLookupdPort)
err = consumer.ConnectToNSQD(nsqdHost + ":" + nsqdPort)
if err != nil {
log.Fatal(err)
}
for _, a := range apiReq.Addresses {
// Make a request with the email and encode it as JSON
req := VerifyReq{Email: a, ResultTopic: resTopic}
reqJSON, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("api-handler-and-manger: Encoing result as JSON failed: %+v, err: %q\n", req, err)
// return //fmt.Errorf("api-handler-and-manger: Encoing result as JSON failed: %+v, err: %q\n", req, err)
}
// Publish to NSQ
// MultiPublish instead?
log.Printf("Publishing %q\t", req)
producer.Publish(reqTopic, reqJSON)
log.Println("done!")
wgResults.Add(1)
}
done := make(chan bool)
go func() {
defer close(done) // Use close as a signal
wgResults.Wait()
}()
select {
case <-consumer.StopChan:
//return
log.Println("Consumer Stopping")
case <-done:
log.Println("Job Done!")
consumer.Stop()
case <-time.After(time.Minute * 3):
log.Println("Jobe Timeout!")
consumer.Stop()
}
return result, nil
}