-
Notifications
You must be signed in to change notification settings - Fork 62
/
new.go
153 lines (134 loc) · 4.26 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
// 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 handler
import (
"errors"
"reflect"
"strings"
"github.com/go-playground/locales/en"
"github.com/go-playground/locales/zh"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
zh_translations "github.com/go-playground/validator/v10/translations/zh" //nolint:importas
"go.uber.org/zap"
"go.uber.org/zap/buffer"
"github.com/bangumi/server/internal/cache"
"github.com/bangumi/server/internal/config"
"github.com/bangumi/server/internal/domain"
"github.com/bangumi/server/internal/errgo"
"github.com/bangumi/server/internal/oauth"
"github.com/bangumi/server/internal/web/captcha"
"github.com/bangumi/server/internal/web/frontend"
"github.com/bangumi/server/internal/web/rate"
"github.com/bangumi/server/internal/web/session"
)
var errTranslationNotFound = errors.New("failed to find translation for zh")
func New(
cfg config.AppConfig,
s domain.SubjectService,
c domain.CharacterService,
p domain.PersonService,
a domain.AuthService,
e domain.EpisodeRepo,
r domain.RevisionRepo,
index domain.IndexRepo,
user domain.UserRepo,
cache cache.Generic,
captcha captcha.Manager,
session session.Manager,
rateLimit rate.Manager,
log *zap.Logger,
engine frontend.TemplateEngine,
oauth oauth.Manager,
) (Handler, error) {
validate, trans, err := getValidator()
if err != nil {
return Handler{}, err
}
log = log.Named("web.handler")
return Handler{
cfg: cfg,
cache: cache,
log: log,
rateLimit: rateLimit,
session: session,
p: p,
s: s,
a: a,
u: user,
e: e,
c: c,
i: index,
r: r,
captcha: captcha,
v: validate,
validatorTranslation: trans,
skip1Log: log.WithOptions(zap.AddCallerSkip(1)),
oauth: oauth,
template: engine,
buffPool: buffer.NewPool(),
}, nil
}
type Handler struct {
validatorTranslation ut.Translator
rateLimit rate.Manager
s domain.SubjectService
p domain.PersonService
a domain.AuthService
session session.Manager
captcha captcha.Manager
e domain.EpisodeRepo
c domain.CharacterService
u domain.UserRepo
cache cache.Generic
i domain.IndexRepo
r domain.RevisionRepo
buffPool buffer.Pool
oauth oauth.Manager
log *zap.Logger
skip1Log *zap.Logger
v *validator.Validate
template frontend.TemplateEngine
cfg config.AppConfig
}
func getValidator() (*validator.Validate, ut.Translator, error) {
validate := validator.New()
uni := ut.New(en.New(), zh.New())
// this is usually know or extracted from http 'Accept-Language' header
// also see uni.FindTranslator(...)
trans, found := uni.GetTranslator(zh.New().Locale())
if !found {
return nil, nil, errTranslationNotFound
}
err := zh_translations.RegisterDefaultTranslations(validate, trans)
if err != nil {
return nil, nil, errgo.Wrap(err, "failed to register translation")
}
validate.RegisterTagNameFunc(func(field reflect.StructField) string {
docName := field.Tag.Get("validateName")
if docName != "" {
return docName
}
tag := field.Tag.Get("json")
if tag == "" {
return field.Name
}
name := strings.SplitN(tag, ",", 2)[0]
if name == "-" {
return ""
}
return name
})
return validate, trans, nil
}