Skip to content

Commit

Permalink
refactor!: 将 internal/problems 并入 server
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Oct 23, 2023
1 parent 6e57baf commit 13e5b1c
Show file tree
Hide file tree
Showing 27 changed files with 302 additions and 281 deletions.
2 changes: 1 addition & 1 deletion app/CONFIG.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<article></article>
<table>
<thead><tr><th>XML</th><th>JSON</th><th>YAML</th><th>类型</th><th>说明</th></tr></thead>
<tbody><tr><td>memoryLimit,attr,omitempty</td><td>memoryLimit,omitempty</td><td>memoryLimit,omitempty</td><td>int64</td><td>内存限制<br /><br />如果小于等于 0,表示不设置该值。<br />除非对该功能非常了解,否则不建议设置该值。<br /><br />具体功能可参考 https://pkg.go.dev/runtime/debug#SetMemoryLimit<br /></td></tr><tr><td>logs,omitempty</td><td>logs,omitempty</td><td>logs,omitempty</td><td><a href="#logsConfig">logsConfig</a></td><td>日志系统的配置项<br /><br />如果为空,所有日志输出都将被抛弃。<br /></td></tr><tr><td>language,attr,omitempty</td><td>language,omitempty</td><td>language,omitempty</td><td>string</td><td>指定默认语言<br /><br />服务端的默认语言以及客户端未指定 accept-language 时的默认值。<br />如果为空,则会尝试当前用户的语言。<br /></td></tr><tr><td>http,omitempty</td><td>http,omitempty</td><td>http,omitempty</td><td><a href="#httpConfig">httpConfig</a></td><td>与 HTTP 请求相关的设置项<br /></td></tr><tr><td>timezone,omitempty</td><td>timezone,omitempty</td><td>timezone,omitempty</td><td>string</td><td>时区名称<br /><br />可以是 Asia/Shanghai 等,具体可参考:<br />https://en.wikipedia.org/wiki/List_of_tz_database_time_zones<br /><br />为空和 Local(注意大小写) 值都会被初始化本地时间。<br /></td></tr><tr><td>cache,omitempty</td><td>cache,omitempty</td><td>cache,omitempty</td><td><a href="#cacheConfig">cacheConfig</a></td><td>指定缓存对象<br /><br />如果为空,则会采用内存作为缓存对象。<br /></td></tr><tr><td>compresses>compress,omitempty</td><td>compresses,omitempty</td><td>compresses,omitempty</td><td><a href="#compressConfig">compressConfig</a></td><td>压缩的相关配置<br /><br />如果为空,那么不支持压缩功能。<br /></td></tr><tr><td>fileSerializers>fileSerializer,omitempty</td><td>fileSerializers,omitempty</td><td>fileSerializers,omitempty</td><td>string</td><td>指定配置文件的序列化<br /><br />如果为空,表示不支持。<br /><br />可通过 [RegisterFileSerializer] 进行添加额外的序列化方法。默认可用为:<br /> - .yaml<br /> - .yml<br /> - .xml<br /> - .json<br /></td></tr><tr><td>mimetypes>mimetype,omitempty</td><td>mimetypes,omitempty</td><td>mimetypes,omitempty</td><td><a href="#mimetypeConfig">mimetypeConfig</a></td><td>指定可用的 mimetype<br /><br />如果为空,那么将不支持任何格式的内容输出。<br /></td></tr><tr><td>idGenerator,omitempty</td><td>idGenerator,omitempty</td><td>idGenerator,omitempty</td><td>string</td><td>唯一 ID 生成器<br /><br />该值由 [RegisterIDGenerator] 注册而来,默认情况下,有以下三个选项:<br /> - date 日期格式,默认值;<br /> - string 普通的字符串;<br /> - number 数值格式;<br />NOTE: 一旦运行在生产环境,就不应该修改此属性,新的生成器无法保证生成的 ID 不会与之前的重复。<br /></td></tr><tr><td>problemTypePrefix,omitempty</td><td>problemTypePrefix,omitempty</td><td>problemTypePrefix,omitempty</td><td>string</td><td>Problem 中 type 字段的前缀<br /></td></tr><tr><td>user,omitempty</td><td>user,omitempty</td><td>user,omitempty</td><td><a href="#T">T</a></td><td>用户自定义的配置项<br /></td></tr></tbody></table><h2 id="logsConfig">logsConfig</h2>
<tbody><tr><td>memoryLimit,attr,omitempty</td><td>memoryLimit,omitempty</td><td>memoryLimit,omitempty</td><td>int64</td><td>内存限制<br /><br />如果小于等于 0,表示不设置该值。<br />除非对该功能非常了解,否则不建议设置该值。<br /><br />具体功能可参考 https://pkg.go.dev/runtime/debug#SetMemoryLimit<br /></td></tr><tr><td>logs,omitempty</td><td>logs,omitempty</td><td>logs,omitempty</td><td><a href="#logsConfig">logsConfig</a></td><td>日志系统的配置项<br /><br />如果为空,所有日志输出都将被抛弃。<br /></td></tr><tr><td>language,attr,omitempty</td><td>language,omitempty</td><td>language,omitempty</td><td>string</td><td>指定默认语言<br /><br />服务端的默认语言以及客户端未指定 accept-language 时的默认值。<br />如果为空,则会尝试当前用户的语言。<br /></td></tr><tr><td>http,omitempty</td><td>http,omitempty</td><td>http,omitempty</td><td><a href="#httpConfig">httpConfig</a></td><td>与 HTTP 请求相关的设置项<br /></td></tr><tr><td>timezone,omitempty</td><td>timezone,omitempty</td><td>timezone,omitempty</td><td>string</td><td>时区名称<br /><br />可以是 Asia/Shanghai 等,具体可参考:<br />https://en.wikipedia.org/wiki/List_of_tz_database_time_zones<br /><br />为空和 Local(注意大小写) 值都会被初始化本地时间。<br /></td></tr><tr><td>cache,omitempty</td><td>cache,omitempty</td><td>cache,omitempty</td><td><a href="#cacheConfig">cacheConfig</a></td><td>指定缓存对象<br /><br />如果为空,则会采用内存作为缓存对象。<br /></td></tr><tr><td>compressions>compression,omitempty</td><td>compressions,omitempty</td><td>compressions,omitempty</td><td><a href="#compressConfig">compressConfig</a></td><td>压缩的相关配置<br /><br />如果为空,那么不支持压缩功能。<br /></td></tr><tr><td>fileSerializers>fileSerializer,omitempty</td><td>fileSerializers,omitempty</td><td>fileSerializers,omitempty</td><td>string</td><td>指定配置文件的序列化<br /><br />如果为空,表示不支持。<br /><br />可通过 [RegisterFileSerializer] 进行添加额外的序列化方法。默认可用为:<br /> - .yaml<br /> - .yml<br /> - .xml<br /> - .json<br /></td></tr><tr><td>mimetypes>mimetype,omitempty</td><td>mimetypes,omitempty</td><td>mimetypes,omitempty</td><td><a href="#mimetypeConfig">mimetypeConfig</a></td><td>指定可用的 mimetype<br /><br />如果为空,那么将不支持任何格式的内容输出。<br /></td></tr><tr><td>idGenerator,omitempty</td><td>idGenerator,omitempty</td><td>idGenerator,omitempty</td><td>string</td><td>唯一 ID 生成器<br /><br />该值由 [RegisterIDGenerator] 注册而来,默认情况下,有以下三个选项:<br /> - date 日期格式,默认值;<br /> - string 普通的字符串;<br /> - number 数值格式;<br />NOTE: 一旦运行在生产环境,就不应该修改此属性,新的生成器无法保证生成的 ID 不会与之前的重复。<br /></td></tr><tr><td>problemTypePrefix,omitempty</td><td>problemTypePrefix,omitempty</td><td>problemTypePrefix,omitempty</td><td>string</td><td>Problem 中 type 字段的前缀<br /></td></tr><tr><td>user,omitempty</td><td>user,omitempty</td><td>user,omitempty</td><td><a href="#T">T</a></td><td>用户自定义的配置项<br /></td></tr></tbody></table><h2 id="logsConfig">logsConfig</h2>
<article></article>
<table>
<thead><tr><th>XML</th><th>JSON</th><th>YAML</th><th>类型</th><th>说明</th></tr></thead>
Expand Down
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"golang.org/x/text/transform"

