-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
149 lines (134 loc) · 4.02 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/pretcat/ugc_test_task/repositories"
"github.com/pretcat/ugc_test_task/config"
"github.com/pretcat/ugc_test_task/http"
"github.com/pretcat/ugc_test_task/logger"
buildmng "github.com/pretcat/ugc_test_task/managers/buildings"
categmng "github.com/pretcat/ugc_test_task/managers/categories"
companmng "github.com/pretcat/ugc_test_task/managers/companies"
"github.com/pretcat/ugc_test_task/pg"
buildrepos "github.com/pretcat/ugc_test_task/repositories/buildings"
categrepos "github.com/pretcat/ugc_test_task/repositories/categories"
companrepos "github.com/pretcat/ugc_test_task/repositories/companies"
)
var (
conf config.Config
pgClient pg.Client
categoryRepos categrepos.Repository
companyRepos companrepos.Repository
buildingRepos buildrepos.Repository
companyMng companmng.Manager
buildingMng buildmng.Manager
categoryMng categmng.Manager
httpApi *http.Api
shutdownServiceTimeout = 5 * time.Second
)
func main() {
var err error
conf, err = config.New()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error while creating config: %v\n", err)
os.Exit(1)
}
if err := initLogger(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error while init logger: %v\n", err)
os.Exit(1)
}
if err := initRepositories(); err != nil {
logger.Msg("error while init repositories").Error(err.Error())
os.Exit(1)
}
initManagers()
httpApi = http.NewApi(http.Config{
Host: conf.HttpServer.Host,
Port: conf.HttpServer.Port,
MetricsPort: conf.HttpServer.MetricsPort,
DebugPort: conf.HttpServer.DebugPort,
ReadTimeout: conf.HttpServer.ReadTimeout,
ReadHeaderTimeout: conf.HttpServer.ReadHeaderTimeout,
WriteTimeout: conf.HttpServer.WriteTimeout,
IdleTimeout: conf.HttpServer.IdleTimeout,
MaxHeaderBytes: conf.HttpServer.MaxHeaderBytes,
CompanyManager: companyMng,
BuildingManager: buildingMng,
CategoryManager: categoryMng,
})
httpApi.Start(func(err error) {
logger.Msg("error while start http api").Error(err.Error())
shutdownService()
os.Exit(1)
})
handleOsSignals()
}
func initLogger() (err error) {
return logger.Init(logger.Config{
Path: conf.Logger.Path,
Stdout: conf.Logger.Stdout,
Stderr: conf.Logger.Stderr,
Level: logger.LevelFromString(conf.Logger.Level),
})
}
func initRepositories() (err error) {
pgConfig := pg.Config{
Host: conf.Pg.Host,
Port: conf.Pg.Port,
User: conf.Pg.User,
Password: conf.Pg.Password,
}
pgClient, err = repositories.InitPgClient(pgConfig)
if err != nil {
return fmt.Errorf("init pg client: %v", err)
}
buildingRepos, err = buildrepos.New(pgClient)
if err != nil {
return fmt.Errorf("init 'building' repository: %v", err)
}
categoryRepos, err = categrepos.New(pgClient)
if err != nil {
return fmt.Errorf("init 'category' repository: %v", err)
}
companyRepos, err = companrepos.New(pgClient, categoryRepos)
if err != nil {
return fmt.Errorf("init 'company' repository: %v", err)
}
return nil
}
func initManagers() {
companyMng = companmng.New(companyRepos)
buildingMng = buildmng.New(buildingRepos)
categoryMng = categmng.New(categoryRepos)
}
func handleOsSignals() {
osSignals := make(chan os.Signal)
defer close(osSignals)
signal.Notify(osSignals, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
for {
<-osSignals
shutdownService()
break
}
}
func shutdownService() {
ctx, cancel := context.WithTimeout(context.Background(), shutdownServiceTimeout)
defer cancel()
httpApi.Shutdown(ctx)
if err := buildingRepos.Stop(ctx); err != nil {
logger.Msg("shutdown 'building' repository").Error(err.Error())
}
logger.Info("shutdown 'building' repository")
if err := categoryRepos.Stop(ctx); err != nil {
logger.Msg("shutdown 'category' repository").Error(err.Error())
}
logger.Info("shutdown 'category' repository")
if err := companyRepos.Stop(ctx); err != nil {
logger.Msg("shutdown 'company' repository").Error(err.Error())
}
logger.Info("shutdown 'company' repository")
}