Skip to content

Commit

Permalink
refactor(server/config): 将加载配置文件从 server 中独立
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Apr 17, 2024
1 parent 621ca66 commit b67e537
Show file tree
Hide file tree
Showing 47 changed files with 596 additions and 919 deletions.
4 changes: 2 additions & 2 deletions cmd/web/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ go 1.22.0
require (
github.com/caixw/gobuild v1.7.5
github.com/getkin/kin-openapi v0.124.0
github.com/issue9/assert/v4 v4.1.1
github.com/issue9/assert/v4 v4.2.0
github.com/issue9/cmdopt v0.13.1
github.com/issue9/localeutil v0.26.5
github.com/issue9/logs/v7 v7.5.1
github.com/issue9/logs/v7 v7.6.0
github.com/issue9/mux/v8 v8.1.0
github.com/issue9/query/v3 v3.1.3
github.com/issue9/sliceutil v0.16.1
Expand Down
8 changes: 4 additions & 4 deletions cmd/web/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY=
github.com/invopop/yaml v0.2.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q=
github.com/issue9/assert/v4 v4.1.1 h1:OhPE8SB8n/qZCNGLQa+6MQtr/B3oON0JAVj68k8jJlc=
github.com/issue9/assert/v4 v4.1.1/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4=
github.com/issue9/assert/v4 v4.2.0 h1:XJGMFYW0xfESqFRPLWbSsr0xWdkofytvQbDfNb5n9fw=
github.com/issue9/assert/v4 v4.2.0/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4=
github.com/issue9/cache v0.12.0 h1:NiDBuN9x22H4UJsOMDoEuIFA8r3qNqPqO9vyzzcvzoY=
github.com/issue9/cache v0.12.0/go.mod h1:0s9j7qiKv4uWYqz0D2N2H7bIBvmtD+903h5GqnxW6i4=
github.com/issue9/cmdopt v0.13.1 h1:VA/Hgd92NBbZyHjZx1xcRCMhoc+XjI1LWhiuZkOZ0VU=
Expand All @@ -30,8 +30,8 @@ github.com/issue9/errwrap v0.3.2 h1:7KEme9Pfe75M+sIMcPCn/DV90wjnOcRbO4DXVAHj3Fw=
github.com/issue9/errwrap v0.3.2/go.mod h1:KcCLuUGiffjooLCUjL89r1cyO8/HT/VRcQrneO53N3A=
github.com/issue9/localeutil v0.26.5 h1:e78b6cOOtgzfb4g4U9uPLC8QyK6Lux+s7ZiQe+6iM1A=
github.com/issue9/localeutil v0.26.5/go.mod h1:BJXJwcAT9CyyVZOlqfmq+B5FcPbqGxGjYnTYbVuiMM8=
github.com/issue9/logs/v7 v7.5.1 h1:H1ua+3C0Nm6LZt4gEFhKZiyHhzyYEkPVYJDtt+1jmvQ=
github.com/issue9/logs/v7 v7.5.1/go.mod h1:UA05C4wF8vrrQp13QV1ncNqI/6nJ8R1c/dpOOcbSKFQ=
github.com/issue9/logs/v7 v7.6.0 h1:dvY1ctPROdd2YaOwYRNOkfbmMx+8OM0w53t8bWrWg9s=
github.com/issue9/logs/v7 v7.6.0/go.mod h1:7Hx1vnAojUciyFdqNlMiwsBJRGBc/P2Yrjt7ACm9Uno=
github.com/issue9/mux/v8 v8.1.0 h1:NZmQv0iE0ocn1oyHWKZWY+PIVcbpZYdaDImJ3+WJ/28=
github.com/issue9/mux/v8 v8.1.0/go.mod h1:ivUHUcMzoTPxvjC33XrsWJ68fnw5vEjJ0y1GcuMrg84=
github.com/issue9/query/v3 v3.1.3 h1:Y6ETEYXxaKqhpM4lXPKCffhJ72VuKQbrAwgwHlacu0Y=
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ go 1.22.0
require (
github.com/andybalholm/brotli v1.1.0
github.com/fxamacker/cbor/v2 v2.6.0
github.com/issue9/assert/v4 v4.1.1
github.com/issue9/assert/v4 v4.2.0
github.com/issue9/cache v0.12.0
github.com/issue9/config v0.6.2
github.com/issue9/conv v1.3.5
github.com/issue9/errwrap v0.3.2
github.com/issue9/localeutil v0.26.5
github.com/issue9/logs/v7 v7.5.1
github.com/issue9/logs/v7 v7.6.0
github.com/issue9/mux/v8 v8.1.0
github.com/issue9/query/v3 v3.1.3
github.com/issue9/scheduled v0.19.5
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fxamacker/cbor/v2 v2.6.0 h1:sU6J2usfADwWlYDAFhZBQ6TnLFBHxgesMrQfQgk1tWA=
github.com/fxamacker/cbor/v2 v2.6.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
github.com/issue9/assert/v4 v4.1.1 h1:OhPE8SB8n/qZCNGLQa+6MQtr/B3oON0JAVj68k8jJlc=
github.com/issue9/assert/v4 v4.1.1/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4=
github.com/issue9/assert/v4 v4.2.0 h1:XJGMFYW0xfESqFRPLWbSsr0xWdkofytvQbDfNb5n9fw=
github.com/issue9/assert/v4 v4.2.0/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4=
github.com/issue9/cache v0.12.0 h1:NiDBuN9x22H4UJsOMDoEuIFA8r3qNqPqO9vyzzcvzoY=
github.com/issue9/cache v0.12.0/go.mod h1:0s9j7qiKv4uWYqz0D2N2H7bIBvmtD+903h5GqnxW6i4=
github.com/issue9/config v0.6.2 h1:znXvsk6gh0wm+fTEn0zUjjramKuOLY8Jt0ZTxp4GIkc=
Expand All @@ -26,8 +26,8 @@ github.com/issue9/errwrap v0.3.2 h1:7KEme9Pfe75M+sIMcPCn/DV90wjnOcRbO4DXVAHj3Fw=
github.com/issue9/errwrap v0.3.2/go.mod h1:KcCLuUGiffjooLCUjL89r1cyO8/HT/VRcQrneO53N3A=
github.com/issue9/localeutil v0.26.5 h1:e78b6cOOtgzfb4g4U9uPLC8QyK6Lux+s7ZiQe+6iM1A=
github.com/issue9/localeutil v0.26.5/go.mod h1:BJXJwcAT9CyyVZOlqfmq+B5FcPbqGxGjYnTYbVuiMM8=
github.com/issue9/logs/v7 v7.5.1 h1:H1ua+3C0Nm6LZt4gEFhKZiyHhzyYEkPVYJDtt+1jmvQ=
github.com/issue9/logs/v7 v7.5.1/go.mod h1:UA05C4wF8vrrQp13QV1ncNqI/6nJ8R1c/dpOOcbSKFQ=
github.com/issue9/logs/v7 v7.6.0 h1:dvY1ctPROdd2YaOwYRNOkfbmMx+8OM0w53t8bWrWg9s=
github.com/issue9/logs/v7 v7.6.0/go.mod h1:7Hx1vnAojUciyFdqNlMiwsBJRGBc/P2Yrjt7ACm9Uno=
github.com/issue9/mux/v8 v8.1.0 h1:NZmQv0iE0ocn1oyHWKZWY+PIVcbpZYdaDImJ3+WJ/28=
github.com/issue9/mux/v8 v8.1.0/go.mod h1:ivUHUcMzoTPxvjC33XrsWJ68fnw5vEjJ0y1GcuMrg84=
github.com/issue9/query/v3 v3.1.3 h1:Y6ETEYXxaKqhpM4lXPKCffhJ72VuKQbrAwgwHlacu0Y=
Expand Down
13 changes: 3 additions & 10 deletions mimetype/html/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/issue9/assert/v4"
"github.com/issue9/logs/v7"
"github.com/issue9/mux/v8/header"
"golang.org/x/text/language"

Expand All @@ -24,16 +25,8 @@ func newServer(a *assert.Assertion, lang string) web.Server {
s, err := server.New("test", "1.0.0", &server.Options{
HTTPServer: &http.Server{Addr: ":8080"},
Language: language.MustParse(lang),
Mimetypes: []*server.Mimetype{
{
Name: html.Mimetype,
Marshal: html.Marshal,
Unmarshal: html.Unmarshal,
},
},
Logs: &server.Logs{
Handler: server.NewTermHandler(os.Stderr, nil),
},
Codec: web.NewCodec().AddMimetype(html.Mimetype, html.Marshal, html.Unmarshal, ""),
Logs: logs.New(logs.NewTermHandler(os.Stderr, nil)),
})
a.NotError(err).NotNil(s)

Expand Down
4 changes: 1 addition & 3 deletions mimetype/jsonp/jsonp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import (
func TestJSONP(t *testing.T) {
a := assert.New(t, false)
s, err := server.New("test", "1.0.0", &server.Options{
Mimetypes: []*server.Mimetype{
{Name: Mimetype, Marshal: Marshal, Unmarshal: Unmarshal, Problem: ""},
},
Codec: web.NewCodec().AddMimetype(Mimetype, Marshal, Unmarshal, ""),
HTTPServer: &http.Server{Addr: ":8080"},
})
a.NotError(err).NotNil(s)
Expand Down
11 changes: 3 additions & 8 deletions mimetype/sse/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/issue9/assert/v4"
"github.com/issue9/logs/v7"

"github.com/issue9/web"
"github.com/issue9/web/mimetype/nop"
Expand Down Expand Up @@ -56,14 +57,8 @@ func TestOnMessage(t *testing.T) {
a := assert.New(t, false)
s, err := server.New("test", "1.0.0", &server.Options{
HTTPServer: &http.Server{Addr: ":8080"},
Mimetypes: []*server.Mimetype{
{Name: Mimetype, Marshal: nop.Marshal, Unmarshal: nop.Unmarshal},
},
Logs: &server.Logs{
Created: server.MicroLayout,
Handler: server.NewTermHandler(os.Stderr, nil),
Levels: server.AllLevels(),
},
Codec: web.NewCodec().AddMimetype(Mimetype, nop.Marshal, nop.Unmarshal, ""),
Logs: logs.New(logs.NewTermHandler(os.Stderr, nil), logs.WithCreated(logs.MicroLayout)),
})
a.NotError(err).NotNil(s)
e := NewServer[int64](s, 50*time.Millisecond, 5*time.Second, 10)
Expand Down
11 changes: 3 additions & 8 deletions mimetype/sse/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/issue9/assert/v4"
"github.com/issue9/logs/v7"
"github.com/issue9/mux/v8/header"

"github.com/issue9/web"
Expand All @@ -25,14 +26,8 @@ func TestServer(t *testing.T) {
a := assert.New(t, false)
s, err := server.New("test", "1.0.0", &server.Options{
HTTPServer: &http.Server{Addr: ":8080"},
Mimetypes: []*server.Mimetype{
{Name: header.JSON, Marshal: json.Marshal, Unmarshal: json.Unmarshal},
},
Logs: &server.Logs{
Created: server.MicroLayout,
Handler: server.NewTermHandler(os.Stderr, nil),
Levels: server.AllLevels(),
},
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()...)),
})
a.NotError(err).NotNil(s)
e := NewServer[int64](s, 50*time.Millisecond, 5*time.Second, 10)
Expand Down
2 changes: 1 addition & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func Recovery(status int, l *Logger) RouterOption {
return mux.Recovery(func(w http.ResponseWriter, msg any) {
err, ok := msg.(error)
if !ok {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
http.Error(w, http.StatusText(status), status)
l.String(source.Stack(4, err))
return
}
Expand Down
9 changes: 5 additions & 4 deletions server/app/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/issue9/web"
"github.com/issue9/web/locales"
"github.com/issue9/web/server"
"github.com/issue9/web/server/config"
)

const (
Expand Down Expand Up @@ -64,7 +65,7 @@ type CLIOptions[T any] struct {
//
// 相对于 ConfigDir 的文件名,不能为空。
//
// 需要保证序列化方法已经由 [RegisterFileSerializer] 注册;
// 需要保证序列化方法已经由 [config.RegisterFileSerializer] 注册;
ConfigFilename string

// 本地化的打印对象
Expand Down Expand Up @@ -101,7 +102,7 @@ type cli[T any] struct {
// - -h 显示帮助信息;
// - -a 执行的指令,该值会传递给 [CLIOptions.NewServer],由用户根据此值决定初始化方式;
//
// T 表示的是配置文件中的用户自定义数据类型,可参考 [server.LoadOptions] 中有关 User 的说明。
// T 表示的是配置文件中的用户自定义数据类型,可参考 [config.Load] 中有关 User 的说明。
//
// 如果是 [CLIOptions] 本身字段设置有问题会直接 panic。
func NewCLI[T comparable](o *CLIOptions[T]) App {
Expand All @@ -112,7 +113,7 @@ func NewCLI[T comparable](o *CLIOptions[T]) App {
var action string // -a 参数

initServer := func() (web.Server, error) {
opt, user, err := server.LoadOptions[T](o.ConfigDir, o.ConfigFilename)
opt, user, err := config.Load[T](o.ConfigDir, o.ConfigFilename)
if err != nil {
return nil, web.NewStackError(err)
}
Expand Down Expand Up @@ -167,7 +168,7 @@ func (cmd *cli[T]) Exec() error { return cmd.exec(os.Args) }

func (o *CLIOptions[T]) sanitize() error {
if o.Printer == nil {
p, err := server.NewPrinter("*.yaml", locales.Locales...)
p, err := config.NewPrinter("*.yaml", locales.Locales...)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit b67e537

Please sign in to comment.