-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
187 lines (146 loc) · 4.9 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"context"
"database/sql"
"errors"
"net"
"net/http"
"os"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/hibiken/asynq"
_ "github.com/jackc/pgx/v5/stdlib"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"google.golang.org/protobuf/encoding/protojson"
"github.com/ahmedkhaeld/simplebank/api"
db "github.com/ahmedkhaeld/simplebank/db/sqlc"
"github.com/ahmedkhaeld/simplebank/gapi"
"github.com/ahmedkhaeld/simplebank/mail"
"github.com/ahmedkhaeld/simplebank/pb"
"github.com/ahmedkhaeld/simplebank/tasks"
"github.com/ahmedkhaeld/simplebank/util"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/rs/zerolog"
zlog "github.com/rs/zerolog/log"
)
var Env = util.Env{}
func init() {
var err error
Env, err = util.LoadEnv(".")
if err != nil {
zlog.Fatal().Err(err).Msg("Failed to load environment")
}
}
func main() {
conn, err := sql.Open(Env.DBDriver, Env.DBSource)
if err != nil {
zlog.Fatal().Err(err).Msg("Failed to open database connection")
}
if Env.Environment == "development" {
zlog.Logger = zlog.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
runDBMigration(Env.MigrationURL, Env.DBSource)
store := db.NewStore(conn)
redisOpt := asynq.RedisClientOpt{
Addr: Env.RedisAddress,
}
taskClient := tasks.PushNewTask(redisOpt)
go runTaskWorker(Env, redisOpt, store)
go runGatewayServer(Env, store, taskClient)
runGrpcServer(Env, store, taskClient)
}
func runDBMigration(migrationURL string, dbSource string) {
migration, err := migrate.New(migrationURL, dbSource)
if err != nil {
zlog.Fatal().Err(err).Msg("can't create migration instance")
}
if err = migration.Up(); err != nil && err != migrate.ErrNoChange {
zlog.Fatal().Err(err).Msg("can't migrate database up")
}
zlog.Info().Msg("db migrated successfully")
}
func runGrpcServer(env util.Env, store db.Store, taskClient tasks.QueueClient) {
//create and initialize a new gRPC server
server, err := gapi.NewServer(env, store, taskClient)
if err != nil {
zlog.Fatal().Err(err).Msg("failed to initialize gRPC server")
}
gprcLogger := grpc.UnaryInterceptor(gapi.GrpcLogger)
grpcServer := grpc.NewServer(gprcLogger)
//Register SimpleBank server with the gRPC server
pb.RegisterSimpleBankServer(grpcServer, server)
// allow clients to introspect the server services and methods
reflection.Register(grpcServer)
listener, err := net.Listen("tcp", env.GRPCServerAddress)
if err != nil {
zlog.Fatal().Err(err).Msg("cannot create listener")
}
zlog.Info().Msgf("start gRPC server at %s", listener.Addr().String())
err = grpcServer.Serve(listener)
if err != nil {
if errors.Is(err, grpc.ErrServerStopped) {
zlog.Fatal().Err(err).Msg("server stopped")
}
zlog.Fatal().Err(err).Msg("cannot serve gRPC server")
}
}
func runGatewayServer(env util.Env, store db.Store, taskClient tasks.QueueClient) {
//create and initialize a new gRPC server
server, err := gapi.NewServer(env, store, taskClient)
if err != nil {
zlog.Fatal().Err(err).Msg("cannot create server")
}
// jsonOptions to enable snake case for fields of the proto messages for the grpc-gateway server
jsonOptions := runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{
UseProtoNames: true,
},
UnmarshalOptions: protojson.UnmarshalOptions{
DiscardUnknown: true,
},
})
grpcMux := runtime.NewServeMux(jsonOptions)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err = pb.RegisterSimpleBankHandlerServer(ctx, grpcMux, server)
if err != nil {
zlog.Fatal().Err(err).Msg("cannot register simple bank handler")
}
mux := http.NewServeMux()
mux.Handle("/", grpcMux)
listener, err := net.Listen("tcp", env.HTTPServerAddress)
if err != nil {
zlog.Fatal().Err(err).Msg("cannot create listener")
}
zlog.Info().Msgf("start HTTP gateway server at %s", listener.Addr().String())
handler := gapi.HttpLogger(mux)
err = http.Serve(listener, handler)
if err != nil {
if errors.Is(err, http.ErrServerClosed) {
zlog.Fatal().Err(err).Msg("server stopped")
}
zlog.Fatal().Err(err).Msg("cannot serve HTTP gateway server")
}
}
func runGinServer(env util.Env, store db.Store) {
server, err := api.NewServer(env, store)
if err != nil {
zlog.Fatal().Err(err).Msg("cannot create server")
}
err = server.Start(Env.HTTPServerAddress)
if err != nil {
zlog.Fatal().Err(err).Msg("cannot start server")
}
zlog.Info().Msg("HTTP server started")
}
func runTaskWorker(env util.Env, redisOpt asynq.RedisClientOpt, store db.Store) {
mailer := mail.NewGmail(env.EmailSenderName, env.EmailSenderAddress, env.EmailSenderPassword)
taskWorker := tasks.PullNewTask(redisOpt, store, mailer)
zlog.Info().Msg("Start task worker")
err := taskWorker.Start()
if err != nil {
zlog.Fatal().Err(err).Msg("failed to start task worker")
}
}