-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (65 loc) · 1.97 KB
/
main.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
package main
import (
"net/http"
"log"
"github.com/ghrehh/tweetatlas/twitterstream"
"github.com/ghrehh/tweetatlas/utils"
"github.com/ghrehh/tweetatlas/web"
"github.com/ghrehh/findlocation"
"github.com/dghubble/go-twitter/twitter"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
)
func main() {
log.Print("App Started")
// Get the search phrase(s)
filters := utils.GetStreamFilter()
// Location aggregate stores our location results
locationAggregate := twitterstream.NewLocationAggregate(filters)
// Location finder is a package that attempts to find a Twitter user's location.
locationFinder := findlocation.NewLocationFinder()
// Connection orchestartor manages the websocket connections
co := web.NewConnectionOrchestrator()
// Handle different types returned by the stream
demux := twitter.NewSwitchDemux()
demux.Tweet = func(tweet *twitter.Tweet) {
location := locationFinder.FindLocation(tweet.User.Location)
locationAggregate.AddParsedLocation(location)
locationAggregate.AddSampleTweet(tweet, location)
co.LaStream <- locationAggregate
}
// Upgrade to a websocket connection, allow all origins
upgrader := websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
// Routing
r := mux.NewRouter()
r.Headers("Content-Type", "application/json")
r.HandleFunc("/tweets", func(w http.ResponseWriter, r *http.Request) {
web.ServeWebsocket(co, w, r, &upgrader)
})
// Get twitter keys
twitterKeys := utils.GetOauthKeys()
// Create tweet stream
sh := twitterstream.NewStreamHandler(twitterKeys)
// Initialize and start the stream switcher
ss := twitterstream.Switch{
Stream: nil,
Handler: demux.Handle,
}
s := twitterstream.Scheduler{
Switch: &ss,
StreamHandler: sh,
Filters: filters,
FilterIndex: 0,
SearchDuration: 15,
LocationAggregate: locationAggregate,
}
go s.Run()
// Start the connection orchestrator
go co.Run()
// Serve routes with CORs handler
http.ListenAndServe(utils.GetPort(), r)
}