forked from ZAF07/tigerlily-e-bakery-inventories
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (105 loc) · 3.65 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
// TODO: Sort out the configuratiion for inventory service. Refer to tigerlily repo for example of what we need
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
"strings"
"github.com/Tiger-Coders/tigerlily-inventories/api/router"
"github.com/Tiger-Coders/tigerlily-inventories/api/rpc"
"github.com/Tiger-Coders/tigerlily-inventories/internal/db"
"github.com/Tiger-Coders/tigerlily-inventories/internal/pkg/env"
"github.com/Tiger-Coders/tigerlily-inventories/internal/pkg/injection"
"github.com/Tiger-Coders/tigerlily-inventories/internal/pkg/logger"
"github.com/Tiger-Coders/tigerlily-inventories/internal/service/inventory"
"github.com/gin-gonic/gin"
"github.com/soheilhy/cmux"
"google.golang.org/grpc"
)
// func main() {
// logs := logger.NewLogger()
// logs.InfoLogger.Println("Starting up server ...")
// // Set ENV vars
// env.SetEnv()
// l, err := net.Listen("tcp", ":8000")
// if err != nil {
// logs.ErrorLogger.Println("Something went wrong in the server startup")
// log.Fatalf("Error connecting tcp port 8000")
// }
// logs.InfoLogger.Println("Successfull server init")
// h := gin.Default()
// h.Use(middleware.CORSMiddleware())
// // Set CORS config
// h.Use(cors.New(cors.Config{
// AllowCredentials: false,
// AllowWildcard: true,
// // AllowAllOrigins: true,
// AllowOrigins: []string{"http://localhost:3000"},
// AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTION", "HEAD", "PATCH", "COMMON"},
// AllowHeaders: []string{"Content-Type", "Content-Length", "Authorization", "accept", "origin", "Referer", "User-Agent"},
// }))
// router.Router(h)
// s := &http.Server{
// Handler: h,
// }
// s.Serve(l)
// }
func main() {
logs := logger.NewLogger()
logs.InfoLogger.Println("Starting up server ...")
appConfig := injection.GetAppConfig()
fmt.Println(appConfig)
if !appConfig.IsConfigFileProvided {
logs.InfoLogger.Println("Starting app with env...")
// Set ENV vars
fmt.Println("No config file detacted. Setting env...")
env.SetEnv()
}
// Spin up the main server instance
var port = flag.String("port", ":8000", "Port to listen on")
if appConfig.IsConfigFileProvided {
inventoryPort := fmt.Sprintf(":%s", appConfig.ServicePort)
port = &inventoryPort
}
lis, err := net.Listen("tcp", *port)
if err != nil {
logs.ErrorLogger.Printf("Something went wrong in the server startup: %+v", err)
log.Fatalf("Error connecting tcp port %s", *port)
}
logs.InfoLogger.Println("Successfull server init")
// Start a new multiplexer passing in the main server
m := cmux.New(lis)
// Listen for HTTP requests first
// If request headers don't specify HTTP, next mux would handle the request
httpListener := m.Match(cmux.HTTP1Fast())
grpclistener := m.Match(cmux.Any())
// Run GO routine to run both servers at diff processes at the same time
go serveGRPC(grpclistener)
go serveHTTP(httpListener)
fmt.Printf("Inventory Service Running@%v\n", lis.Addr())
if err := m.Serve(); !strings.Contains(err.Error(), "use of closed network connection") {
log.Fatalf("MUX ERR : %+v", err)
}
}
// GRPC Server initialisation
func serveGRPC(l net.Listener) {
grpcServer := grpc.NewServer()
// Register GRPC stubs (pass the GRPCServer and the initialisation of the service layer)
rpc.RegisterInventoryServiceServer(grpcServer, inventory.NewInventoryService(db.NewDB()))
if err := grpcServer.Serve(l); err != nil {
log.Fatalf("error running GRPC server %+v", err)
}
}
// HTTP Server initialisation (using gin)
func serveHTTP(l net.Listener) {
h := gin.Default()
router.Router(h)
s := &http.Server{
Handler: h,
}
if err := s.Serve(l); err != cmux.ErrListenerClosed {
log.Fatalf("error serving HTTP : %+v", err)
}
}