Documentation
¶
Overview ¶
Package context 用于处理单个请求的上下文关系。
Index ¶
- type Context
- func (ctx *Context) Body() (body []byte, err error)
- func (ctx *Context) ClientIP() string
- func (ctx *Context) Critical(status int, v ...interface{})
- func (ctx *Context) Criticalf(status int, format string, v ...interface{})
- func (ctx *Context) Error(status int, v ...interface{})
- func (ctx *Context) Errorf(status int, format string, v ...interface{})
- func (ctx *Context) Exit(status int)
- func (ctx *Context) Fprint(w io.Writer, v ...interface{}) (int, error)
- func (ctx *Context) Fprintf(w io.Writer, key message.Reference, v ...interface{}) (int, error)
- func (ctx *Context) Fprintln(w io.Writer, v ...interface{}) (int, error)
- func (ctx *Context) Marshal(status int, v interface{}, headers map[string]string) error
- func (ctx *Context) NewResult(code int) *Result
- func (ctx *Context) ParamID(key string) (int64, error)
- func (ctx *Context) ParamInt64(key string) (int64, error)
- func (ctx *Context) ParamString(key string) (string, error)
- func (ctx *Context) Params() *Params
- func (ctx *Context) Print(v ...interface{}) (int, error)
- func (ctx *Context) Printf(key message.Reference, v ...interface{}) (int, error)
- func (ctx *Context) Println(v ...interface{}) (int, error)
- func (ctx *Context) Queries() *Queries
- func (ctx *Context) QueryObject(v interface{}) (errors map[string]string)
- func (ctx *Context) Read(v interface{}) (ok bool)
- func (ctx *Context) Render(status int, v interface{}, headers map[string]string)
- func (ctx *Context) ServeContent(buf io.ReadSeeker, name string, headers map[string]string)
- func (ctx *Context) ServeFile(path, name string, headers map[string]string)
- func (ctx *Context) Sprint(v ...interface{}) string
- func (ctx *Context) Sprintf(key message.Reference, v ...interface{}) string
- func (ctx *Context) Sprintln(v ...interface{}) string
- func (ctx *Context) Unmarshal(v interface{}) error
- func (ctx *Context) Upload(field string, u *upload.Upload) ([]string, error)
- type Params
- func (p *Params) Bool(key string) bool
- func (p *Params) Errors() map[string]string
- func (p *Params) Float64(key string) float64
- func (p *Params) HasErrors() bool
- func (p *Params) ID(key string) int64
- func (p *Params) Int(key string) int
- func (p *Params) Int64(key string) int64
- func (p *Params) MustBool(key string, def bool) bool
- func (p *Params) MustFloat64(key string, def float64) float64
- func (p *Params) MustID(key string, def int64) int64
- func (p *Params) MustInt(key string, def int) int
- func (p *Params) MustInt64(key string, def int64) int64
- func (p *Params) MustString(key, def string) string
- func (p *Params) Result(code int) *Result
- func (p *Params) String(key string) string
- type Queries
- func (q *Queries) Bool(key string, def bool) bool
- func (q *Queries) Errors() map[string]string
- func (q *Queries) Float64(key string, def float64) float64
- func (q *Queries) HasErrors() bool
- func (q *Queries) Int(key string, def int) int
- func (q *Queries) Int64(key string, def int64) int64
- func (q *Queries) Result(code int) *Result
- func (q *Queries) String(key, def string) string
- type Result
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct { App *app.App Response http.ResponseWriter Request *http.Request // 指定输出时所使用的媒体类型,以及名称 OutputMimeType mimetype.MarshalFunc OutputMimeTypeName string // 输出到客户端的字符集 // // 若值为 encoding.Nop 或是空,表示为 utf-8 OutputCharset encoding.Encoding OutputCharsetName string // 客户端内容所使用的媒体类型。 InputMimeType mimetype.UnmarshalFunc // 客户端内容所使用的字符集 // // 若值为 encoding.Nop 或是空,表示为 utf-8 InputCharset encoding.Encoding // 输出语言的相关设置项。 OutputTag language.Tag LocalePrinter *message.Printer // contains filtered or unexported fields }
Context 是对当前请求内容的封装,仅与当前请求相关。
func New ¶
New 根据当前请求内容生成 Context 对象
如果 Accept 的内容与当前配置无法匹配, 则退出(panic)并输出 NotAcceptable 状态码。
mt 为 mimetype.Mimetypes 对象,用于从中查找指定名称的 mimetype 转码函数。
errlog 为错误信息输出通道,在 New() 非正常退出时,除了输出一个 HTTP 的状态码之外, 若还指定了 errlog,则还会将错误信息输出到该通道上,为 nil,则不输出任何错误信息。
NOTE: New 仅供框架内部使用,不保证兼容性。如果框架提供的 Context 不符合你的要求,那么请直接使用 &Context{} 指定相关的值构建对象。
func (*Context) ClientIP ¶
ClientIP 返回客户端的 IP 地址。
获取顺序如下:
- X-Forwarded-For 的第一个元素
- Remote-Addr 报头
- X-Read-IP 报头
func (*Context) Critical ¶
Critical 输出一条日志到 CRITICAL 日志通道,并向用户输出一个指定状态码的页面。
若是输出日志的过程中出错,则 panic 若没有错误信息,则仅向客户端输出一条状态码信息。
func (*Context) Criticalf ¶
Criticalf 输出一条日志到 CRITICAL 日志通道,并向用户输出一个指定状态码的页面。
若是输出日志的过程中出错,则 panic 若没有错误信息,则仅向客户端输出一条状态码信息。
func (*Context) Error ¶
Error 输出一条日志到 ERROR 日志通道,并向用户输出一个指定状态码的页面。
若是输出日志的过程中出错,则 panic 若没有错误信息,则仅向客户端输出一条状态码信息。
func (*Context) Errorf ¶
Errorf 输出一条日志到 ERROR 日志通道,并向用户输出一个指定状态码的页面。
若是输出日志的过程中出错,则 panic 若没有错误信息,则仅向客户端输出一条状态码信息。
func (*Context) Exit ¶
Exit 以指定的状态码退出当前协程
status 表示输出的状态码,如果为 0,则不会作任何状态码输出。
Exit 最终是以 panic 的形式退出,所以如果你的代码里截获了 panic, 那么 Exit 并不能达到退出当前请求的操作。
与 Error 的不同在于: Error 不会主动退出当前协程,而 Exit 则会触发 panic,退出当前协程。
func (*Context) Marshal ¶
Marshal 将 v 解码并发送给客户端。
若 v 是一个 nil 值,则不会向客户端输出任何内容; 若是需要正常输出一个 nil 类型到客户端(JSON 中会输出 null), 可以使用 mimetype.Nil 变量代替。
NOTE: 如果需要指定一个特定的 Content-Type 和 Content-Language, 可以在 headers 中指定,否则使用当前的编码和语言名称。
func (*Context) ParamID ¶
ParamID 获取地址参数中表示 ID 的值。相对于 ParamInt64,该值必须大于 0。
NOTE: 若需要获取多个参数,可以使用 Context.Params 获取会更方便。
func (*Context) ParamInt64 ¶
ParamInt64 取地址参数中的 int64 值。
NOTE: 若需要获取多个参数,可以使用 Context.Params 获取会更方便。
func (*Context) ParamString ¶
ParamString 取地址参数中的 string 值。
NOTE: 若需要获取多个参数,可以使用 Context.Params 获取会更方便。
func (*Context) QueryObject ¶
QueryObject 将查询参数解析到一个对象中。
返回的是每一个字段对应的错误信息。
具体的文档信息可以参考 https://github.com/issue9/query
func (*Context) Read ¶
Read 从客户端读取数据并转换成 v 对象。
功能与 Unmarshal() 相同,只不过 Read() 在出错时, 会直接调用 Error() 处理:输出 422 的状态码, 并返回一个 false,告知用户转换失败。
func (*Context) Render ¶
Render 将 v 渲染给客户端。
功能与 Marshal() 相同,只不过 Render() 在出错时, 会直接调用 Error() 处理,输出 500 的状态码。
如果需要具体控制出错后的处理方式,可以使用 Marshal 函数。
func (*Context) ServeContent ¶ added in v0.21.0
ServeContent 将一块内存中的内容转换为文件提供下载
文件可能提供连续的下载功能,其状态码是未定的, 所以提供了一个类似于 Render 的变体专门用于下载功能。
buf 实现 io.ReadSeeker 接口的内存块; name 下载时,显示的文件; headers 文件报头内容。
func (*Context) ServeFile ¶ added in v0.21.0
ServeFile 提供文件下载
文件可能提供连续的下载功能,其状态码是未定的, 所以提供了一个类似于 Render 的变体专门用于下载功能。
path 指向本地文件的地址; name 下载时,显示的文件,若为空,则直接使用 path 中的文件名部分; headers 额外显示的报头内容。
type Params ¶
type Params struct {
// contains filtered or unexported fields
}
Params 用于处理路径中包含的参数。
p := ctx.Params() aid := p.Int64("aid") bid := p.Int64("bid") if p.HasErrors() { // do something return }
func (*Params) Bool ¶
Bool 获取参数 key 所代表的值,并转换成 bool。
最终会调用 strconv.ParseBool 进行转换, 也只有该方法中允许的字符串会被正确转换。
func (*Params) MustBool ¶
MustBool 获取参数 key 所代表的值,并转换成 bool。 若不存在或是转换出错,则返回 def 作为其默认值。 仅在类型转换出错时,才会向 errors 写入错误信息。
最终会调用 strconv.ParseBool 进行转换, 也只有该方法中允许的字符串会被正确转换。
func (*Params) MustFloat64 ¶
MustFloat64 获取参数 key 所代表的值,并转换成 float64。 若不存在或是转换出错,则返回 def 作为其默认值。 仅在类型转换出错时,才会向 errors 写入错误信息。
func (*Params) MustID ¶
MustID 获取参数 key 所代表的值,转换成 int64 且必须大于 0。 若不存在或是转换出错,则返回 def 作为其默认值。 仅在类型转换出错或是小于零时,才会向 errors 写入错误信息。
func (*Params) MustInt ¶
MustInt 获取参数 key 所代表的值,并转换成 int。 若不存在或是转换出错,则返回 def 作为其默认值。 仅在类型转换出错时,才会向 errors 写入错误信息。
func (*Params) MustInt64 ¶
MustInt64 获取参数 key 所代表的值,并转换成 int64。 若不存在或是转换出错,则返回 def 作为其默认值。 仅在类型转换出错时,才会向 errors 写入错误信息。
func (*Params) MustString ¶
MustString 获取参数 key 所代表的值,并转换成 string。 若不存在或是转换出错,则返回 def 作为其默认值。
type Queries ¶
type Queries struct {
// contains filtered or unexported fields
}
Queries 用于处理路径中的查询参数。
q,_ := ctx.Queries() page := q.Int64("page", 1) size := q.Int64("size", 20) if q.HasErrors() { // do something return }
type Result ¶ added in v0.21.0
type Result struct { XMLName struct{} `json:"-" xml:"result" yaml:"-"` Message string `json:"message" xml:"message,attr" yaml:"message"` Code int `json:"code" xml:"code,attr" yaml:"code"` Detail []*detail `json:"detail,omitempty" xml:"field,omitempty" yaml:"detail,omitempty"` // contains filtered or unexported fields }
Result 定义了出错时,向客户端返回的结构体。支持以下格式:
JSON:
{ 'message': 'error message', 'code': 4000001, 'detail':[ {'field': 'username': 'message': '已经存在相同用户名'}, {'field': 'username': 'message': '已经存在相同用户名'}, ] }
XML:
<result code="400" message="error message"> <field name="username">已经存在相同用户名</field> <field name="username">已经存在相同用户名</field> </result>
YAML:
message: 'error message' code: 40000001 detail: - field: username message: 已经存在相同用户名 - field: username message: 已经存在相同用户名
FormData:
message=errormessage&code=4000001&detail.username=message&detail.username=message
func (*Result) MarshalForm ¶ added in v0.21.0
MarshalForm 为 form.Marshaler 接口实现。用于将 result 对象转换成 form 数据格式