-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
125 lines (111 loc) · 3.15 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
package main
import (
"context"
cors "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/streadway/amqp"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"google.golang.org/grpc"
"log"
"pixstall-user/app/file/repo"
"pixstall-user/app/middleware"
"strings"
"time"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
//MongoDB
dbClient, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
panic(err)
}
defer cancel()
defer func() {
if err = dbClient.Disconnect(ctx); err != nil {
panic(err)
}
}()
db := dbClient.Database("pixstall-user")
//RabbitMQ
rabbitmqConn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
log.Fatalf("Failed to connect to RabbitMQ %v", err)
}
defer rabbitmqConn.Close()
ch, err := rabbitmqConn.Channel()
if err != nil {
log.Fatalf("Failed to create channel %v", err)
}
err = ch.ExchangeDeclare(
"user",
"topic",
true,
false,
false,
false,
nil,
)
if err != nil {
log.Fatalf("Failed to create exchange %v", err)
}
commMsgBroker := InitCommissionMessageBroker(db, rabbitmqConn)
go commMsgBroker.StartCommUsersValidateQueue()
defer commMsgBroker.StopAllQueue()
// gRPC - Auth
authGRPCConn, err := grpc.Dial("localhost:50051", grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatal(err)
}
defer authGRPCConn.Close()
//gRPC - File
fileGRPCConn, err := grpc.Dial("localhost:50052", grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatal(err)
}
defer fileGRPCConn.Close()
r := gin.Default()
//r.Use(cors.Default())
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "POST", "PUT", "PATCH"},
AllowHeaders: []string{"Origin", "Content-Type", "Access-Control-Allow-Origin", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
AllowWildcard: true,
AllowFiles: true,
MaxAge: 12 * time.Hour,
}))
apiGroup := r.Group("/api")
authGroup := apiGroup.Group("/auth")
{
ctr := InitAuthController(authGRPCConn, db)
authGroup.GET("/url", ctr.GetAuthURL)
authGroup.GET("/callback", ctr.AuthCallback)
}
regGroup := apiGroup.Group("/reg")
{
authIDExtractor := middleware.NewJWTPayloadsExtractor([]string{"authId"})
ctr := InitRegController(authGRPCConn, (*repo.FileGRPCClientConn)(fileGRPCConn), db, ch, rabbitmqConn)
regGroup.POST("", authIDExtractor.ExtractPayloadsFromJWT, ctr.Registration)
}
userGroup := apiGroup.Group("/users")
{
userIDExtractor := middleware.NewJWTPayloadsExtractor([]string{"userId"})
ctr := InitUserController(authGRPCConn, (*repo.FileGRPCClientConn)(fileGRPCConn), db, rabbitmqConn)
userGroup.GET("/:id", func(c *gin.Context) {
if strings.HasSuffix(c.Request.RequestURI, "/me") {
userIDExtractor.ExtractPayloadsFromJWT(c)
if c.IsAborted() {
return
}
ctr.GetUserDetails(c)
return
}
ctr.GetUser(c)
})
userGroup.PATCH("/:id", userIDExtractor.ExtractPayloadsFromJWT, ctr.UpdateUser)
}
err = r.Run(":9001")
print(err)
}