# README
web
web 是一个比较完整的 API 开发框架,相对于简单的路由,提供了更多的便利功能。 如果你只是需要一个简单的路由工具,那么你可以移步到 mux。
package main
import "github.com/issue9/web"
import "github.com/issue9/web/server"
// main.go
func main() {
srv, err := server.New("web", "1.0.0", &server.Options{})
router := srv.Routers().New()
router.Get("/admins", getAdmins).
Get("/groups", getGroups)
srv.Serve()
}
func getAdmins(ctx* web.Context) web.Responser {
return ctx.NotImplemented()
}
func getGroups(ctx* web.Context) web.Responser {
return ctx.NotImplemented()
}
字符集和文档类型
https://www.iana.org/assignments/character-sets/character-sets.xhtml 中列出的字符集都能自动转换。
文档类型由 Server.Mimetypes
指定。
package main
import (
"github.com/issue9/web"
"github.com/issue9/web/server"
"github.com/issue9/web/mimetype/json"
"github.com/issue9/web/mimetype/xml"
)
srv := server.New("app", "1.0.0", &server.Options{
Codec: web.NewCodec().
AddMimetype(xml.Mimetype, json.Marshal, json.Unmarshal, xml.ProblemMimetype).
AddMimetype(xml.Mimetype, xml.Marshal, xml.Unmarshal, xml.ProblemMimetype)
}
})
srv.Serve()
客户端只要在请求时设置 Accept 报头就可返回相应类型的数据,而 Accept-Charset 报头可设置接收的字符集。 Content-Type 则可以有向服务器指定提交内容的文档类型和字符集。
错误处理
框架根据 RFC7807 提供了一种输出错误信息内容的机制。
插件
- https://github.com/issue9/webuse 提供了常用的中间件和插件。
- https://github.com/issue9/webfilter 提供了常用的验证和修正数据的方法。
工具
https://github.com/issue9/web/releases 提供了一个简易的辅助工具。可以帮助用户完成以下工作:
- 提取和更新本地化信息;
- 生成 openapi 文档;
- 热编译项目;
macOS 和 linux 用户可以直接使用 brew 进行安装:
brew tap caixw/brew
brew install caixw/brew/web
版权
# Packages
Package compressor 提供了压缩算法的实现.
Package comptime 提供了编译期相关的处理方式
go 以 //go:build 的方式区别编译内容,目前支持以下标签: - development 表示开发环境,[Mode] 会被赋为 [Development]; - 其它情况下,则 [Mode] 的值永远是 [Production];
在 go build 命令行中可以使用 -tags=development 指定为开发模式, 或是使用 [web] 时添加 dev 参数;
[web] https://pkg.go.dev/github.com/issue9/web/cmd/web.
Package filter 过滤器
包含了数据验证和数据修正两个功能。
各个类型之间的关系如下:
|---[Sanitize] | [Filter]---[Builder]----[Rule] | |---[Validator]
Sanitize 表示对数据的修正,其函数原型为:func(*T) 指针传入数据,实现方可以对指向的数据进行修改,可由 [S]、[SS] 或 [MS] 转换为 [Rule];
Validator 负责验证数据,其原型为:func(T)bool 返回值表示是否符合当前函数的需求,可由 [V]、[SV] 或 [MV] 转换为 [Rule];.
Package locales 为 web 包提供了本地化的内容.
Package mimetype 对媒体类型的编解码处理.
Package openapi 采用 [web.Middleware] 中间件的形式生成 [openapi] 文档
# struct tag
定义了以下几种标签: - [CommentTag] 用于可翻译的注释,该内容会被翻译后保存在 [Schema.Description] 中; - openapi 对 openapi 类型的自定义,格式为 type,format,可以自定义字段的类型和格式;
# Schema
大部分情况下,通过 [NewSchema] 可以将一个类型直接转换为 [Schema] 对象, 如果对类型中的字段有特殊要求,可以通过结构体标签 openapi 进行简单的自定义:
type State int8
type x struct { State State `openapi:"string"` }
以上代码会将 State 解析为字符串类型,而不是默认的数值。
当然对于复杂的需求,还可以通过实现 [OpenAPISchema] 接口实现自定义:
type State int8
func(s State) OpenAPISchema(s *Schema) { s.Type = TypeString s.Enum = []string {"stopped", "running" } }
以上代码,会在解析 State 始终采用 [OpenAPISchema] 修改。
结构体标签的优先级要高于 [OpenAPISchema] 接口。
[openapi]: https://spec.openapis.org/oas/v3.1.1.html.
Package selector 提供负载均衡的相关功能.
Package server 提供与服务端实现相关的功能
目前实现了三种类型的服务端: - [NewHTTP] 构建普通的 HTTP 服务; - [NewGateway] 构建微服务的网关服务; - [NewService] 构建微服务;.
# Functions
No description provided by the author
ErrExitContext 当已经退出 [Context] 对象时还对其进行操作将返回此错误.
GetAppVersion 获得应用的版本号
如果当前应用没有指定版本号,则采用 v 作为返回值。
NOTE: 只有 go1.24 及之后且 -buildinfo 参数不为 false 编译的程序才会带版本信息,否则始终返回 v。.
InternalNewServer 声明 [InternalServer]
s 为实际的 [Server] 接口对象; requestIDKey 表示客户端提交的 X-Request-ID 报头名; problemPrefix 可以为空; onRender 在每个对象的渲染之前可以对内容进行的修改;
NOTE: 此为内部使用函数,由调用者保证参数的正确性。
NOTE: [Server] 的实现者,不应该重新实现 [InternalServer] 已经实现的接口, 否则可能出现 [InternalServer] 中的调用与 [Server] 的实现调用不同的问题。 比如重新实现了 [Server.Location],那么将出现 [InternalServer] 内部的 Location 与 新实现的 Location 返回不同值的情况。.
IsProblem 这是表示错误的状态码.
KeepAlive 保持当前会话不退出.
NewCache 声明带有统一前缀的缓存接口.
NewClient 创建 [Client] 实例
client 如果为空,表示采用 &http.Client{} 作为默认值; marshalName 和 marshal 表示编码的名称和方法; requestIDKey 表示 x-request-id 的报头名称,如果为空则表示不需要; requestIDGen 表示生成 x-request-id 值的方法;.
NewCodec 声明 [Codec] 对象.
NewError 用 HTTP 状态码包装一个错误信息
status 表示 HTTP 状态码; err 被包装的错误信息,如果是空值,将会 panic;
此方法返回的错误,在 [Context.Error] 和 [WithRecovery] 中会被识别且按指定的状态码输出。.
NewFieldError 返回表示配置文件错误的对象
field 表示错误的字段名; msg 表示错误信息,可以是任意类型,如果 msg 是 [FieldError] 类型, 那么此操作相当于调用了 [FieldError.AddFieldParent];.
NewLocaleError 本地化的错误信息.
NewStackError 为 err 带上调用信息
位置从调用 NewStackError 开始。如果 err 为 nil,则返回 nil。 多次调用 NewStackError 包装,则返回第一次包装的返回值。.
No description provided by the author
NotModified 决定何时可返回 304 状态码
etag 返回当前内容关联的 ETag 报头内容,其原型为:
func()(etag string, weak bool)
etag 表示对应的 etag 报头,需要包含双绰号,但是不需要 W/ 前缀,weak 是否为弱验证。
body 获取返回给客户端的报文主体对象,其原型如下:
func()(body any, err error)
如果返回 body 的是 []byte 类型,会原样输出,其它类型则按照 [Context.Marshal] 进行转换成 []byte 之后输出。 body 可能参与了 etag 的计算,为了防止重复生成 body,所以此函数允许 body 直接为 []byte 类型, 在不需要 body 参与计算 etag 的情况下,应该返回非 []byte 类型的 body。.
OK 返回 200 状态码下的对象.
Phrase 生成本地化的语言片段.
Redirect 重定向至新的 URL.
Response 输出状态和对象至客户端
body 表示需要输出的对象,该对象最终会被转换成相应的编码; kv 为报头,必须以偶数数量出现,奇数位为报头名,偶数位为对应的报头值;.
SprintError 将 err 转换为字符串
p 如果 err 实现了 [LocaleStringer],将采用 p 进行转换; detail 是否输出调用堆栈;.
Status 仅向客户端输出状态码和报头
kv 为报头,必须以偶数数量出现,奇数位为报头名,偶数位为对应的报头值;
NOTE: 即使 code 为 400 等错误代码,当前函数也不会返回 [Problem] 对象。.
WithAllowedCORS 允许跨域请求.
No description provided by the author
WithCORS 自定义跨域请求设置项
具体参数可参考 [mux.WithCORS]。.
WithDenyCORS 禁用跨域请求.
No description provided by the author
No description provided by the author
WithRecovery 在路由奔溃之后的处理方式
相对于 [mux.WithRecovery],提供了对 [NewError] 错误的处理。.
WithTrace 控制 TRACE 请求是否有效
body 表示是否显示 body 内容;.
WithURLDomain 为 [Router.URL] 生成的地址带上域名.
No description provided by the author
# Constants
出错,不再执行后续操作.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
正在运行.
停止状态,默认状态.
Version 当前框架的版本.
# Structs
Client 用于访问远程的客户端
NOTE: 远程如果不是 [Server] 实现的服务,可能无法正确处理返回对象。.
Codec 编码解码工具
包含了压缩方法和媒体类型的处理.
Context 根据当次 HTTP 请求生成的上下文内容
Context 同时也实现了 [http.ResponseWriter] 接口, 但是不推荐非必要情况下直接使用 [http.ResponseWriter] 的接口方法, 而是采用返回 [Responser] 的方式向客户端输出内容。.
FilterContext 处理由过滤器生成的各错误.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Queries 提供对查询参数的处理.
No description provided by the author
No description provided by the author
# Type aliases
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Paths 提供对路径参数的处理.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author