/
http_server.go
150 lines (135 loc) · 4.52 KB
/
http_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
146
147
148
149
150
// Licensed to LinDB under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. LinDB licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package http
import (
"context"
"fmt"
"io/fs"
"net/http"
"github.com/felixge/fgprof"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/lindb/lindb"
"github.com/lindb/lindb/config"
"github.com/lindb/lindb/constants"
_ "github.com/lindb/lindb/docs"
"github.com/lindb/lindb/internal/conntrack"
"github.com/lindb/lindb/internal/linmetric"
"github.com/lindb/lindb/pkg/hostutil"
"github.com/lindb/lindb/pkg/http/middleware"
"github.com/lindb/lindb/pkg/logger"
)
//go:generate mockgen -source ./http_server.go -destination=./http_server_mock.go -package=http
// Server represents http server with gin framework.
type Server interface {
// GetAPIRouter returns api router.
GetAPIRouter() *gin.RouterGroup
// Run runs the HTTP server.
Run() error
// Close closes the server.
Close(ctx context.Context) error
}
// server implements Server interface.
type server struct {
addr string
server http.Server
gin *gin.Engine
staticResource bool
cfg config.HTTP
r *linmetric.Registry
logger *logger.Logger
}
// NewServer creates http server.
func NewServer(cfg config.HTTP, staticResource bool, r *linmetric.Registry) Server {
s := &server{
cfg: cfg,
addr: fmt.Sprintf(":%d", cfg.Port),
gin: gin.New(),
staticResource: staticResource,
server: http.Server{
// use extra timeout for ingestion and query timeout
// if write timeout will return ERR_EMPTY_RESPONSE, chrome will does auto retry.
// https://www.bennadel.com/blog/3257-google-chrome-will-automatically-retry-requests-on-certain-error-responses.htm
// https://mariocarrion.com/2021/09/17/golang-software-architecture-resilience-http-servers.html
// WriteTimeout: cfg.WriteTimeout.Duration(),
ReadTimeout: cfg.ReadTimeout.Duration(),
IdleTimeout: cfg.IdleTimeout.Duration(),
},
r: r,
logger: logger.GetLogger("HTTP", "Server"),
}
s.init()
return s
}
// init initializes http server default router/handle/middleware.
func (s *server) init() {
// Using middlewares on group.
s.gin.Use(middleware.Recovery())
// use AccessLog to log panic error with zap
s.gin.Use(middleware.AccessLog())
s.gin.Use(cors.Default())
if config.Profile {
s.logger.Info("/debug/pprof is enabled")
pprof.Register(s.gin)
s.logger.Info("/debug/fgprof is enabled")
s.gin.GET("/debug/fgprof", gin.WrapH(fgprof.Handler()))
}
if config.Doc {
// swagger-ui: http://localhost:port/swagger/index.html
ip, _ := hostutil.GetHostIP()
s.gin.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler,
ginSwagger.URL(fmt.Sprintf("http://%s:%d/swagger/doc.json", ip, s.cfg.Port)),
ginSwagger.DefaultModelsExpandDepth(-1)))
}
if s.staticResource {
// server static file
staticFS, err := fs.Sub(lindb.StaticContent, "web/static")
staticHome := "/console"
if err != nil {
s.logger.Error("cannot find static resource", logger.Error(err))
} else {
s.gin.StaticFS(staticHome, http.FS(staticFS))
// redirects to admin console
s.gin.GET("/", func(c *gin.Context) {
c.Request.URL.Path = staticHome
s.gin.HandleContext(c)
})
}
}
}
// GetAPIRouter returns api router.
func (s *server) GetAPIRouter() *gin.RouterGroup {
return s.gin.Group(constants.APIRoot)
}
// Run runs the HTTP server.
func (s *server) Run() error {
s.logger.Info("starting http server", logger.String("addr", s.server.Addr))
s.server.Handler = s.gin
// Open listener.
trackedListener, err := conntrack.NewTrackedListener("tcp", s.addr, s.r)
if err != nil {
return err
}
return s.server.Serve(trackedListener)
}
// Close closes the server.
func (s *server) Close(ctx context.Context) error {
return s.server.Shutdown(ctx)
}