-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
202 lines (167 loc) · 5.11 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
201
202
package main
import (
"flag"
"fmt"
"io"
"net/http"
"os"
"os/signal"
"strings"
"git.abyle.org/hps/alolstats/api"
"git.abyle.org/hps/alolstats/config"
"git.abyle.org/hps/alolstats/fetchrunner"
"git.abyle.org/hps/alolstats/logging"
"git.abyle.org/hps/alolstats/mongobackend"
"git.abyle.org/hps/alolstats/riotclient"
"git.abyle.org/hps/alolstats/riotclientv4"
"git.abyle.org/hps/alolstats/statsrunner"
"git.abyle.org/hps/alolstats/storage"
riotclientdd "git.abyle.org/hps/alolstats/riotclient/datadragon"
riotclientrl "git.abyle.org/hps/alolstats/riotclient/ratelimit"
"github.com/BurntSushi/toml"
"github.com/sirupsen/logrus"
)
const (
defaultConfigPath = "./alolstats.toml"
defaultLoggingLevel = "debug"
)
/**
* Version should be set while build using ldflags (see Makefile)
*/
var version string
var compTime string
var configPath string
var loggingLevel string
var logFile string
var interrupt chan os.Signal
var log *logrus.Entry
func init() {
flag.StringVar(&configPath, "c", defaultConfigPath, "Path to toml config file")
flag.StringVar(&loggingLevel, "l", defaultLoggingLevel, "Logging level (panic, fatal, error, warn/warning, info or debug)")
flag.StringVar(&logFile, "L", "", "Log file to use")
flag.Parse()
}
func statusEndpoint(w http.ResponseWriter, r *http.Request) {
status := fmt.Sprintf(`{"status":"OK", "version":"%s", "compiled_at":"%s"}`, version, compTime)
io.WriteString(w, string(status))
}
func storageBackendCreator(cfg config.StorageBackend) (storage.Backend, error) {
backendName := strings.ToLower(cfg.Backend)
switch backendName {
case "mongo":
fallthrough
case "mongodb":
backend, err := mongobackend.NewBackend(cfg.MongoBackend)
if err != nil {
log.Errorln("Error creating the Mongo Storage Backend:" + err.Error())
return nil, err
}
return backend, nil
default:
return nil, fmt.Errorf("Unknown storage backend specified in config: %s", cfg.Backend)
}
}
func riotClientCreator(cfg config.RiotClient) (riotclient.Client, error) {
version := strings.ToLower(cfg.APIVersion)
switch version {
case "v3":
return nil, fmt.Errorf("API v3 is not supported anymore")
case "v4":
httpClient := &http.Client{}
ddragon, err := riotclientdd.New(httpClient, cfg)
if err != nil {
log.Errorln("Error creating Riot Client Data Dragon:" + err.Error())
return nil, err
}
rateLimit, err := riotclientrl.New()
if err != nil {
log.Errorln("Error creating Riot Client Rate Limit Checker:" + err.Error())
return nil, err
}
riotClient, err := riotclientv4.NewClient(httpClient, cfg, ddragon, rateLimit)
if err != nil {
log.Errorln("Error creating RiotClient APIVersion V4:" + err.Error())
return nil, err
}
return riotClient, nil
default:
return nil, fmt.Errorf("Unknown RiotClient APIVersion specified in config: %s", cfg.APIVersion)
}
}
func main() {
logging.Init()
if len(logFile) > 0 {
logging.SetLogFile(logFile)
}
logging.SetLoggingLevel(loggingLevel)
log = logging.Get("main")
log.Println("ALoLStats " + version + " (" + compTime + ") is STARTING")
interrupt = make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
var cfg config.Config
if _, err := toml.DecodeFile(configPath, &cfg); err != nil {
log.Fatalln(err)
}
api, err := api.NewAPI(cfg.API)
if err != nil {
log.Fatalln("Error creating the API:" + err.Error())
}
api.AttachModuleGet("/status", statusEndpoint)
clients := make(map[string]riotclient.Client)
for name, clientConfig := range cfg.RiotClient {
client, err := riotClientCreator(clientConfig)
if err != nil {
log.Fatalf("Error creating the Riot Client %s: %s", name, err.Error())
}
client.Start()
clients[name] = client
}
backend, err := storageBackendCreator(cfg.StorageBackend)
if err != nil {
log.Fatalf("Error creating the Storage Backend: %s", err)
}
err = backend.Connect()
if err != nil {
log.Fatalf("Error connecting the Storage Backend: %s", err)
}
storage, err := storage.NewStorage(cfg.LoLStorage, clients, backend)
if err != nil {
log.Fatalf("Error creating the Storage: %s", err)
}
storage.RegisterAPI(api)
storage.Start()
statsRunner, err := statsrunner.NewStatsRunner(cfg.StatsRunner, storage)
if err != nil {
log.Fatalf("Error creating the StatsRunner: %s", err)
}
var fetchRunners []*fetchrunner.FetchRunner
for name, fetchRunnerConfig := range cfg.FetchRunner {
fetchRunner, err := fetchrunner.NewFetchRunner(fetchRunnerConfig, storage)
if err != nil {
log.Fatalf("Error creating the FetchRunner %s: %s", name, err.Error())
}
fetchRunner.Start()
fetchRunners = append(fetchRunners, fetchRunner)
}
statsRunner.Start()
api.Start()
log.Println("ALoLStats (" + version + ") is READY")
for {
select {
case <-interrupt:
api.Stop()
statsRunner.Stop()
for _, fetchRunner := range fetchRunners {
fetchRunner.Stop()
}
storage.Stop()
for _, client := range clients {
client.Stop()
}
log.Printf("Storage handeled %d requests since startup", storage.GetHandeledRequests())
log.Printf("StatsRunner handeled %d requests since startup", statsRunner.GetHandeledRequests())
log.Println("ALoLStats gracefully shut down")
return
}
}
}