-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
200 lines (171 loc) · 6.38 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
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
package main
import (
"log"
"net/http"
"os"
"strconv"
"github.com/onestay/MarathonTools-API/api/routes/donations"
"github.com/onestay/MarathonTools-API/api/donationProviders"
"github.com/onestay/MarathonTools-API/api/routes/timer"
"github.com/go-redis/redis"
"github.com/joho/godotenv"
"github.com/onestay/MarathonTools-API/api/routes/social"
"github.com/julienschmidt/httprouter"
"github.com/onestay/MarathonTools-API/api/common"
"github.com/onestay/MarathonTools-API/api/routes/runs"
"github.com/onestay/MarathonTools-API/ws"
"gopkg.in/mgo.v2"
)
var (
mgs *mgo.Session
redisClient *redis.Client
port string
twitchClientID string
twitchClientSecret string
twitchCallback string
twitterKey string
twitterSecret string
twitterCallback string
refreshInterval int
marathonSlug string
gdqURL, gdqEventID, gdqUsername, gdqPassword string
mgoURL, redisURL string
socialAuthURL, socialAuthKey string
featuredChannelsKey string
)
type Server struct {
r *httprouter.Router
}
func init() {
err := godotenv.Load()
if err != nil {
log.Println("Error loading .env file.")
}
parseEnvVars()
log.Printf("Connecting to mgo server at %v", mgoURL)
mgs = getMongoSession()
log.Printf("Connecting to redis server at %v", redisURL)
redisClient = getRedisClient()
}
func main() {
startHTTPServer()
}
func startHTTPServer() {
r := httprouter.New()
hub := ws.NewHub()
log.Println("Initializing base controller...")
baseController := common.NewController(hub, mgs, 0, redisClient)
log.Println("Initializing social controller...")
social.NewSocialController(twitchClientID, twitchClientSecret, twitchCallback, twitterKey, twitterSecret, twitterCallback, socialAuthURL, socialAuthKey, featuredChannelsKey, baseController, r)
log.Println("Initializing time controller...")
timer.NewTimeController(baseController, refreshInterval, r)
log.Println("Initializing run controller")
runs.NewRunController(baseController, r)
var donProv donations.DonationProvider
donationsEnabled := true
var err error
if os.Getenv("DONATION_PROVIDER") == "gdq" {
log.Println("Creating new GDQ donation provider")
donProv, err = donationProviders.NewGDQDonationProvider(gdqURL, gdqEventID, gdqUsername, gdqPassword)
if err != nil {
log.Printf("Error during gdq donation provider creation: %v", err)
donationsEnabled = false
}
} else if os.Getenv("DONATION_PROVIDER") == "srcom" {
log.Println("Creating new speedrun.com donation provider")
donProv, err = donationProviders.NewSRComDonationProvider(marathonSlug)
if err != nil {
log.Printf("Error during donation provider creation: %v", err)
donationsEnabled = false
}
} else {
log.Print("No donation provider specified")
donationsEnabled = false
}
donationController := donations.NewDonationController(baseController, donProv, donationsEnabled)
log.Println("Starting websocket hub...")
go hub.Run()
r.GET("/ws", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
ws.ServeWs(hub, w, r, baseController.SendInitialData())
})
// routes for donation endpoint
r.GET("/donations/total", donationController.GetTotal)
r.GET("/donations/all", donationController.GetAll)
r.GET("/donations/total/amount", donationController.GetTotalDonations)
r.GET("/donations/total/update/start", donationController.StartTotalUpdate)
r.GET("/donations/total/update/stop", donationController.StopTotalUpdate)
// checklist stuff
r.POST("/checklist/add", baseController.CL.AddItem)
r.DELETE("/checklist/delete", baseController.CL.DeleteItem)
r.PUT("/checklist/toggle", baseController.CL.ToggleItem)
r.GET("/checklist/done", baseController.CL.CheckDoneHTTP)
r.GET("/checklist", baseController.CL.GetChecklist)
// settings stuff
r.POST("/settings", baseController.Settings.SetSettings)
r.GET("/settings", baseController.Settings.GetSettings)
log.Println("server running on " + port)
log.Fatal(http.ListenAndServe(port, &Server{r}))
}
func getMongoSession() *mgo.Session {
s, err := mgo.Dial(mgoURL)
if err != nil {
panic("Couldn't establish mgo session " + err.Error())
}
return s
}
func getRedisClient() *redis.Client {
client := redis.NewClient(&redis.Options{
Addr: redisURL + ":6379",
Password: "",
DB: 0,
})
return client
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, PATCH, OPTIONS, HEAD")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
if r.Method == "OPTIONS" {
// TODO: proper OPTIONS handling
w.WriteHeader(200)
} else {
s.r.ServeHTTP(w, r)
}
}
func parseEnvVars() {
mgoURL = os.Getenv("MONGO_SERVER")
redisURL = os.Getenv("REDIS_SERVER")
twitchClientID = os.Getenv("TWITCH_CLIENT_ID")
twitchClientSecret = os.Getenv("TWITCH_CLIENT_SECRET")
twitchCallback = os.Getenv("TWITCH_CALLBACK")
twitterKey = os.Getenv("TWITTER_KEY")
twitterSecret = os.Getenv("TWITTER_SECRET")
twitterCallback = os.Getenv("TWITTER_CALLBACK")
socialAuthURL = os.Getenv("SOCIAL_AUTH_URL")
socialAuthKey = os.Getenv("SOCIAL_AUTH_KEY")
i, err := strconv.Atoi(os.Getenv("REFRESH_INTERVAL"))
if err != nil && len(os.Getenv("REFRESH_INTERVAL")) != 0 {
log.Println("Error parsing REFRESH_INTERVAL defaulting to 100ms")
i = 100
} else if err != nil {
i = 100
}
refreshInterval = i
port = os.Getenv("HTTP_PORT")
if len(port) == 0 {
port = ":3000"
}
if os.Getenv("DONATION_PROVIDER") == "gdq" {
gdqURL = os.Getenv("GDQ_TRACKER_URL")
gdqEventID = os.Getenv("GDQ_TRACKER_EVENT_ID")
gdqUsername = os.Getenv("GDQ_TRACKER_USERNAME")
gdqPassword = os.Getenv("GDQ_TRACKER_PASSWORD")
} else if os.Getenv("DONATION_PRODIVER") == "srcom" {
marathonSlug = os.Getenv("MARATHON_SLUG")
} else if len(os.Getenv("DONATION_PROVIDER")) == 0 {
log.Println("No donations provider specified. Donations disabled.")
} else {
log.Printf("Unknown donation provider %v. Donations disabled.", os.Getenv("DONATION_PROVIDER"))
}
featuredChannelsKey = os.Getenv("FEATURED_CHANNELS_KEY")
}