-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstarter.go
459 lines (376 loc) · 13.8 KB
/
starter.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package main
import (
"log"
"net/http"
"os"
"strconv"
"time"
"gopkg.in/mgo.v2"
"encoding/json"
"fmt"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
const (
contentTypeKey = "Content-Type"
contentTypeValJSON = "application/json"
)
var mongoClient *mgo.Session
func main() {
// log.SetOutput(os.Stdout)
mongoClient = MongoGetSession(os.Getenv("MONGO_IP"), os.Getenv("MONGO_USERNAME"), os.Getenv("MONGO_PASSWORD"))
MongoCreateCollectionIndexes(mongoClient)
allowedHeaders := handlers.AllowedHeaders([]string{"X-Requested-With"})
allowedOrigins := handlers.AllowedOrigins([]string{"*"})
allowedMethods := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"})
router := makeRouter()
fmt.Println("ri-storage-twitter MS running")
log.Fatal(http.ListenAndServe(":9682", handlers.CORS(allowedHeaders, allowedOrigins, allowedMethods)(router)))
}
func makeRouter() *mux.Router {
router := mux.NewRouter()
// Insert
router.HandleFunc("/hitec/repository/twitter/store/tweet/", postTweet).Methods("POST")
router.HandleFunc("/hitec/repository/twitter/store/classified/tweet/", postClassifiedTweet).Methods("POST")
router.HandleFunc("/hitec/repository/twitter/store/observable/", postObservableTwitter).Methods("POST")
router.HandleFunc("/hitec/repository/twitter/label/tweet/", postLabelTwitter).Methods("POST")
router.HandleFunc("/hitec/repository/twitter/store/topics", postTweetTopics).Methods("POST")
router.HandleFunc("/hitec/repository/twitter/access_key", postCheckAccessKey).Methods("POST")
router.HandleFunc("/hitec/repository/twitter/access_key/update", postUpdateAccessKeyConfiguration).Methods("POST")
// Get
router.HandleFunc("/hitec/repository/twitter/account_name/{account_name}/class/{tweet_class}", getTweetOfClass).Methods("GET")
router.HandleFunc("/hitec/repository/twitter/account_name/{account_name}/class/{tweet_class}/limit/{limit}", getTweetOfClassLimited).Methods("GET")
router.HandleFunc("/hitec/repository/twitter/account_name/{account_name}/all", getAllTweetsOfAccount).Methods("GET")
router.HandleFunc("/hitec/repository/twitter/account_name/{account_name}/all/unlabeled", getAllUnlabeledTweetsOfAccount).Methods("GET")
router.HandleFunc("/hitec/repository/twitter/account_name/{account_name}/currentweek", getAllTweetsOfAccountForCurrentWeek).Methods("GET")
router.HandleFunc("/hitec/repository/twitter/account_name/{account_name}/lang/{lang}/unclassified", getAllUnclassifiedTweetsOfAccount).Methods("GET")
router.HandleFunc("/hitec/repository/twitter/account_name/all", getAllTwitterAccountNames).Methods("GET")
router.HandleFunc("/hitec/repository/twitter/labeledtweets/all", getAllLabeledTweets).Methods("GET")
router.HandleFunc("/hitec/repository/twitter/observables", getObservablesTwitter).Methods("GET")
router.HandleFunc("/hitec/repository/twitter/access_key/configuration", postAccessKeyConfiguration).Methods("POST")
// Delete
router.HandleFunc("/hitec/repository/twitter/observables", deleteObservableTwitter).Methods("DELETE")
return router
}
func postTweet(w http.ResponseWriter, r *http.Request) {
var tweets []Tweet
err := json.NewDecoder(r.Body).Decode(&tweets)
if err != nil {
fmt.Printf("ERROR decoding json: %s for request body: %v\n", err, r.Body)
w.WriteHeader(http.StatusBadRequest)
return
}
err = validateTweets(tweets)
if err != nil {
fmt.Printf("ERROR validating json: %s for request body: %v\n", err, r.Body)
w.WriteHeader(http.StatusBadRequest)
return
}
// insert data into the db
m := mongoClient.Copy()
defer m.Close()
MongoInsertTweets(m, tweets)
// send response
w.WriteHeader(http.StatusOK)
w.Header().Set(contentTypeKey, contentTypeValJSON)
}
func postClassifiedTweet(w http.ResponseWriter, r *http.Request) {
var tweets []Tweet
err := json.NewDecoder(r.Body).Decode(&tweets)
if err != nil {
fmt.Printf("ERROR decoding json: %s for request body: %v\n", err, r.Body)
w.WriteHeader(http.StatusBadRequest)
return
}
err = validateTweets(tweets)
if err != nil {
fmt.Printf("ERROR validating json: %s for request body: %v\n", err, r.Body)
w.WriteHeader(http.StatusBadRequest)
return
}
fmt.Printf("REST call (postClassifiedTweet): update %v tweets\n", len(tweets))
// insert data into the db
m := mongoClient.Copy()
defer m.Close()
MongoUpdateTweetsSentimentAndClass(m, tweets)
// send response
w.WriteHeader(http.StatusOK)
w.Header().Set(contentTypeKey, contentTypeValJSON)
}
func postObservableTwitter(w http.ResponseWriter, r *http.Request) {
var observable ObservableTwitter
err := json.NewDecoder(r.Body).Decode(&observable)
if err != nil {
fmt.Printf("ERROR: %s for request body: %v\n", err, r.Body)
w.WriteHeader(http.StatusBadRequest)
return
}
err = observable.validate()
if err != nil {
fmt.Printf("ERROR: %s for request body: %v\n", err, r.Body)
w.WriteHeader(http.StatusBadRequest)
return
}
fmt.Printf("REST call (postObservableTwitter): insert observable %v\n", observable)
// insert data into the db
m := mongoClient.Copy()
defer m.Close()
ok := MongoInsertObservableTwitter(m, observable)
// send response
if ok {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
w.Header().Set(contentTypeKey, contentTypeValJSON)
}
func postLabelTwitter(w http.ResponseWriter, r *http.Request) {
var tweetLabel TweetLabel
err := json.NewDecoder(r.Body).Decode(&tweetLabel)
if err != nil {
fmt.Printf("ERROR: %s for request body: %v\n", err, r.Body)
w.WriteHeader(http.StatusBadRequest)
return
}
err = tweetLabel.validate()
if err != nil {
fmt.Printf("ERROR: %s for request body: %v\n", err, r.Body)
w.WriteHeader(http.StatusBadRequest)
return
}
fmt.Printf("REST call (postLabelTwitter): insert tweet label %v\n", tweetLabel)
// insert data into the db
m := mongoClient.Copy()
defer m.Close()
insertionOk := MongoInsertTweetLabel(m, tweetLabel)
updateOk := MongoUpdateTweetClassAndAnnotation(m, tweetLabel)
// send response
if insertionOk && updateOk {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
w.Header().Set(contentTypeKey, contentTypeValJSON)
}
func postTweetTopics(w http.ResponseWriter, r *http.Request) {
var tweet Tweet
err := json.NewDecoder(r.Body).Decode(&tweet)
if err != nil {
fmt.Printf("ERROR: %s for request body: %v\n", err, r.Body)
w.WriteHeader(http.StatusBadRequest)
return
}
fmt.Printf("REST call (postTweetTopics)\n")
// insert data into the db
m := mongoClient.Copy()
defer m.Close()
updateOk := MongoUpdateTweetTopics(m, tweet)
// send response
if updateOk {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
w.Header().Set(contentTypeKey, contentTypeValJSON)
}
func postCheckAccessKey(w http.ResponseWriter, r *http.Request) {
var accessKey AccessKey
err := json.NewDecoder(r.Body).Decode(&accessKey)
if err != nil {
fmt.Printf("ERROR: %s for request body: %v\n", err, r.Body)
w.WriteHeader(http.StatusBadRequest)
return
}
fmt.Printf("REST call (postCheckAccessKey)\n")
// insert data into the db
m := mongoClient.Copy()
defer m.Close()
accessKeyExists := MongoGetAccessKeyExists(m, accessKey)
// send response
w.Header().Set(contentTypeKey, contentTypeValJSON)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(ResponseMessage{Message: "access key status", Status: accessKeyExists})
}
func postUpdateAccessKeyConfiguration(w http.ResponseWriter, r *http.Request) {
var accessKey AccessKey
err := json.NewDecoder(r.Body).Decode(&accessKey)
if err != nil {
fmt.Printf("ERROR: %s for request body: %v\n", err, r.Body)
w.WriteHeader(http.StatusBadRequest)
return
}
fmt.Printf("REST call (postUpdateAccessKeyConfiguration) %s\n", accessKey)
// insert data into the db
m := mongoClient.Copy()
defer m.Close()
MongoUpdateAccessKeyConfiguration(m, accessKey)
// send response
w.Header().Set(contentTypeKey, contentTypeValJSON)
w.WriteHeader(http.StatusOK)
}
func getTweetOfClass(w http.ResponseWriter, r *http.Request) {
// get request param
params := mux.Vars(r)
tweetedToName := params["account_name"]
tweetClass := params["tweet_class"]
fmt.Println("params: ", tweetedToName, tweetClass)
m := mongoClient.Copy()
defer m.Close()
tweets := MongoGetTweetOfClass(m, tweetedToName, tweetClass)
// write the response
w.Header().Set(contentTypeKey, contentTypeValJSON)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(tweets)
}
func getTweetOfClassLimited(w http.ResponseWriter, r *http.Request) {
// get request param
params := mux.Vars(r)
tweetedToName := params["account_name"]
tweetClass := params["tweet_class"]
limitParam := params["limit"]
limit, err := strconv.Atoi(limitParam)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
fmt.Println("params: ", tweetedToName, tweetClass)
m := mongoClient.Copy()
defer m.Close()
tweets := MongoGetTweetOfClassLimited(m, tweetedToName, tweetClass, limit)
// write the response
w.Header().Set(contentTypeKey, contentTypeValJSON)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(tweets)
}
func getAllTweetsOfAccount(w http.ResponseWriter, r *http.Request) {
// get request param
params := mux.Vars(r)
accountName := params["account_name"]
fmt.Printf("REST call: getAllTweetsOfAccount, account %s\n", accountName)
m := mongoClient.Copy()
defer m.Close()
tweets := MongoGetAllTweetsOfAccountName(m, accountName)
// write the response
w.Header().Set(contentTypeKey, contentTypeValJSON)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(tweets)
}
func getAllUnclassifiedTweetsOfAccount(w http.ResponseWriter, r *http.Request) {
// get request param
params := mux.Vars(r)
accountName := params["account_name"]
lang := params["lang"]
fmt.Printf("REST call: getAllUnclassifiedTweetsOfAccount, account %s and lang %s \n", accountName, lang)
m := mongoClient.Copy()
defer m.Close()
tweets := MongoGetUnclassifiedAllTweetsOfAccountName(m, accountName, lang)
// write the response
w.Header().Set(contentTypeKey, contentTypeValJSON)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(tweets)
}
func getAllUnlabeledTweetsOfAccount(w http.ResponseWriter, r *http.Request) {
// get request param
params := mux.Vars(r)
accountName := params["account_name"]
fmt.Printf("REST call: getAllUnlabeledTweetsOfAccount, account %s\n", accountName)
m := mongoClient.Copy()
defer m.Close()
tweets := MongoGetAllUnlabeledTweetsOfAccountName(m, accountName)
// write the response
w.Header().Set(contentTypeKey, contentTypeValJSON)
if len(tweets) > 0 {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(tweets)
} else {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(ResponseMessage{Message: "could not find any matching tweet", Status: false})
}
}
func getAllTweetsOfAccountForCurrentWeek(w http.ResponseWriter, r *http.Request) {
// get request param
params := mux.Vars(r)
accountName := params["account_name"]
fmt.Printf("REST call: getAllTweetsOfAccountForCurrentWeek, account %s\n", accountName)
from, _ := strconv.Atoi(time.Now().AddDate(0, 0, -6).Format("20060102"))
to, _ := strconv.Atoi(time.Now().Format("20060102"))
m := mongoClient.Copy()
defer m.Close()
tweets := MongoGetAllTweetsOfAccountForCurrentWeek(m, accountName, from, to)
// write the response
w.Header().Set(contentTypeKey, contentTypeValJSON)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(tweets)
}
func getAllTwitterAccountNames(w http.ResponseWriter, r *http.Request) {
m := mongoClient.Copy()
defer m.Close()
fmt.Printf("REST call: getAllTwitterAccountNames\n")
twitterAccounts := MongoGetAllTwitterAccounts(m)
fmt.Printf("REST call: getAllTwitterAccountNames, result %v\n", twitterAccounts)
// write the response
w.Header().Set(contentTypeKey, contentTypeValJSON)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(twitterAccounts)
}
func getAllLabeledTweets(w http.ResponseWriter, r *http.Request) {
m := mongoClient.Copy()
defer m.Close()
labeledTweets := MongoGetAllLabeledTweets(m)
// write the response
w.Header().Set(contentTypeKey, contentTypeValJSON)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(labeledTweets)
}
func getObservablesTwitter(w http.ResponseWriter, r *http.Request) {
m := mongoClient.Copy()
defer m.Close()
observables := MongoGetAllObservableTwitter(m)
// write the response
w.Header().Set(contentTypeKey, contentTypeValJSON)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(observables)
}
func deleteObservableTwitter(w http.ResponseWriter, r *http.Request) {
var observable ObservableTwitter
err := json.NewDecoder(r.Body).Decode(&observable)
if err != nil {
fmt.Printf("ERROR: %s for request body: %v\n", err, r.Body)
w.WriteHeader(http.StatusBadRequest)
return
}
fmt.Printf("REST call: deleteObservableTwitter for %v\n", observable)
m := mongoClient.Copy()
defer m.Close()
ok := MongoDeleteObservableTwitter(m, observable)
// write the response
w.Header().Set(contentTypeKey, contentTypeValJSON)
if ok {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(ResponseMessage{Message: "observable successfully deleted", Status: true})
return
} else {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(ResponseMessage{Message: "could not delete observable", Status: false})
}
}
func postAccessKeyConfiguration(w http.ResponseWriter, r *http.Request) {
var accessKey AccessKey
err := json.NewDecoder(r.Body).Decode(&accessKey)
if err != nil {
fmt.Printf("ERROR: %s for request body: %v\n", err, r.Body)
w.WriteHeader(http.StatusBadRequest)
return
}
m := mongoClient.Copy()
defer m.Close()
w.Header().Set(contentTypeKey, contentTypeValJSON)
accessKeyExists := MongoGetAccessKeyExists(m, accessKey)
if !accessKeyExists {
w.WriteHeader(http.StatusBadRequest)
} else {
accessKeyConfiguration := MongoGetAccessKeyConfiguration(m, accessKey)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(accessKeyConfiguration)
}
}