forked from raystack/salt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
130 lines (106 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
124
125
126
127
128
129
130
package main
import (
"context"
"fmt"
"net/http"
"time"
"github.com/goto/salt/common"
"github.com/goto/salt/server"
commonv1 "github.com/goto/salt/server/example/proto/gotocompany/common/v1"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"go.uber.org/zap"
"google.golang.org/grpc"
)
var Server = &commonv1.Version{
Version: "v1.0.2",
}
var GRPCMiddlewaresInterceptor = grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
grpc_recovery.UnaryServerInterceptor(),
grpc_ctxtags.UnaryServerInterceptor(),
grpc_prometheus.UnaryServerInterceptor,
grpc_zap.UnaryServerInterceptor(zap.NewExample()),
))
func main() {
grpcPort := 8000
httpPort := 8080
muxPort := 9000
gatewayClientPort := grpcPort
go grpcS(grpcPort)
go httpS(httpPort, gatewayClientPort)
muxS(muxPort)
}
func httpS(httpPort, gatewayClientPort int) {
ctx, cancelFunc := context.WithCancel(server.HandleSignals(context.Background()))
defer cancelFunc()
s, err := server.NewHTTP(server.Config{
Port: httpPort,
})
if err != nil {
panic(err)
}
gw, err := server.NewGateway("", gatewayClientPort)
if err != nil {
panic(err)
}
gw.RegisterHandler(ctx, commonv1.RegisterCommonServiceHandlerFromEndpoint)
s.SetGateway("/api", gw)
s.RegisterHandler("/ping", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "pong")
}))
go s.Serve()
<-ctx.Done()
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), time.Second*10)
defer shutdownCancel()
s.Shutdown(shutdownCtx)
}
func grpcS(grpcPort int) {
ctx, cancelFunc := context.WithCancel(server.HandleSignals(context.Background()))
defer cancelFunc()
s, err := server.NewGRPC(server.Config{
Port: grpcPort,
}, server.WithGRPCServerOptions(GRPCMiddlewaresInterceptor))
if err != nil {
panic(err)
}
s.RegisterService(&commonv1.CommonService_ServiceDesc,
common.New(Server),
)
go s.Serve()
<-ctx.Done()
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), time.Second*10)
defer shutdownCancel()
s.Shutdown(shutdownCtx)
}
func muxS(muxPort int) {
ctx, cancelFunc := context.WithCancel(server.HandleSignals(context.Background()))
defer cancelFunc()
s, err := server.NewMux(server.Config{
Port: muxPort,
}, server.WithMuxGRPCServerOptions(GRPCMiddlewaresInterceptor))
if err != nil {
panic(err)
}
gatewayClientPort := muxPort
gw, err := server.NewGateway("", gatewayClientPort)
if err != nil {
panic(err)
}
gw.RegisterHandler(ctx, commonv1.RegisterCommonServiceHandlerFromEndpoint)
s.SetGateway("/api", gw)
s.RegisterHandler("/ping", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "pong")
}))
s.RegisterService(&commonv1.CommonService_ServiceDesc,
common.New(Server),
)
go s.Serve()
<-ctx.Done()
// clean anything that needs to be closed etc like common server implementation etc
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), time.Second*10)
defer shutdownCancel()
s.Shutdown(shutdownCtx)
}