-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
154 lines (130 loc) · 4.08 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
150
151
152
153
154
package main
import (
"context"
_ "embed"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/go-redis/redis/v8"
"github.com/hibiken/asynq"
"github.com/rs/zerolog/log"
"github.com/sirjager/gopkg/cache"
dbPkg "github.com/sirjager/gopkg/db"
"github.com/sirjager/gopkg/mail"
"github.com/sirjager/trueauth/internal/tokens"
"github.com/swaggo/swag/example/basic/docs"
"golang.org/x/sync/errgroup"
"github.com/sirjager/trueauth/cmd/gateway"
"github.com/sirjager/trueauth/cmd/grpc"
"github.com/sirjager/trueauth/config"
"github.com/sirjager/trueauth/db/db"
"github.com/sirjager/trueauth/logger"
"github.com/sirjager/trueauth/migrations"
"github.com/sirjager/trueauth/internal/hash"
"github.com/sirjager/trueauth/server"
"github.com/sirjager/trueauth/worker"
)
var startTime time.Time
// NOTE: Listenting to thse signals for gracefull shutdown
var interuptSignals = []os.Signal{
os.Interrupt,
syscall.SIGTERM,
syscall.SIGINT,
}
func init() {
startTime = time.Now()
}
func main() {
// NOTE: change name of .env file here. For defaults, use "defaults"
config, err := config.LoadConfigs(".", "defaults", startTime)
if err != nil {
log.Fatal().Err(err).Msg("failed to load configurations")
}
docs.SwaggerInfo.Host = fmt.Sprintf("%s:%d", config.Host, config.RestPort)
logger, err := logger.NewLogger(config.Logger)
if err != nil {
log.Fatal().Err(err).Msg("failed to initialize logger")
}
defer logger.Close()
ctx, stop := signal.NotifyContext(context.Background(), interuptSignals...)
defer stop()
wg, ctx := errgroup.WithContext(ctx)
// initialize database
database, conn, err := dbPkg.NewDatabae(ctx, config.Database, logger.Logr)
if err != nil {
logger.Logr.Fatal().Err(err).Msg("failed to create new database instance")
}
defer database.Close()
// migrate database to latest version
if err = database.MigrateUsingBindata(migrations.AssetNames(), migrations.Asset); err != nil {
logger.Logr.Fatal().Err(err).Msg("failed to migrate database")
}
// initialize mailer for sending emails
mailer, err := mail.NewGmailSender(config.Mail)
if err != nil {
logger.Logr.Fatal().Err(err).Msg("failed to initialize gmail smtp")
}
// initialize store for database operations
store := db.NewStore(conn)
rOpts, pErr := redis.ParseURL(config.Database.RedisURL)
if pErr != nil {
logger.Logr.Fatal().Err(pErr).Msg("failed to parse redis url")
}
redisOpt := asynq.RedisClientOpt{
Addr: rOpts.Addr,
Password: rOpts.Password,
Username: rOpts.Username,
Network: rOpts.Network,
TLSConfig: rOpts.TLSConfig,
DB: rOpts.DB,
PoolSize: rOpts.PoolSize,
}
tasks := worker.NewRedisTaskDistributor(logger.Logr, redisOpt)
defer tasks.Shutdown()
// redis client for cache system
redisClient := redis.NewClient(rOpts)
if pingErr := redisClient.Ping(ctx).Err(); pingErr != nil {
logger.Logr.Fatal().Err(pingErr).Msg("failed to ping redis client")
}
defer redisClient.Close()
cache := cache.NewCacheRedis(redisClient, logger.Logr)
// initialize token builder for token generation
tokens, err := tokens.NewPasetoBuilder(config.Auth.Secret)
if err != nil {
logger.Logr.Fatal().Err(err).Msg("failed to create token builder")
}
hasher := hash.NewBryptHash()
adapters := &server.Adapters{
Cache: cache,
Logr: logger.Logr,
Store: store,
Tasks: tasks,
Mail: mailer,
Hash: hasher,
Tokens: tokens,
Config: config,
}
// initialize server
srvr, err := server.New(adapters)
if err != nil {
logger.Logr.Fatal().Err(err).Msg("failed to initialize server")
}
// start task processor to process tasks in background
worker.RunTaskProcessor(ctx, wg, logger.Logr, store, mailer, config, redisOpt)
// start rest server if port is not empty
if config.RestPort != 0 {
address := fmt.Sprintf("%s:%d", config.Host, config.RestPort)
gateway.StartServer(ctx, wg, address, srvr, config)
}
// start grpc server if port is not empty
if config.GrpcPort != 0 {
address := fmt.Sprintf("%s:%d", config.Host, config.GrpcPort)
grpc.RunServer(ctx, wg, address, srvr)
}
err = wg.Wait()
if err != nil {
logger.Logr.Fatal().Err(err).Msg("error from wait group")
}
}