-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
150 lines (121 loc) · 4.35 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
gatewaygRPCClient "dineflow-api-gateway/client_grpc"
restClient "dineflow-api-gateway/client_rest"
"dineflow-api-gateway/configs"
"dineflow-api-gateway/controllers"
gatewayHTTPHandler "dineflow-api-gateway/http_handler"
pb "dineflow-api-gateway/pb"
"dineflow-api-gateway/routes"
"dineflow-api-gateway/services"
)
var (
server *gin.Engine
ctx context.Context
mongoclient *mongo.Client
userService services.UserService
UserController controllers.UserController
UserRouteController routes.UserRouteController
authCollection *mongo.Collection
authService services.AuthService
AuthController controllers.AuthController
AuthRouteController routes.AuthRouteController
)
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
log.Println("Handling CORS :D")
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
return
}
c.Next()
}
}
func init() {
ctx = context.TODO()
// Connect to MongoDB
mongoconn := options.Client().ApplyURI(configs.EnvMongoURI())
mongoclient, err := mongo.Connect(ctx, mongoconn)
if err != nil {
panic(err)
}
if err := mongoclient.Ping(ctx, readpref.Primary()); err != nil {
panic(err)
}
fmt.Println("MongoDB successfully connected...")
// Collections
authCollection = mongoclient.Database("user_db").Collection("users")
userService = services.NewUserServiceImpl(authCollection, ctx)
authService = services.NewAuthService(authCollection, ctx)
AuthController = controllers.NewAuthController(authService, userService, ctx, authCollection)
AuthRouteController = routes.NewAuthRouteController(AuthController)
UserController = controllers.NewUserController(userService)
UserRouteController = routes.NewRouteUserController(UserController)
server = gin.Default()
}
func main() {
defer mongoclient.Disconnect(ctx)
server.Use(CORSMiddleware())
// Use the main engine instance directly
router := server.Group("/api")
AuthRouteController.AuthRoute(router, userService)
UserRouteController.UserRoute(router, userService)
flag.Parse()
defer gracefulShutdown()
// Initialize gRPC connections
ordergRPCConn := initOrdergRPCConnection()
ordergRPCClienter := pb.NewOrderServiceClient(ordergRPCConn)
defer ordergRPCConn.Close()
// Dependency Injection
ordergRPCClient := gatewaygRPCClient.ProvideOrderClient(&ordergRPCClienter)
orderHandler := gatewayHTTPHandler.ProvideOrderHandler(ordergRPCClient)
reviewClientRest := restClient.ProvideReviewClientRest(&http.Client{})
reviewHandler := gatewayHTTPHandler.ProvideReviewHandler(reviewClientRest)
notificationClientRest := restClient.ProvideNotificationClientRest(&http.Client{})
notificationHandler := gatewayHTTPHandler.ProvideNotificationHandler(notificationClientRest)
menuClientRest := restClient.ProvideMenuClientRest(&http.Client{})
menuHandler := gatewayHTTPHandler.ProvideMenuHandler(menuClientRest)
// Use the main engine instance directly
gatewayHTTPHandler.ProvideRouter(server,
userService,
orderHandler,
reviewHandler,
notificationHandler,
menuHandler,
)
log.Fatal(server.Run(":" + configs.EnvPort()))
}
func initOrdergRPCConnection() *grpc.ClientConn {
dest := fmt.Sprintf(configs.EnvOrderServiceHost()+":%s", configs.EnvOrderServicePort())
// Set up a connection to the server.
conn, err := grpc.Dial(dest, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
log.Default().Println("Connected to Order gRPC Service")
return conn
}
func gracefulShutdown() {
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
fmt.Println("Shutting down server...")
}