-
Notifications
You must be signed in to change notification settings - Fork 62
/
new.go
213 lines (177 loc) · 6.14 KB
/
new.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
// SPDX-License-Identifier: AGPL-3.0-only
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package web
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/pprof"
"strconv"
"strings"
"time"
"github.com/bytedance/sonic/decoder"
"github.com/bytedance/sonic/encoder"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/bangumi/server/config"
"github.com/bangumi/server/config/env"
"github.com/bangumi/server/internal/metrics"
"github.com/bangumi/server/internal/pkg/errgo"
"github.com/bangumi/server/internal/pkg/gtime"
"github.com/bangumi/server/internal/pkg/logger"
"github.com/bangumi/server/internal/pkg/random"
"github.com/bangumi/server/openapi"
"github.com/bangumi/server/web/middleware/recovery"
"github.com/bangumi/server/web/req/cf"
"github.com/bangumi/server/web/res"
"github.com/bangumi/server/web/util"
)
const headerProcessTime = "x-process-time-ms"
const headerServerVersion = "x-server-version"
type echoJSONSerializer struct {
}
func (e echoJSONSerializer) Serialize(c echo.Context, i any, indent string) error {
enc := encoder.NewStreamEncoder(c.Response())
enc.SetIndent("", indent)
return enc.Encode(i) //nolint:wrapcheck
}
func (e echoJSONSerializer) Deserialize(c echo.Context, i any) error {
return decoder.NewStreamDecoder(c.Request().Body).Decode(i) //nolint:wrapcheck
}
var _ echo.JSONSerializer = echoJSONSerializer{}
//nolint:funlen
func New() *echo.Echo {
app := echo.New()
app.JSONSerializer = echoJSONSerializer{}
app.HTTPErrorHandler = getDefaultErrorHandler()
app.HideBanner = true
app.HidePort = true
app.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
metrics.RequestCount.Inc()
start := time.Now()
err := next(c)
sub := time.Since(start)
metrics.RequestHistogram.Observe(sub.Seconds())
c.Set(headerProcessTime, strconv.FormatInt(sub.Milliseconds(), 10))
c.Set(headerServerVersion, config.Version)
return err
}
})
if env.Development {
app.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
devRequestID := "fake-ray-" + random.Base62String(10)
c.Request().Header.Set(cf.HeaderRequestID, devRequestID)
c.Request().Header.Set(cf.HeaderRequestIP, c.Request().RemoteAddr)
c.Set(cf.HeaderRequestID, devRequestID)
return next(c)
}
})
}
app.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
ExposeHeaders: []string{headerProcessTime, headerServerVersion, cf.HeaderRequestID},
MaxAge: gtime.OneWeekSec,
}))
app.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
reqID := c.Request().Header.Get(cf.HeaderRequestID)
reqIP := c.Request().Header.Get(cf.HeaderRequestIP)
c.SetRequest(c.Request().
WithContext(context.WithValue(context.Background(), logger.RequestKey, &logger.RequestTrace{
IP: reqIP,
ReqID: reqID,
})))
return next(c)
}
})
app.Use(recovery.New())
app.GET("/metrics", echo.WrapHandler(promhttp.Handler()))
addProfile(app)
app.GET("/openapi", func(c echo.Context) error {
return c.Redirect(http.StatusFound, "/openapi/")
})
if env.Development {
// fasthttp bug, it uses an internal global variable and causing data race here
app.Static("/openapi/", "./openapi/")
} else {
app.StaticFS("/openapi/", openapi.Static)
}
return app
}
func addProfile(app *echo.Echo) {
app.GET("/debug/pprof/cmdline", echo.WrapHandler(http.HandlerFunc(pprof.Cmdline)))
app.GET("/debug/pprof/profile", echo.WrapHandler(http.HandlerFunc(pprof.Profile)))
app.GET("/debug/pprof/symbol", echo.WrapHandler(http.HandlerFunc(pprof.Symbol)))
app.GET("/debug/pprof/trace", echo.WrapHandler(http.HandlerFunc(pprof.Trace)))
app.Any("/debug/pprof/", echo.WrapHandler(http.HandlerFunc(pprof.Index)))
}
func Start(c config.AppConfig, app *echo.Echo) error {
addr := c.ListenAddr()
logger.Infoln("http server listening at", addr)
if env.Development {
fmt.Printf("\nvisit http://%s/\n\n", strings.ReplaceAll(addr, "0.0.0.0", "127.0.0.1"))
}
return errgo.Wrap(app.Start(c.ListenAddr()), "echo.Start")
}
func getDefaultErrorHandler() echo.HTTPErrorHandler {
var log = logger.Named("http.err").
WithOptions(zap.AddStacktrace(zapcore.PanicLevel), zap.WithCaller(false))
return func(err error, c echo.Context) {
{
var e res.HTTPError
if errors.As(err, &e) {
// handle expected http error
_ = c.JSON(e.Code, res.Error{
Title: http.StatusText(e.Code),
Description: e.Msg,
Details: util.Detail(c),
})
return
}
}
{
//nolint:forbidigo,errorlint
if e, ok := err.(*echo.HTTPError); ok {
log.Error("unexpected echo error",
zap.Int("code", e.Code),
zap.Any("message", e.Message),
zap.String("path", c.Request().URL.Path),
zap.String("query", c.Request().URL.RawQuery),
zap.String("cf-ray", c.Request().Header.Get(cf.HeaderRequestID)),
)
_ = c.JSON(http.StatusInternalServerError, res.Error{
Title: http.StatusText(e.Code),
Description: e.Error(),
Details: util.DetailWithErr(c, err),
})
return
}
}
log.Error("unexpected error",
zap.Error(err),
zap.String("path", c.Path()),
zap.String("query", c.Request().URL.RawQuery),
zap.String("cf-ray", c.Request().Header.Get(cf.HeaderRequestID)),
)
// unexpected error, return internal server error
_ = res.InternalError(c, err, "Unexpected Internal Server Error")
}
}