-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
64 lines (54 loc) · 1.55 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
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"path"
"github.com/jinzhu/configor"
"github.com/krzysztof-gzocha/curnot/pkg/aggregator"
"github.com/krzysztof-gzocha/curnot/pkg/checker"
"github.com/krzysztof-gzocha/curnot/pkg/config"
"github.com/krzysztof-gzocha/curnot/pkg/currency"
"github.com/krzysztof-gzocha/curnot/pkg/notifier"
)
func main() {
var cfgFileName string
flag.StringVar(&cfgFileName, "cfg", path.Join(os.Getenv("HOME"), "curnot.yaml"), "Config file")
flag.Parse()
if cfgFileName == "" {
fmt.Println("Please specify config file path by --cfg")
os.Exit(1)
return
}
f, err := os.OpenFile(cfgFileName, os.O_RDONLY, 0)
if err != nil {
fmt.Printf("No such file: %s\n", cfgFileName)
os.Exit(1)
return
}
_ = f.Close()
ctx := context.Background()
cfg := config.Config{}
err = configor.New(&configor.Config{ENVPrefix: "CURNOT"}).Load(&cfg, cfgFileName)
if err != nil {
fmt.Printf("Could not load %s: %+v\n", cfgFileName, err)
os.Exit(1)
return
}
httpTimeout := cfg.Timeout
if cfg.Interval < cfg.Timeout {
httpTimeout = cfg.Interval
}
httpClient := &http.Client{Timeout: httpTimeout}
providersPool := currency.GetProvidersPool(httpClient, cfg.Providers)
notifierChain := notifier.NewChain(httpClient, cfg.Notifiers)
agg := aggregator.NewRateAggregator(notifierChain, cfg.Currencies)
tickerChecker := checker.NewTickerChecker(
cfg.Interval,
checker.NewChecker(cfg.Currencies, providersPool, agg),
)
fmt.Printf("Starting checking every %s..\n\n", cfg.Interval)
tickerChecker.StartChecking(ctx)
}