"github.com/issue9/web/internal/header"
"github.com/issue9/web/internal/problems"
"github.com/issue9/web/internal/status"
)

// Client 用于访问远程的客户端
Expand Down Expand Up @@ -146,7 +146,7 @@ func (c *Client) ParseResponse(rsp *http.Response, resp any, problem *RFC7807) (
}
defer rsp.Body.Close()

if problems.IsProblemStatus(rsp.StatusCode) {
if status.IsProblemStatus(rsp.StatusCode) {
return inputMimetype(data, problem)
}
return inputMimetype(data, resp)
Expand Down
1 change: 1 addition & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"github.com/issue9/assert/v3"

"github.com/issue9/web/internal/header"
)

Expand Down
4 changes: 2 additions & 2 deletions codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Package codec 编码解码工具
//
// 包含了压缩方法和媒体类型的处理。
// 包含了压缩方法和媒体类型的处理,实现了 [web.Codec] 接口及相关内容
package codec

import (
Expand Down Expand Up @@ -151,7 +151,7 @@ func BestCompressionCompressions(contentType ...string) []*Compression {

// New 声明 [web.Codec] 对象
//
// 用户需要自行调用 ms 和 cs 的 [config.Sanitizer] 接口对数据合规性作检测。
// NOTE: 用户需要自行调用 ms 和 cs 的 [config.Sanitizer] 接口对数据合规性作检测。
func New(ms []*Mimetype, cs []*Compression) web.Codec {
c := &codec{
compressions: make([]*compression, 0, len(cs)),
Expand Down
4 changes: 2 additions & 2 deletions codec/compressor/make_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/issue9/source"
"github.com/klauspost/compress/zstd"

"github.com/issue9/web/internal/problems/make"
"github.com/issue9/web/internal/status"
)

const (
Expand All @@ -29,7 +29,7 @@ func main() {

b := &errwrap.Buffer{}

b.WString(make.FileHeader).
b.WString(status.FileHeader).
WString("package ").WString(pkg).WString("\n\n")

b.WString("var (\n")
Expand Down
2 changes: 1 addition & 1 deletion filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func NewSlice[T any, S ~[]T](rule RulerFuncOf[S], s ...func(*T)) FilterFuncOf[S]
}
}

// NewSlice 生成针对 map 元素的 [FilterFuncOf]
// NewMap 生成针对 map 元素的 [FilterFuncOf]
func NewMap[K comparable, V any, M ~map[K]V](rule RulerFuncOf[M], s ...func(*V)) FilterFuncOf[M] {
return func(name string, v *M) FilterFunc {
return func() (string, localeutil.Stringer) {
Expand Down
81 changes: 0 additions & 81 deletions internal/problems/problems.go

This file was deleted.

67 changes: 0 additions & 67 deletions internal/problems/problems_test.go

This file was deleted.

7 changes: 3 additions & 4 deletions internal/problems/make/make.go → internal/status/make.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-License-Identifier: MIT

// Package make 提供一些通用的生成工具
package make
package status

import (
"go/ast"
Expand All @@ -16,8 +15,8 @@ import (

const FileHeader = "// 此文件由工具产生,请勿手动修改!\n\n"

// GetStatuses 从 net/http/status.go 获取所有的状态码
func GetStatuses() ([]Pair, error) {
// Get 从 net/http/status.go 获取所有的状态码
func Get() ([]Pair, error) {
path := filepath.Join(build.Default.GOROOT, "src", "net", "http", "status.go")
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, path, nil, parser.AllErrors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ import (
"github.com/issue9/errwrap"
"github.com/issue9/source"

"github.com/issue9/web/internal/problems/make"
"github.com/issue9/web/internal/status"
)

const (
filename = "statuses.go"
pkgName = "problems"
pkgName = "status"
)

func main() {
buf := &errwrap.Buffer{}
buf.WString(make.FileHeader).
buf.WString(status.FileHeader).
WString("package ").WString(pkgName).WString("\n\n").
WString("import \"net/http\"\n\n")

kvs, err := make.GetStatuses()
kvs, err := status.Get()
if err != nil {
panic(err)
}
Expand All @@ -38,7 +38,7 @@ func main() {
}
}

func makeStatuses(buf *errwrap.Buffer, kvs []make.Pair) {
func makeStatuses(buf *errwrap.Buffer, kvs []status.Pair) {
buf.WString("var problemStatuses=map[int]struct{}{\n")

for _, item := range kvs {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

package make
package status

import (
"testing"
Expand All @@ -11,7 +11,7 @@ import (
func TestGetStatuses(t *testing.T) {
a := assert.New(t, false)

status, err := GetStatuses()
status, err := Get()
a.NotError(err).NotEmpty(status).
True(status[0].Value > 399)

Expand Down
11 changes: 11 additions & 0 deletions internal/status/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT

//go:generate go run ./make_statuses.go

// Package status 用于处理与状态码相关的功能
package status

func IsProblemStatus(status int) bool {
_, found := problemStatuses[status]
return found
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 此文件由工具产生,请勿手动修改!

package problems
package status

import "net/http"

Expand Down
3 changes: 0 additions & 3 deletions locales/und.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,6 @@ messages:
- key: the client miss content-type header
message:
msg: the client miss content-type header
- key: the response is empty
message:
msg: the response is empty
- key: the server miss content-type header
message:
msg: the server miss content-type header
Expand Down
3 changes: 0 additions & 3 deletions locales/zh-CN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,6 @@ messages:
- key: the client miss content-type header
message:
msg: 客户端未指定 Content-Type 报头
- key: the response is empty
message:
msg: 未返回任何内容
- key: the server miss content-type header
message:
msg: 服务端未指定 Content-Type 报头
Expand Down
17 changes: 7 additions & 10 deletions make_problems.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/issue9/errwrap"
"github.com/issue9/source"

"github.com/issue9/web/internal/problems/make"
"github.com/issue9/web/internal/status"
)

const (
Expand All @@ -20,14 +20,11 @@ const (

func main() {
buf := &errwrap.Buffer{}
buf.WString(make.FileHeader).
buf.WString(status.FileHeader).
WString("package ").WString(pkgName).WString("\n\n").
WString("import (\n").
WString("\"net/http\"\n\n").
WString("\"github.com/issue9/web/internal/problems\"\n").
WString(")\n\n")
WString("import \"net/http\"\n\n")

kvs, err := make.GetStatuses()
kvs, err := status.Get()
if err != nil {
panic(err)
}
Expand All @@ -44,18 +41,18 @@ func main() {
}
}

func makeID(buf *errwrap.Buffer, kvs []make.Pair) {
func makeID(buf *errwrap.Buffer, kvs []status.Pair) {
buf.WString("const(\n")

buf.WString(`ProblemAboutBlank = problems.AboutBlank`).WString("\n\n")
buf.WString(`ProblemAboutBlank = "about:blank"`).WString("\n\n")
for _, item := range kvs {
buf.Printf("%s=\"%s\"\n", item.ID(), strconv.Itoa(item.Value))
}

buf.WString(")\n\n")
}

func makeIDs(buf *errwrap.Buffer, kvs []make.Pair) {
func makeIDs(buf *errwrap.Buffer, kvs []status.Pair) {
buf.WString("var problemsID=map[int]string{\n")

for _, item := range kvs {
Expand Down
2 changes: 1 addition & 1 deletion problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (ctx *Context) Problem(id string) Problem { return ctx.initProblem(newRFC78

func (ctx *Context) initProblem(p *RFC7807, id string) Problem {
pp := ctx.LocalePrinter()
ctx.Server().InitProblem(p, id, pp)
ctx.Server().Problems().Init(p, id, pp)
return p.WithInstance(ctx.ID())
}

Expand Down
Loading

0 comments on commit 13e5b1c

Please sign in to comment.