This repository has been archived by the owner on Jul 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
131 lines (113 loc) · 3.23 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
package main
import (
"context"
"os"
"os/signal"
"syscall"
"time"
"github.com/nezorflame/opengapps-mirror-bot/internal/pkg/config"
"github.com/nezorflame/opengapps-mirror-bot/internal/pkg/db"
"github.com/nezorflame/opengapps-mirror-bot/internal/pkg/storage"
"github.com/nezorflame/opengapps-mirror-bot/pkg/net"
"github.com/nezorflame/opengapps-mirror-bot/pkg/telegram"
"github.com/google/go-github/v37/github"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"golang.org/x/oauth2"
)
var configName string
func init() {
// get flags, init logger
pflag.StringVar(&configName, "config", "config", "Config file name")
level := pflag.String("log-level", "INFO", "Logrus log level (DEBUG, WARN, etc.)")
pflag.Parse()
logLevel, err := log.ParseLevel(*level)
if err != nil {
log.Fatalf("Unknown log level: %s", *level)
}
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
log.SetOutput(os.Stdout)
log.SetLevel(logLevel)
if configName == "" {
pflag.PrintDefaults()
os.Exit(1)
}
}
func main() {
// init flags and ctx
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// init config and tracing
log.Info("Starting the bot")
cfg, err := config.New(configName)
if err != nil {
log.Fatalf("Unable to init config: %v", err)
}
log.Info("Config parsed")
// init Github client
log.Info("Creating Github client")
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: cfg.GetString("github.token")},
)
tc := oauth2.NewClient(ctx, ts)
gh := github.NewClient(tc)
// init download queue and cache
log.Info("Creating download queue")
dq := net.NewQueue(cfg.GetInt("max_downloads"))
cache, err := db.NewDB(cfg.GetString("db.path"), cfg.GetDuration("db.timeout"))
if err != nil {
log.Fatal(err)
}
// init GApps global storage
log.Info("Initiating GApps global storage")
gs := storage.NewGlobalStorage(cache)
if err = gs.Load(); err != nil {
log.Fatalf("Unable to load the global storage from cache: %v", err)
}
if err = gs.AddLatestStorage(ctx, gh, dq, cfg); err != nil {
log.Fatalf("Unable to add the latest storage: %v", err)
}
// init package watcher
log.Info("Initiating GApps package watcher")
go func() {
ticker := time.NewTicker(cfg.GetDuration("gapps.renew_period"))
for {
select {
case <-ticker.C:
log.Info("Updating the current storage")
if err = gs.AddLatestStorage(ctx, gh, dq, cfg); err != nil {
log.Errorf("Unable to add the latest storage: %v", err)
}
case <-ctx.Done():
log.Warnf("Closing the watcher by context: %v", ctx.Err())
ticker.Stop()
return
}
}
}()
// create bot
bot, err := telegram.NewBot(ctx, cfg, dq, gs, gh)
if err != nil {
log.WithError(err).Fatal("Unable to create bot")
}
log.Info("Bot created")
// init graceful stop chan
log.Debug("Initiating system signal watcher")
var gracefulStop = make(chan os.Signal)
signal.Notify(gracefulStop, syscall.SIGTERM)
signal.Notify(gracefulStop, syscall.SIGINT)
go func() {
sig := <-gracefulStop
log.Warnf("Caught sig %+v, stopping the app", sig)
cancel()
bot.Stop()
gs.Save()
if err = cache.Close(false); err != nil {
log.WithError(err).Error("Unable to close DB")
}
os.Exit(0)
}()
// start the bot
log.Info("Starting the bot")
bot.Start()
}