-
Notifications
You must be signed in to change notification settings - Fork 1
/
metaqtv.go
96 lines (77 loc) · 2.04 KB
/
metaqtv.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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
apiVersion1 "metaqtv/api/v1"
"metaqtv/dataprovider"
"metaqtv/mhttp"
"metaqtv/scrape"
)
func main() {
// config
conf := getConfig()
// data provider
scraper := scrape.NewServerScraper()
scraper.Config = conf.scrapeConfig
scraper.Start()
dataProvider := dataprovider.New(&scraper)
// api versions
apiVersions := []mhttp.Api{
apiVersion1.New("v1", &dataProvider),
}
// merge endpoints
endpoints := make(mhttp.Endpoints, 0)
for _, api := range apiVersions {
for url, handler := range api.Endpoints {
fullUrl := fmt.Sprintf("/api/%s/%s", api.BaseUrl, url)
endpoints[fullUrl] = handler
}
}
mhttp.Serve(conf.httpPort, endpoints)
}
type AppConfig struct {
httpPort int
scrapeConfig scrape.Config
}
func getConfig() AppConfig {
var (
httpPort int
masterInterval int
serverInterval int
activeServerInterval int
)
flag.IntVar(&httpPort, "port", 80, "HTTP listen port")
flag.IntVar(&masterInterval, "master", scrape.DefaultConfig.MasterInterval, "Master server update interval in seconds")
flag.IntVar(&serverInterval, "server", scrape.DefaultConfig.ServerInterval, "Server update interval in seconds")
flag.IntVar(&activeServerInterval, "active", scrape.DefaultConfig.ActiveServerInterval, "Active server update interval in seconds")
flag.Parse()
masterServers, err := getMasterServersFromJsonFile("master_servers.json")
if err != nil {
log.Println("Unable to read master_servers.json")
os.Exit(1)
}
return AppConfig{
httpPort: httpPort,
scrapeConfig: scrape.Config{
MasterServers: masterServers,
MasterInterval: masterInterval,
ServerInterval: serverInterval,
ActiveServerInterval: activeServerInterval,
},
}
}
func getMasterServersFromJsonFile(filePath string) ([]string, error) {
result := make([]string, 0)
jsonFile, err := os.ReadFile(filePath)
if err != nil {
return result, err
}
err = json.Unmarshal(jsonFile, &result)
if err != nil {
return result, err
}
return result, nil
}