/
context.go
128 lines (103 loc) · 3.04 KB
/
context.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
package main
import (
"log"
"os"
"github.com/saladinkzn/dotabot-cron/dota"
"github.com/saladinkzn/dotabot-cron/matches"
"github.com/saladinkzn/dotabot-cron/metrics"
"github.com/saladinkzn/dotabot-cron/repository"
"github.com/saladinkzn/dotabot-cron/telegram"
"github.com/go-redis/redis"
)
type Context struct {
DotaApi dota.DotaApi
TelegramApi telegram.TelegramApi
MatchSubscriber matches.MatchSubscriber
SubscriptionRepository repository.SubscriptionRepository
YandexMonitoringClient metrics.YandexMonitoringClient
}
func InitContext() (context *Context, err error) {
dotaApiBaseUrl := getDotaApiBaseUrl()
dotaApiToken := getDotaApiToken()
log.Printf("Loaded dota settings %s, %s", dotaApiBaseUrl, dotaApiToken)
telegramApiBaseUrl := getTelegramApiBaseUrl()
telegramApiToken := getTelegramApiToken()
telegramProxyUrl := getTelegramProxyUrl()
log.Printf("Loaded telegram settings: %s, %s, %s", telegramApiBaseUrl, telegramApiToken, telegramProxyUrl)
dotaApi, err := dota.NewDotaClient(dotaApiBaseUrl, dotaApiToken)
if err != nil {
return
}
telegramApi, err := telegram.CreateTelegramApiClient(telegramApiBaseUrl, telegramApiToken, telegramProxyUrl)
if err != nil {
return
}
client := redis.NewClient(&redis.Options{
Addr: getRedisAddr(),
Password: "",
DB: 0,
})
var subscriptionRepository repository.SubscriptionRepository = repository.CreateRedisRepository(client)
matchSubscriber := matches.CreateMatchSubscriber(dotaApi, subscriptionRepository, telegramApi)
if err != nil {
return
}
monitoringClient := metrics.MakeYandexMonitoringClientImpl(getMonitoringUrl(), getFolderId(), getIamContext())
context = &Context{
DotaApi: dotaApi,
TelegramApi: telegramApi,
MatchSubscriber: matchSubscriber,
SubscriptionRepository: subscriptionRepository,
YandexMonitoringClient: monitoringClient}
return
}
func getDotaApiBaseUrl() string {
dotaApiBaseUrl := os.Getenv("DOTA_API_BASE_URL")
if dotaApiBaseUrl != "" {
return dotaApiBaseUrl
} else {
return "https://api.steampowered.com"
}
}
func getDotaApiToken() string {
return os.Getenv("DOTA_API_TOKEN")
}
func getTelegramApiBaseUrl() string {
telegramApiBaseUrl := os.Getenv("TELEGRAM_API_BASE_URL")
if telegramApiBaseUrl != "" {
return telegramApiBaseUrl
} else {
return "https://api.telegram.org"
}
}
func getTelegramApiToken() string {
return os.Getenv("TELEGRAM_API_TOKEN")
}
func getRedisAddr() string {
return os.Getenv("REDIS_ADDR")
}
func getTelegramProxyUrl() string {
return os.Getenv("TELEGRAM_PROXY_URL")
}
func getMonitoringUrl() string {
return os.Getenv("MONITORING_URL")
}
func getFolderId() string {
return os.Getenv("FOLDER_ID")
}
func getServiceAccountId() string {
return os.Getenv("SERVICE_ACCOUNT_ID")
}
func getKeyId() string {
return os.Getenv("KEY_ID")
}
func getKeyFileName() string {
return os.Getenv("KEY_FILE_NAME")
}
func getIamContext() metrics.IamContext {
return metrics.CreateIamContext(
getKeyId(),
getServiceAccountId(),
getKeyFileName(),
)
}