-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (99 loc) · 3.24 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
package main
import (
"context"
"fmt"
"net"
"time"
"github.com/ArtyomArtamonov/msg/internal/model"
"github.com/ArtyomArtamonov/msg/internal/repository"
"github.com/ArtyomArtamonov/msg/internal/server"
"github.com/ArtyomArtamonov/msg/internal/service"
"github.com/jmoiron/sqlx"
"github.com/streadway/amqp"
pb "github.com/ArtyomArtamonov/msg/internal/server/msg-proto"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
func main() {
logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetLevel(logrus.TraceLevel)
err := godotenv.Load("../../.env")
failOnError(err, "Error loading .env file")
env := server.NewEnv()
host := env.API_HOST
lis, err := net.Listen("tcp", host)
failOnError(err, "could not create tcp connection")
connectionString := fmt.Sprintf(
"host=database port=5432 sslmode=disable dbname=%s user=%s password=%s",
env.POSTGRES_DB,
env.POSTGRES_USER,
env.POSTGRES_PASSWORD,
)
db := sqlx.MustOpen("postgres", connectionString)
err = db.Ping()
failOnError(err, "could not ping database")
defer db.Close()
conn, err := amqp.Dial(fmt.Sprintf("amqp://%s:%s@message-broker:5672/",
env.RABBITMQ_DEFAULT_USER,
env.RABBITMQ_DEFAULT_PASS))
failOnError(err, "could not connect to message-broker")
ch, err := conn.Channel()
failOnError(err, "could not create channel (message-broker)")
defer ch.Close()
grpcServer := createAndPrepareGRPCServer(db, ch, env)
logrus.Info("Starting grpc server on ", host)
if err := grpcServer.Serve(lis); err != nil {
logrus.Fatal(err.Error())
}
}
func createAndPrepareGRPCServer(db *sqlx.DB, ch *amqp.Channel, env *server.Env) *grpc.Server {
endpoints := server.NewEndpoints()
endpointRoles := server.NewEndpointRoles(endpoints)
// AUTH
userStore := repository.NewPostgresUserStore(db)
refreshTokenStore := repository.NewRefreshTokenPostgresStore(db)
// DEBUG PURPOSE BLOCK
{
user, err := model.NewUser("user", "user", model.USER_ROLE)
if err != nil {
logrus.Error(err)
}
if err := userStore.Save(context.TODO(), user); err != nil {
logrus.Error(err)
}
admin, err := model.NewUser("admin", "admin", model.ADMIN_ROLE)
if err != nil {
logrus.Error(err)
}
if err := userStore.Save(context.TODO(), admin); err != nil {
logrus.Error(err)
}
}
jwtManager := service.NewJWTManager(
env.JWT_SECRET,
time.Minute*time.Duration(env.JWT_DURATION_MIN),
time.Hour*24*time.Duration(env.REFRESH_DURATION_DAYS),
)
authServer := server.NewAuthServer(userStore, refreshTokenStore, jwtManager)
authInterceptor := server.NewAuthInterceptor(jwtManager, endpointRoles)
// API
amqpManager := service.NewRabbitMQManager(ch)
roomStore := repository.NewPostgresRoomStore(db)
messageStore := repository.NewPostgresMessageStore(db)
apiServer := server.NewApiServer(jwtManager, roomStore, messageStore, amqpManager)
grpcServer := grpc.NewServer(
grpc.UnaryInterceptor(authInterceptor.Unary()),
grpc.StreamInterceptor(authInterceptor.Stream()),
)
pb.RegisterAuthServiceServer(grpcServer, authServer)
pb.RegisterApiServiceServer(grpcServer, apiServer)
reflection.Register(grpcServer)
return grpcServer
}
func failOnError(err error, text string) {
if err != nil {
logrus.Fatalf("%s: %v", text, err)
}
}