Skip to content

Commit

Permalink
refactor(server): 删除了 Options.Catalog
Browse files Browse the repository at this point in the history
从外部继承一个 catalog 的可能性太少了,即使有也未必是 catalog.Builder 类型。
  • Loading branch information
caixw committed Apr 18, 2024
1 parent a82e420 commit 317a4ab
Show file tree
Hide file tree
Showing 17 changed files with 35 additions and 57 deletions.
1 change: 1 addition & 0 deletions cmd/web/locale/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// SPDX-License-Identifier: MIT

// Package update 更新本地化文档
package update

import (
Expand Down
2 changes: 1 addition & 1 deletion internal/locale/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func BenchmarkLocale_NewPrinter(b *testing.B) {
l := New(language.SimplifiedChinese, nil, nil)
l := New(language.SimplifiedChinese, nil)

b.Run("equal Locale.id", func(b *testing.B) {
b.ResetTimer()
Expand Down
6 changes: 2 additions & 4 deletions internal/locale/locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ type Locale struct {
ttl *ttlcache.Cache[language.Tag, *message.Printer]
}

func New(id language.Tag, conf *config.Config, b *catalog.Builder) *Locale {
if b == nil {
b = catalog.NewBuilder(catalog.Fallback(id))
}
func New(id language.Tag, conf *config.Config) *Locale {
b := catalog.NewBuilder(catalog.Fallback(id))

// 保证 b 中包含一条 id 语言的翻译项,
// 这样可以始终让 Locale.Printer 的对象始终是有值的。
Expand Down
17 changes: 8 additions & 9 deletions internal/locale/locale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,25 @@ import (
func TestLocale_Printer(t *testing.T) {
a := assert.New(t, false)

b := catalog.NewBuilder()
l := New(language.SimplifiedChinese, nil, b)
l := New(language.SimplifiedChinese, nil)
a.NotError(l.SetString(language.SimplifiedChinese, "lang", "hans")).
NotNil(l).Equal(l.Sprintf("lang"), "hans").
NotError(l.SetString(language.SimplifiedChinese, "lang", "hans-2")).
Equal(l.Sprintf("lang"), "hans-2")

// ID 不存在于 catalog

b = catalog.NewBuilder()
a.NotError(b.SetString(language.SimplifiedChinese, "lang", "hans"))
l = New(language.Afrikaans, nil, b)
a.NotNil(l).Equal(l.Sprintf("lang"), "lang"). // 找不到对应的翻译项,返回原值
NotError(l.SetString(language.Afrikaans, "lang", "afrik")).
Equal(l.Sprintf("lang"), "afrik")
l = New(language.Afrikaans, nil)
a.NotNil(l).
NotError(l.SetString(language.SimplifiedChinese, "lang", "hans")).
Equal(l.Sprintf("lang"), "lang"). // 找不到对应的翻译项,返回原值
NotError(l.SetString(language.Afrikaans, "lang", "afrik")).
Equal(l.Sprintf("lang"), "afrik")
}

func TestLocale_NewPrinter(t *testing.T) {
a := assert.New(t, false)
l := New(language.SimplifiedChinese, nil, nil)
l := New(language.SimplifiedChinese, nil)
a.NotNil(l).Equal(l.ID(), language.SimplifiedChinese)

// language.SimplifiedChinese 是默认的 ID,初始化 l 时即已存在。
Expand Down
2 changes: 1 addition & 1 deletion mimetype/html/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

func newServer(a *assert.Assertion, lang string) web.Server {
s, err := server.New("test", "1.0.0", &server.Options{
s, err := server.NewHTTP("test", "1.0.0", &server.Options{
HTTPServer: &http.Server{Addr: ":8080"},
Language: language.MustParse(lang),
Codec: web.NewCodec().AddMimetype(html.Mimetype, html.Marshal, html.Unmarshal, ""),
Expand Down
2 changes: 1 addition & 1 deletion mimetype/jsonp/jsonp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

func TestJSONP(t *testing.T) {
a := assert.New(t, false)
s, err := server.New("test", "1.0.0", &server.Options{
s, err := server.NewHTTP("test", "1.0.0", &server.Options{
Codec: web.NewCodec().AddMimetype(Mimetype, Marshal, Unmarshal, ""),
HTTPServer: &http.Server{Addr: ":8080"},
})
Expand Down
2 changes: 1 addition & 1 deletion mimetype/sse/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestMessage_append(t *testing.T) {

func TestOnMessage(t *testing.T) {
a := assert.New(t, false)
s, err := server.New("test", "1.0.0", &server.Options{
s, err := server.NewHTTP("test", "1.0.0", &server.Options{
HTTPServer: &http.Server{Addr: ":8080"},
Codec: web.NewCodec().AddMimetype(Mimetype, nop.Marshal, nop.Unmarshal, ""),
Logs: logs.New(logs.NewTermHandler(os.Stderr, nil), logs.WithCreated(logs.MicroLayout)),
Expand Down
2 changes: 1 addition & 1 deletion mimetype/sse/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

func TestServer(t *testing.T) {
a := assert.New(t, false)
s, err := server.New("test", "1.0.0", &server.Options{
s, err := server.NewHTTP("test", "1.0.0", &server.Options{
HTTPServer: &http.Server{Addr: ":8080"},
Codec: web.NewCodec().AddMimetype(json.Mimetype, json.Marshal, json.Unmarshal, ""),
Logs: logs.New(logs.NewTermHandler(os.Stderr, nil), logs.WithCreated(logs.MicroLayout), logs.WithLevels(logs.AllLevels()...)),
Expand Down
4 changes: 1 addition & 3 deletions server/app/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ type CLIOptions[T any] struct {

// 配置文件所在的目录
//
// 这也将影响后续 [server.Options.Config] 变量,如果为空,则会采用 [server.DefaultConfigDir]。
//
// 有以下几种前缀用于指定不同的保存目录:
// - ~ 表示系统提供的配置文件目录,比如 Linux 的 XDG_CONFIG、Windows 的 AppData 等;
// - @ 表示当前程序的主目录;
Expand All @@ -70,7 +68,7 @@ type CLIOptions[T any] struct {

// 本地化的打印对象
//
// 若为空,则以 server.NewPrinter(locales.Locales, "*.yaml") 进行初始化。
// 若为空,则以 config.NewPrinter("*.yaml", locales.Locales) 进行初始化。
//
// 若是自定义,至少需要保证以下几个字符串的翻译项,才有效果:
// - cmd.show_version
Expand Down
4 changes: 2 additions & 2 deletions server/app/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestCLI(t *testing.T) {
ServeActions: []string{"serve"},
NewServer: func(name, ver string, opt *server.Options, _ empty, act string) (web.Server, error) {
action = act
return server.New(name, ver, opt)
return server.NewHTTP(name, ver, opt)
},
}
cmd := NewCLI(o)
Expand All @@ -55,7 +55,7 @@ func TestCLI_sanitize(t *testing.T) {
Name: "app",
Version: "1.1.1",
NewServer: func(name, ver string, opt *server.Options, _ empty, _ string) (web.Server, error) {
return server.New(name, ver, opt)
return server.NewHTTP(name, ver, opt)
},
ConfigFilename: "web.yaml",
}
Expand Down
2 changes: 1 addition & 1 deletion server/app/restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestSignalHUP(t *testing.T) {
ConfigFilename: "web.yaml",
ServeActions: []string{"serve"},
NewServer: func(name, ver string, opt *server.Options, _ empty, _ string) (web.Server, error) {
return server.New(name, ver, opt)
return server.NewHTTP(name, ver, opt)
},
}
c := NewCLI(cmd).(*cli[empty])
Expand Down
2 changes: 1 addition & 1 deletion server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func CheckConfigSyntax[T comparable](configDir, filename string) error {
return err
}

// NewPrinter 根据参数构建一个本地化的打印对象
// NewPrinter 根据参数指定的配置文件构建一个本地化的打印对象
//
// 语言由 [localeutil.DetectUserLanguageTag] 决定。
// fsys 指定了加载本地化文件的文件系统,glob 则指定了加载的文件匹配规则;
Expand Down
19 changes: 3 additions & 16 deletions server/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/issue9/mux/v8/header"
"github.com/issue9/unique/v2"
"golang.org/x/text/language"
"golang.org/x/text/message/catalog"
"gopkg.in/yaml.v3"

"github.com/issue9/web"
Expand All @@ -38,9 +37,9 @@ const (
)

type (
// Options [web.Server] 的初始化参数
// Options 初始化 [web.Server] 的参数
//
// 这些参数都有默认值,且无法在 [web.Server] 初始化之后进行更改。
// NOTE: 这些参数都有默认值,且无法在 [web.Server] 初始化之后进行更改。
Options struct {
// 项目的配置文件管理
//
Expand Down Expand Up @@ -101,13 +100,6 @@ type (
// 如果为空,则会尝试读取当前系统的本地化信息。
Language language.Tag

// 本地化的数据
//
// 如果为空,则会被初始化成一个空对象。
// Catalog 中会强行插入一条 tag 与 [Options.Language] 相同的翻译项,
// 以保证能正确构建 [web.Server.Printer] 对象。
Catalog *catalog.Builder

locale *locale.Locale

// 所有 [web.Problem.Type] 字段的前缀
Expand Down Expand Up @@ -199,12 +191,7 @@ func sanitizeOptions(o *Options, t int) (*Options, *web.FieldError) {
}
o.Language = tag
}

if o.Catalog == nil {
o.Catalog = catalog.NewBuilder(catalog.Fallback(o.Language))
}

o.locale = locale.New(o.Language, o.Config, o.Catalog)
o.locale = locale.New(o.Language, o.Config)

if o.Logs == nil {
o.Logs = logs.New(logs.NewNopHandler())
Expand Down
2 changes: 1 addition & 1 deletion server/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func newTestServer(a *assert.Assertion) web.Server {
srv, err := server.New("test", "1.0.0", &server.Options{
srv, err := server.NewHTTP("test", "1.0.0", &server.Options{
Logs: logs.New(logs.NewTermHandler(os.Stderr, nil), logs.WithLevels(logs.AllLevels()...), logs.WithLocation(true), logs.WithCreated(logs.NanoLayout)),
HTTPServer: &http.Server{Addr: ":8080"},
})
Expand Down
6 changes: 3 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Package server 提供与服务端实现相关的功能
//
// 目前实现了三种类型的服务端:
// - [New] 构建普通的 HTTP 服务;
// - [NewHTTP] 构建普通的 HTTP 服务;
// - [NewGateway] 构建微服务的网关服务;
// - [NewService] 构建微服务;
package server
Expand Down Expand Up @@ -60,11 +60,11 @@ func newHTTPServer(name, version string, o *Options, s web.Server) *httpServer {
return srv
}

// New 新建 HTTP 服务
// NewHTTP 新建 HTTP 服务
//
// name, version 表示服务的名称和版本号;
// o 指定了一些带有默认值的参数;
func New(name, version string, o *Options) (web.Server, error) {
func NewHTTP(name, version string, o *Options) (web.Server, error) {
o, err := sanitizeOptions(o, typeHTTP)
if err != nil {
return nil, err.AddFieldParent("o")
Expand Down
4 changes: 2 additions & 2 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func buildHandler(code int) web.HandlerFunc {
func TestNew(t *testing.T) {
a := assert.New(t, false)

srv, err := New("app", "0.1.0", nil)
srv, err := NewHTTP("app", "0.1.0", nil)
a.NotError(err).NotNil(srv).
False(srv.Uptime().IsZero()).
NotNil(srv.Cache()).
Expand Down Expand Up @@ -79,7 +79,7 @@ func newOptions(o *Options) *Options {
}

func newTestServer(a *assert.Assertion, o *Options) web.Server {
srv, err := New("app", "0.1.0", newOptions(o))
srv, err := NewHTTP("app", "0.1.0", newOptions(o))
a.NotError(err).NotNil(srv).
Equal(srv.Name(), "app").Equal(srv.Version(), "0.1.0")

Expand Down
15 changes: 5 additions & 10 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"github.com/issue9/mux/v8/header"
"github.com/issue9/unique/v2"
"golang.org/x/text/language"
"golang.org/x/text/message"
"golang.org/x/text/message/catalog"

"github.com/issue9/web/internal/locale"
)
Expand All @@ -37,28 +35,25 @@ func onExitContext(ctx *Context, status int) {
}

func newTestServer(a *assert.Assertion) *testServer {
c := catalog.NewBuilder()
a.NotError(c.SetString(language.SimplifiedChinese, "lang", "cn")).
NotError(c.SetString(language.TraditionalChinese, "lang", "tw"))

l := language.SimplifiedChinese
p := message.NewPrinter(l, message.Catalog(c))
l := locale.New(language.SimplifiedChinese, nil)
a.NotError(l.SetString(language.SimplifiedChinese, "lang", "cn")).
NotError(l.SetString(language.TraditionalChinese, "lang", "tw"))

logBuf := new(bytes.Buffer)
log := logs.New(
logs.NewTextHandler(logBuf, os.Stderr),
logs.WithCreated(logs.NanoLayout),
logs.WithLocation(true),
logs.WithLevels(logs.AllLevels()...),
logs.WithLocale(p),
logs.WithLocale(l.Printer()),
)
a.NotNil(log)

srv := &testServer{logBuf: logBuf}

cc, gc := memory.New()
u := unique.NewNumber(100)
srv.InternalServer = InternalNewServer(srv, "test", "1.0.0", time.Local, log, u.String, locale.New(l, nil, c), cc, newCodec(a), header.XRequestID, "", nil)
srv.InternalServer = InternalNewServer(srv, "test", "1.0.0", time.Local, log, u.String, l, cc, newCodec(a), header.XRequestID, "", nil)
srv.Services().Add(Phrase("unique"), u)
srv.Services().AddTicker(Phrase("gc memory"), func(t time.Time) error { gc(t); return nil }, time.Minute, false, false)

Expand Down

0 comments on commit 317a4ab

Please sign in to comment.