-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (92 loc) · 2.59 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
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/sku4/ad-notifier/configs"
"github.com/sku4/ad-notifier/internal/handler/rest"
"github.com/sku4/ad-notifier/internal/handler/watcher"
"github.com/sku4/ad-notifier/internal/repository"
"github.com/sku4/ad-notifier/internal/sender"
"github.com/sku4/ad-notifier/internal/service"
"github.com/sku4/ad-parser/pkg/logger"
"github.com/tarantool/go-tarantool/v2"
"github.com/tarantool/go-tarantool/v2/pool"
)
func main() {
// init config
log := logger.Get()
cfg, err := configs.Init()
if err != nil {
log.Fatalf("error init config: %s", err)
}
// init tarantool
conn, err := pool.Connect(cfg.Tarantool.Servers, tarantool.Opts{
Timeout: cfg.Tarantool.Timeout,
Reconnect: cfg.Tarantool.ReconnectInterval,
RequiredProtocolInfo: tarantool.ProtocolInfo{
Features: []tarantool.ProtocolFeature{tarantool.WatchersFeature},
},
})
if err != nil {
log.Fatalf("error tarantool connection refused: %s", err)
}
defer func() {
errs := conn.Close()
for _, e := range errs {
log.Errorf("error close connection pool: %s", e)
}
}()
// init context
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
ctx = configs.Set(ctx, cfg)
senders, errs := sender.NewSender(ctx)
if errs != nil {
log.Fatalf("error init senders: %s", errors.Join(errs...))
}
repos := repository.NewRepository(conn)
services := service.NewService(repos, senders)
watchers := watcher.NewWatcher(ctx, services)
handlers := rest.NewHandler()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
err = watchers.Register()
if err != nil {
log.Fatalf("error watchers register: %s", err)
}
// run rest server
routes := handlers.InitRoutes()
restServer := rest.NewServer(ctx, routes)
go func() {
log.Info(fmt.Sprintf("Rest server is running on: %d", cfg.Rest.Port))
if errRest := restServer.ListenAndServe(); errRest != nil {
log.Infof("Rest server %s", errRest)
quit <- nil
}
}()
log.Infof("App Started")
// graceful shutdown
log.Infof("Got signal %v, attempting graceful shutdown", <-quit)
cancel()
log.Info("Context is stopped")
err = restServer.Shutdown(ctx)
if err != nil {
log.Errorf("error rest server shutdown: %s", err)
}
err = services.Notifier.Shutdown()
if err != nil {
log.Errorf("error notifier shutdown: %s", err)
} else {
log.Info("Notifier stopped")
}
watchers.Unregister()
errs = conn.CloseGraceful()
for _, e := range errs {
log.Errorf("error close graceful connection pool: %s", e)
}
log.Info("App Shutting Down")
}