/
genericapiserver.go
249 lines (196 loc) · 6.85 KB
/
genericapiserver.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2020 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package server
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/bolo-tourism/go/pkg/version"
"github.com/bolo-tourism/go/pkg/core"
"github.com/bolo-tourism/go/pkg/log"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
ginprometheus "github.com/zsais/go-gin-prometheus"
"golang.org/x/sync/errgroup"
"github.com/bolo-tourism/go/bolo/pkg/middleware"
)
// GenericAPIServer contains state for an iam api server.
// type GenericAPIServer gin.Engine.
type GenericAPIServer struct {
middlewares []string
// // SecureServingInfo holds configuration of the TLS server.
// SecureServingInfo *SecureServingInfo
// InsecureServingInfo holds configuration of the insecure HTTP server.
InsecureServingInfo *InsecureServingInfo
// ShutdownTimeout is the timeout used for server shutdown. This specifies the timeout before server
// gracefully shutdown returns.
ShutdownTimeout time.Duration
*gin.Engine
healthz bool
enableMetrics bool
enableProfiling bool
// wrapper for gin.Engine
insecureServer, secureServer *http.Server
}
func initGenericAPIServer(s *GenericAPIServer) {
// do some setup
// s.GET(path, ginSwagger.WrapHandler(swaggerFiles.Handler))
s.Setup()
s.InstallMiddlewares()
s.InstallAPIs()
}
// Setup do some setup work for gin engine.
func (s *GenericAPIServer) Setup() {
gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
log.Infof("%-6s %-s --> %s (%d handlers)", httpMethod, absolutePath, handlerName, nuHandlers)
}
}
// InstallMiddlewares install generic middlewares.
func (s *GenericAPIServer) InstallMiddlewares() {
// necessary middlewares
s.Use(middleware.RequestID())
s.Use(middleware.Context())
// install custom middlewares
for _, m := range s.middlewares {
mw, ok := middleware.Middlewares[m]
if !ok {
log.Warnf("can not find middleware: %s", m)
continue
}
log.Infof("install middleware: %s", m)
s.Use(mw)
}
}
// InstallAPIs install generic apis.
func (s *GenericAPIServer) InstallAPIs() {
// install healthz handler
if s.healthz {
s.GET("/healthz", func(c *gin.Context) {
core.WriteResponse(c, nil, map[string]string{"status": "ok"})
})
}
// install metric handler
if s.enableMetrics {
prometheus := ginprometheus.NewPrometheus("gin")
prometheus.Use(s.Engine)
}
// install pprof handler
if s.enableProfiling {
pprof.Register(s.Engine)
}
s.GET("/version", func(c *gin.Context) {
core.WriteResponse(c, nil, version.Get())
})
}
/*
// preparedGenericAPIServer is a private wrapper that enforces a call of PrepareRun() before Run can be invoked.
type preparedGenericAPIServer struct {
*GenericAPIServer
}
func (s *GenericAPIServer) PrepareRun() preparedGenericAPIServer {
return preparedGenericAPIServer{s}
}
*/
// Run spawns the http server. It only returns when the port cannot be listened on initially.
func (s *GenericAPIServer) Run() error {
// For scalability, use custom HTTP configuration mode here
s.insecureServer = &http.Server{
Addr: s.InsecureServingInfo.Address,
Handler: s,
// ReadTimeout: 10 * time.Second,
// WriteTimeout: 10 * time.Second,
// MaxHeaderBytes: 1 << 20,
}
// For scalability, use custom HTTP configuration mode here
// s.secureServer = &http.Server{
// Addr: s.SecureServingInfo.Address(),
// Handler: s,
// // ReadTimeout: 10 * time.Second,
// // WriteTimeout: 10 * time.Second,
// // MaxHeaderBytes: 1 << 20,
// }
var eg errgroup.Group
// Initializing the server in a goroutine so that
// it won't block the graceful shutdown handling below
eg.Go(func() error {
log.Infof("Start to listening the incoming requests on http address: %s", s.InsecureServingInfo.Address)
if err := s.insecureServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err.Error())
return err
}
log.Infof("Server on %s stopped", s.InsecureServingInfo.Address)
return nil
})
// eg.Go(func() error {
// key, cert := s.SecureServingInfo.CertKey.KeyFile, s.SecureServingInfo.CertKey.CertFile
// if cert == "" || key == "" || s.SecureServingInfo.BindPort == 0 {
// return nil
// }
// log.Infof("Start to listening the incoming requests on https address: %s", s.SecureServingInfo.Address())
// if err := s.secureServer.ListenAndServeTLS(cert, key); err != nil && !errors.Is(err, http.ErrServerClosed) {
// log.Fatal(err.Error())
// return err
// }
// log.Infof("Server on %s stopped", s.SecureServingInfo.Address())
// return nil
// })
// Ping the server to make sure the router is working.
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if s.healthz {
if err := s.ping(ctx); err != nil {
return err
}
}
if err := eg.Wait(); err != nil {
log.Fatal(err.Error())
}
return nil
}
// Close graceful shutdown the api server.
func (s *GenericAPIServer) Close() {
// The context is used to inform the server it has 10 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := s.secureServer.Shutdown(ctx); err != nil {
log.Warnf("Shutdown secure server failed: %s", err.Error())
}
if err := s.insecureServer.Shutdown(ctx); err != nil {
log.Warnf("Shutdown insecure server failed: %s", err.Error())
}
}
// ping pings the http server to make sure the router is working.
func (s *GenericAPIServer) ping(ctx context.Context) error {
url := fmt.Sprintf("http://%s/healthz", s.InsecureServingInfo.Address)
if strings.Contains(s.InsecureServingInfo.Address, "0.0.0.0") {
url = fmt.Sprintf("http://127.0.0.1:%s/healthz", strings.Split(s.InsecureServingInfo.Address, ":")[1])
}
for {
// Change NewRequest to NewRequestWithContext and pass context it
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return err
}
// Ping the server by sending a GET request to `/healthz`.
resp, err := http.DefaultClient.Do(req)
if err == nil && resp.StatusCode == http.StatusOK {
log.Info("The router has been deployed successfully.")
resp.Body.Close()
return nil
}
// Sleep for a second to continue the next ping.
log.Info("Waiting for the router, retry in 1 second.")
time.Sleep(1 * time.Second)
select {
case <-ctx.Done():
log.Fatal("can not ping http server within the specified time interval.")
default:
}
}
// return fmt.Errorf("the router has no response, or it might took too long to start up")
}