-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (93 loc) · 3.05 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
package main
import (
"context"
"fmt"
"game-app/Validator/matchingvalidator"
"game-app/Validator/uservalidator"
presenceClient "game-app/adapter/presence"
"game-app/adapter/redis"
"game-app/config"
"game-app/delivery/httpserver"
"game-app/repository/migrator"
"game-app/repository/mysql"
"game-app/repository/mysql/mysqlaccesscontrol"
"game-app/repository/mysql/mysqluser"
"game-app/repository/redis/redismatching"
"game-app/repository/redis/redispresence"
"game-app/scheduler"
"game-app/service/authorizationservice"
"game-app/service/authservice"
"game-app/service/backofficeuserservice"
"game-app/service/matchingservice"
"game-app/service/presenceservice"
"game-app/service/userservice"
_ "google.golang.org/grpc"
"os"
"os/signal"
"sync"
"time"
)
const (
JwtSignKey = "jwt_secret"
)
func main() {
cfg := config.Load("config.yml")
fmt.Printf("cfg: %+v\n", cfg)
mgr := migrator.New(cfg.Mysql)
//TODO - add command for migrations
mgr.Up()
//TODO - add struct and add these returned items as struct field
authSvc, userSvc, userValidator, backofficeSvc, authorizationSvc,
matchingSvc, matchingV, presencSvc := SetupServices(cfg)
server := httpserver.New(cfg, authSvc, userSvc, userValidator,
backofficeSvc, authorizationSvc, matchingSvc, matchingV, presencSvc)
go func() {
server.Serve()
}()
done := make(chan bool)
var wg sync.WaitGroup
go func() {
sch := scheduler.New(cfg.Scheduler, matchingSvc)
wg.Add(1)
sch.Start(done, &wg)
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
ctx := context.Background()
ctxWithTimeout, cancel := context.WithTimeout(ctx,
cfg.Application.GracefulShutdownTimeout)
defer cancel()
if err := server.Router.Shutdown(ctxWithTimeout); err != nil {
fmt.Println("http server shutdown error: ", err)
}
fmt.Println("received interrupt signal, shutting down gracefully...")
done <- true
time.Sleep(cfg.Application.GracefulShutdownTimeout)
<-ctxWithTimeout.Done()
wg.Wait()
}
func SetupServices(cfg config.Config) (
authservice.Service, userservice.Service, uservalidator.Validator,
backofficeuserservice.Service, authorizationservice.Service,
matchingservice.Service, matchingvalidator.Validator,
presenceservice.Service,
) {
authSvc := authservice.New(cfg.Auth)
MysqlRepo := mysql.New(cfg.Mysql)
userMysql := mysqluser.New(MysqlRepo)
userSvc := userservice.New(userMysql, authSvc)
backofficeUserSvc := backofficeuserservice.New()
aclMysql := mysqlaccesscontrol.New(MysqlRepo)
authorizationSvc := authorizationservice.New(aclMysql)
uV := uservalidator.New(userMysql)
matchingV := matchingvalidator.New()
redisAdapter := redis.New(cfg.Redis)
matchingRepo := redismatching.New(redisAdapter)
presenceRepo := redispresence.New(redisAdapter)
presenceSvc := presenceservice.New(cfg.PresenceService, presenceRepo)
presenceAdapter := presenceClient.New(":8086")
matchingSvc := matchingservice.New(cfg.MatchingService, matchingRepo, presenceAdapter, redisAdapter)
return authSvc, userSvc, uV, backofficeUserSvc,
authorizationSvc, matchingSvc, matchingV, presenceSvc
}