/
main.go
114 lines (93 loc) · 2.77 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
package main
import (
"context"
"fmt"
"net/http"
"time"
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"
"github.com/raystack/salt/server"
"go.uber.org/zap"
"google.golang.org/grpc"
)
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)
}
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)
}
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)
}
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()
// 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)
}