-
Notifications
You must be signed in to change notification settings - Fork 2
/
gateway_server.go
145 lines (121 loc) · 3 KB
/
gateway_server.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
package grpc
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"sync/atomic"
"time"
"contrib.go.opencensus.io/exporter/stackdriver/propagation"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/rs/cors"
"go.opencensus.io/plugin/ocgrpc"
"go.opencensus.io/plugin/ochttp"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type gatewayServer struct {
httpServer *http.Server
listener net.Listener
closed atomic.Bool
}
func (s *gatewayServer) addr() string {
return s.listener.Addr().String()
}
func (s *gatewayServer) listenAndServe() error {
err := s.httpServer.Serve(s.listener)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
}
func (s *gatewayServer) close() error {
if s.closed.Load() {
return nil
}
s.closed.Store(true)
return s.httpServer.Shutdown(context.Background())
}
// nolint: revive // false-positive, it reports tracing as a control flag.
func newGatewayServer(
muxOptions []runtime.ServeMuxOption,
tracing bool,
registerGateway registerGatewayFunc,
address string,
httpRoutes []httpRoute,
middlewares chi.Middlewares,
debugStandardLibraryEndpoints bool,
corsOptions cors.Options,
) (
*gatewayServer,
error,
) {
grpcGatewayMux := runtime.NewServeMux(
muxOptions...,
)
var handler http.Handler = grpcGatewayMux
if tracing {
handler = &ochttp.Handler{
Handler: grpcGatewayMux,
Propagation: &propagation.HTTPFormat{},
}
}
if registerGateway != nil {
dialOptions := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithStatsHandler(&ocgrpc.ClientHandler{}),
}
err := registerGateway(grpcGatewayMux, dialOptions)
if err != nil {
return nil, fmt.Errorf("register gateway: %w", err)
}
}
listener, err := net.Listen("tcp", address)
if err != nil {
return nil, fmt.Errorf("new listener: %w", err)
}
corsHandler := cors.New(corsOptions)
router := chi.NewRouter()
router.Use(middlewares...)
router.Use(corsHandler.Handler)
router.Get(
"/",
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
},
)
router.Mount("/", handler)
for _, route := range httpRoutes {
router.MethodFunc(
route.method,
route.path,
route.handler,
)
}
if debugStandardLibraryEndpoints {
// Register all the standard library debug endpoints.
router.Mount("/debug/", middleware.Profiler())
}
const (
handlerTimeout = 10 * time.Second
readHeaderTimeout = 5 * time.Second
wiggleRoom = 200 * time.Millisecond
readTimeout = handlerTimeout + readHeaderTimeout + wiggleRoom
writeTimeout = handlerTimeout + wiggleRoom
idleTimeout = 2 * time.Minute
)
return &gatewayServer{
httpServer: &http.Server{
Handler: router,
ReadTimeout: readTimeout,
ReadHeaderTimeout: readHeaderTimeout,
WriteTimeout: writeTimeout,
IdleTimeout: idleTimeout,
},
listener: listener,
},
nil
}