-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (56 loc) · 1.71 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
package main
import (
"fmt"
"log"
"github.com/dnahurnyi/proxybot/bot"
"github.com/dnahurnyi/proxybot/client"
"github.com/dnahurnyi/proxybot/opts"
"github.com/dnahurnyi/proxybot/storage/postgres"
_ "github.com/golang/mock/mockgen/model" // no lint
"gopkg.in/go-playground/validator.v9"
gorm_postgres "gorm.io/driver/postgres"
"gorm.io/gorm"
)
func main() {
config, err := opts.ReadOS()
if err != nil {
log.Fatal(fmt.Errorf("read configs: %w", err))
}
err = validator.New().Struct(config)
if err != nil {
log.Fatal(fmt.Errorf("invalid config: %w", err))
}
tgClient, err := client.New(config.AppID, config.AppHash, config.MasterChatID)
if err != nil {
fmt.Println("Can't initiate tg client")
log.Fatal(err)
}
repo, err := repositoryPG(config.DB)
if err != nil {
fmt.Println("Can't initiate postres repo")
log.Fatal(err)
}
updatesHandler, err := bot.NewUpdatesHandler(tgClient, repo, config.MasterChatID, bot.NewIDGenerator())
if err != nil {
fmt.Println("Can't create updates handler")
log.Fatal(err)
}
fmt.Println("Start listener")
err = tgClient.Start(updatesHandler)
if err != nil {
fmt.Println("Listening updates failed")
log.Fatal(err)
}
}
func repositoryPG(config opts.DB) (*postgres.Repository, error) {
dsn := postgresDSN(config.User, config.Password, config.Host, config.Port, config.DBName)
db, err := gorm.Open(gorm_postgres.Open(dsn), &gorm.Config{})
if err != nil {
return nil, fmt.Errorf("connect to posgres: %w", err)
}
return postgres.New(db)
}
func postgresDSN(user, password, host, port, database string) string {
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=require TimeZone=Europe/Kiev", host, user, password, database, port)
return dsn
}