diff --git a/server/config/http.go b/server/config/http.go index d6bd7d52..3ae0dc1d 100644 --- a/server/config/http.go +++ b/server/config/http.go @@ -47,10 +47,10 @@ type ( tlsConfig *tls.Config - ReadTimeout duration `yaml:"readTimeout,omitempty" json:"readTimeout,omitempty" xml:"readTimeout,attr,omitempty"` - WriteTimeout duration `yaml:"writeTimeout,omitempty" json:"writeTimeout,omitempty" xml:"writeTimeout,attr,omitempty"` - IdleTimeout duration `yaml:"idleTimeout,omitempty" json:"idleTimeout,omitempty" xml:"idleTimeout,attr,omitempty"` - ReadHeaderTimeout duration `yaml:"readHeaderTimeout,omitempty" json:"readHeaderTimeout,omitempty" xml:"readHeaderTimeout,attr,omitempty"` + ReadTimeout Duration `yaml:"readTimeout,omitempty" json:"readTimeout,omitempty" xml:"readTimeout,attr,omitempty"` + WriteTimeout Duration `yaml:"writeTimeout,omitempty" json:"writeTimeout,omitempty" xml:"writeTimeout,attr,omitempty"` + IdleTimeout Duration `yaml:"idleTimeout,omitempty" json:"idleTimeout,omitempty" xml:"idleTimeout,attr,omitempty"` + ReadHeaderTimeout Duration `yaml:"readHeaderTimeout,omitempty" json:"readHeaderTimeout,omitempty" xml:"readHeaderTimeout,attr,omitempty"` MaxHeaderBytes int `yaml:"maxHeaderBytes,omitempty" json:"maxHeaderBytes,omitempty" xml:"maxHeaderBytes,attr,omitempty"` // Recovery 拦截 panic 时反馈给客户端的状态码 @@ -90,8 +90,10 @@ type ( httpServer *http.Server } - // 表示时间段,等同于 [time.Duration] - duration time.Duration // 封装 [time.Duration] 以实现对 JSON、XML 和 YAML 的解析 + // Duration 表示时间段 + // + // 封装 [time.Duration] 以实现对 JSON、XML 和 YAML 的解析 + Duration time.Duration headerConfig struct { // 报头名称 @@ -168,7 +170,7 @@ var ( return err == nil || errors.Is(err, fs.ErrExist) }, locales.NotFound) - durShouldGreatThan0 = filter.V(func(v duration) bool { return v >= 0 }, locales.ShouldGreatThan(0)) + durShouldGreatThan0 = filter.V(func(v Duration) bool { return v >= 0 }, locales.ShouldGreatThan(0)) ) func (cert *certificateConfig) sanitize() *web.FieldError { @@ -311,14 +313,15 @@ func (l *acmeConfig) sanitize() *web.FieldError { ) } -func (d duration) Duration() time.Duration { return time.Duration(d) } +// Duration 转换为标准库的 [time.Duration] +func (d Duration) Duration() time.Duration { return time.Duration(d) } -func (d duration) MarshalText() ([]byte, error) { return []byte(time.Duration(d).String()), nil } +func (d Duration) MarshalText() ([]byte, error) { return []byte(time.Duration(d).String()), nil } -func (d *duration) UnmarshalText(b []byte) error { +func (d *Duration) UnmarshalText(b []byte) error { v, err := time.ParseDuration(string(b)) if err == nil { - *d = duration(v) + *d = Duration(v) } return err } diff --git a/server/config/http_test.go b/server/config/http_test.go index 80731ccd..6662133a 100644 --- a/server/config/http_test.go +++ b/server/config/http_test.go @@ -106,12 +106,12 @@ func TestACME_sanitize(t *testing.T) { var ( dur time.Duration - _ encoding.TextMarshaler = duration(1) - _ encoding.TextUnmarshaler = (*duration)(&dur) + _ encoding.TextMarshaler = Duration(1) + _ encoding.TextUnmarshaler = (*Duration)(&dur) ) type testDuration struct { - Duration duration `xml:"dur" json:"dur" yaml:"dur"` + Duration Duration `xml:"dur" json:"dur" yaml:"dur"` } func TestDuration_Duration(t *testing.T) { @@ -119,14 +119,14 @@ func TestDuration_Duration(t *testing.T) { dur := time.Second * 2 - a.Equal(dur, duration(dur).Duration()) + a.Equal(dur, Duration(dur).Duration()) } func TestDuration_YAML(t *testing.T) { a := assert.New(t, false) m := &testDuration{ - Duration: duration(time.Nanosecond * 5), + Duration: Duration(time.Nanosecond * 5), } bs, err := yaml.Marshal(m) @@ -143,7 +143,7 @@ func TestDuration_XML(t *testing.T) { a := assert.New(t, false) m := &testDuration{ - Duration: duration(time.Nanosecond * 5), + Duration: Duration(time.Nanosecond * 5), } bs, err := xml.MarshalIndent(m, "", " ") @@ -160,10 +160,10 @@ func TestDuration_XMLAttr(t *testing.T) { a := assert.New(t, false) type obj struct { - D duration `xml:"d,attr"` + D Duration `xml:"d,attr"` } m := &obj{ - D: duration(time.Nanosecond * 5), + D: Duration(time.Nanosecond * 5), } bs, err := xml.MarshalIndent(m, "", " ") @@ -177,7 +177,7 @@ func TestDuration_JSON(t *testing.T) { a := assert.New(t, false) m := &testDuration{ - Duration: duration(time.Nanosecond * 5), + Duration: Duration(time.Nanosecond * 5), } bs, err := json.Marshal(m)