generated from suchimauz/golang-project-template
/
app.go
104 lines (77 loc) · 2.51 KB
/
app.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
package app
import (
"net/http"
"net/url"
"os"
"os/signal"
"syscall"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/suchimauz/wal-g-telegram-info/internal/config"
"github.com/suchimauz/wal-g-telegram-info/internal/job"
"github.com/suchimauz/wal-g-telegram-info/pkg/logger"
"github.com/suchimauz/wal-g-telegram-info/pkg/storage"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
cr "github.com/robfig/cron/v3"
)
func Run() {
// Initialize config
cfg, err := config.NewConfig()
if err != nil {
logger.Errorf("[ENV] %s", err.Error())
return
}
// Create new http client for telegram api
tgclient := &http.Client{}
// When http proxy for telegram api is declared,
if cfg.Telegram.HttpProxy != "" {
proxy, err := url.Parse(cfg.Telegram.HttpProxy)
if err != nil {
logger.Errorf("[TelegramBotApi] Proxy: %s", err.Error())
}
transport := &http.Transport{}
transport.Proxy = http.ProxyURL(proxy)
tgclient.Transport = transport
}
tgbot, err := tgbotapi.NewBotAPIWithClient(cfg.Telegram.BotToken, cfg.Telegram.ApiEndpoint, tgclient)
if err != nil {
logger.Errorf("[TelegramBotApi] %s", err.Error())
return
}
// Make new cron object, calls constructor
cron := cr.New(cr.WithSeconds(), cr.WithLocation(cfg.Timezone))
storageProvider, err := newStorageProvider(cfg)
if err != nil {
logger.Errorf("[FileStorage] Provider: %s", err.Error())
return
}
ij := job.NewInfoJob(cfg, tgbot, storageProvider)
jobId, err := cron.AddJob(cfg.CronSpec, ij)
if err != nil {
logger.Errorf("[Cron] AddJob: %s", err.Error())
}
cron.Start()
logger.Info("[Cron] Started job ", jobId)
// Graceful Shutdown
// Make new channel of size = 1
quit := make(chan os.Signal, 1)
// Listen system 15 and 2 signals, when one of they called, send info to quit channel
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
// Read channel, this block of code lock this thread, until someone writes to the channel
<-quit
// When someone call SIGTERM or SIGINT signals, we'll get to here
// cron.Stop() -> Wait jobs and stop
cron.Stop()
logger.Info("[Cron] Stopped! Exit")
}
func newStorageProvider(cfg *config.Config) (storage.Provider, error) {
client, err := minio.New(cfg.Minio.Endpoint, &minio.Options{
Creds: credentials.NewStaticV4(cfg.Minio.AccessKey, cfg.Minio.SecretKey, ""),
Secure: cfg.Minio.Secure,
})
if err != nil {
return nil, err
}
provider := storage.NewFileStorage(client, cfg.Minio.Bucket, cfg.Minio.Endpoint)
return provider, nil
}