-
Notifications
You must be signed in to change notification settings - Fork 62
/
index.go
143 lines (125 loc) · 4.04 KB
/
index.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
// Copyright (c) 2021-2022 Trim21 <trim21.me@gmail.com>
//
// 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"
"fmt"
"net"
"strconv"
"time"
"github.com/goccy/go-json"
"github.com/gofiber/adaptor/v2"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"github.com/uber-go/tally/v4"
promreporter "github.com/uber-go/tally/v4/prometheus"
"go.uber.org/fx"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/bangumi/server/config"
"github.com/bangumi/server/internal/errgo"
"github.com/bangumi/server/internal/logger"
"github.com/bangumi/server/internal/metrics"
"github.com/bangumi/server/web/middleware/recovery"
"github.com/bangumi/server/web/res"
"github.com/bangumi/server/web/util"
)
func New(scope tally.Scope, reporter promreporter.Reporter) *fiber.App {
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
StrictRouting: true,
CaseSensitive: true,
ErrorHandler: getDefaultErrorHandler(),
JSONEncoder: json.MarshalNoEscape,
})
count := scope.Counter("request_count_total")
histogram := scope.Histogram("response_time", metrics.ResponseTimeBucket())
app.Use(func(c *fiber.Ctx) error {
count.Inc(1)
start := time.Now()
err := c.Next()
sub := time.Since(start)
histogram.RecordDuration(sub)
c.Set("x-process-time-ms", strconv.FormatInt(sub.Milliseconds(), 10))
c.Set("x-server-version", config.Version)
return err
})
app.Use(recovery.New())
app.Get("/metrics", adaptor.HTTPHandler(reporter.HTTPHandler()))
return app
}
func Listen(lc fx.Lifecycle, c config.AppConfig, app *fiber.App) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
logger.Infoln("start http server at port", c.HTTPPort)
addr := fmt.Sprintf(":%d", c.HTTPPort)
ln, err := net.Listen("tcp", addr)
if err != nil {
return errgo.Wrap(err, "failed to start listener")
}
go func() {
err := app.Listener(ln)
if err != nil {
logger.Panic("failed to start fiber app on listener", zap.Error(err))
}
}()
return nil
},
OnStop: func(ctx context.Context) error {
return errgo.Wrap(app.Shutdown(), "web.Shutdown")
},
})
}
func getDefaultErrorHandler() func(c *fiber.Ctx, err error) error {
var log = logger.Named("http.err").WithOptions(zap.AddStacktrace(zapcore.PanicLevel))
return func(ctx *fiber.Ctx, err error) error {
// Default 500 status code
code := fiber.StatusInternalServerError
title := "Internal Server Error"
description := "Unexpected Internal Server Error"
// router will return an un-wrapped error, so just check it like this.
// DO NOT rewrite it to errors.Is.
if e, ok := err.(*fiber.Error); ok { //nolint:errorlint
code = e.Code
switch e.Code {
case fiber.StatusInternalServerError:
break
case fiber.StatusNotFound:
description = "resource can't be found in the database or has been removed"
title = utils.StatusMessage(code)
default:
description = e.Error()
title = utils.StatusMessage(code)
}
} else {
log.Error("unexpected error",
zap.Error(err),
zap.String("path", ctx.Path()),
zap.ByteString("query", ctx.Request().URI().QueryString()),
zap.String("cf-ray", ctx.Get("cf-ray")),
)
}
return ctx.Status(code).JSON(res.Error{
Title: title,
Description: description,
Details: util.Detail{
Error: err.Error(),
Path: ctx.Path(),
QueryString: utils.UnsafeString(ctx.Request().URI().QueryString()),
},
})
}
}