content

package
v0.43.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2021 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package content 与生成内容相关的功能

Index

Constants

View Source
const DefaultCharset = "utf-8"

DefaultCharset 默认的字符集

View Source
const DefaultIndexPage = "index.html"

DefaultIndexPage ServeFileFS index 参数的默认值

View Source
const DefaultMimetype = "application/octet-stream"

DefaultMimetype 默认的媒体类型

在不能获取输入和输出的媒体类型时,会采用此值作为其默认值。

Variables

This section is empty.

Functions

This section is empty.

Types

type BuildResultFunc added in v0.40.0

type BuildResultFunc func(status, code int, message string) Result

BuildResultFunc 用于生成 Result 接口对象的函数

type Content added in v0.40.0

type Content struct {
	// contains filtered or unexported fields
}

Content 管理反馈给用户的数据

func New added in v0.40.0

func New(builder BuildResultFunc, loc *time.Location, locale *serialization.Locale, tag language.Tag) *Content

New 返回 *Content 实例

locale 本地化数据,context 从此处查找对应的本地化信息; tag 默认的本地化语言标签,context 查找不到数据时采用此值,同时也作为 context 实例的默认输出语言。

func (*Content) AddResult added in v0.40.0

func (c *Content) AddResult(status, code int, phrase localeutil.LocaleStringer)

AddResult 添加一条错误信息

status 指定了该错误代码反馈给客户端的 HTTP 状态码;

func (*Content) AddResults added in v0.42.0

func (c *Content) AddResults(messages map[int]localeutil.LocaleStringer)

AddResults 添加多条错误信息

键名为错误信息的数字代码,键值是具体的错误信息描述。 同时键名向下取整,直到三位长度的整数作为其返回给客户端的状态码。

func (*Content) Files added in v0.41.0

func (c *Content) Files() *serialization.Files

Files 返回用于序列化文件内容的操作接口

func (*Content) Locale added in v0.41.0

func (c *Content) Locale() *serialization.Locale

func (*Content) LocalePrinter added in v0.41.0

func (c *Content) LocalePrinter() *message.Printer

func (*Content) Location added in v0.43.0

func (c *Content) Location() *time.Location

func (*Content) Mimetypes added in v0.41.0

func (c *Content) Mimetypes() *serialization.Mimetypes

Mimetypes 管理 mimetype 的序列化操作

func (*Content) NewContext added in v0.40.0

func (c *Content) NewContext(l *log.Logger, w http.ResponseWriter, r *http.Request) (*Context, int)

NewContext 从用户请求中构建一个 Context 实例

如果不合规则,会以指定的状码返回并向 l 输出信息。

func (*Content) Result added in v0.40.0

func (c *Content) Result(p *message.Printer, code int, fields ResultFields) Result

Result 返回 Result 实例

如果找不到 code 对应的错误信息,则会直接 panic。 fields 表示明细字段,可以为空,之后通过 Result.Add 添加。

func (*Content) Results added in v0.40.0

func (c *Content) Results(p *message.Printer) map[int]string

Results 错误信息列表

p 用于返回特定语言的内容。

func (*Content) Tag added in v0.41.0

func (c *Content) Tag() language.Tag

type Context added in v0.40.0

type Context struct {
	Response http.ResponseWriter
	Request  *http.Request

	// 指定输出时所使用的媒体类型,以及名称
	OutputMimetype     serialization.MarshalFunc
	OutputMimetypeName string

	// 输出到客户端的字符集
	//
	// 若值为 encoding.Nop 或是空,表示为 utf-8
	OutputCharset     encoding.Encoding
	OutputCharsetName string

	// 客户端内容所使用的媒体类型
	InputMimetype serialization.UnmarshalFunc

	// 客户端内容所使用的字符集
	//
	// 若值为 encoding.Nop 或是空,表示为 utf-8
	InputCharset encoding.Encoding

	// 输出语言的相关设置项
	OutputTag     language.Tag
	LocalePrinter *message.Printer

	// 与当前对话相关的时区
	Location *time.Location
	// contains filtered or unexported fields
}

Context 单次请求生成的上下文数据

NOTE: 用户不应该直接引用该对象,而是 server.Context。

func (*Context) Body added in v0.40.0

func (ctx *Context) Body() (body []byte, err error)

Body 获取用户提交的内容

相对于 ctx.Request.Body,此函数可多次读取。不存在 body 时,返回 nil

func (*Context) Fprint added in v0.40.0

func (ctx *Context) Fprint(w io.Writer, v ...interface{}) (int, error)

Fprint 相当于 ctx.LocalePrinter.Fprint

func (*Context) Fprintf added in v0.40.0

func (ctx *Context) Fprintf(w io.Writer, key message.Reference, v ...interface{}) (int, error)

Fprintf 相当于 ctx.LocalePrinter.Fprintf

func (*Context) Fprintln added in v0.40.0

func (ctx *Context) Fprintln(w io.Writer, v ...interface{}) (int, error)

Fprintln 相当于 ctx.LocalePrinter.Fprintln

func (*Context) Marshal added in v0.40.0

func (ctx *Context) Marshal(status int, v interface{}, headers map[string]string) error

Marshal 将 v 解码并发送给客户端

status 表示输出的状态码,如果出错,可能输出的不是该状态码; v 输出的对象,若是一个 nil 值,则不会向客户端输出任何内容; 若是需要正常输出一个 nil 类型到客户端(比如JSON 中的 null), 可以传递一个 *struct{} 值,或是自定义实现相应的解码函数; headers 报头信息,如果已经存在于 ctx.Response 将覆盖 ctx.Response 中的值, 如果需要指定一个特定的 Content-Type 和 Content-Language, 可以在 headers 中指定,否则使用当前的编码和语言名称;

func (*Context) Print added in v0.40.0

func (ctx *Context) Print(v ...interface{}) (int, error)

Print 相当于 ctx.LocalePrinter.Print

func (*Context) Printf added in v0.40.0

func (ctx *Context) Printf(key message.Reference, v ...interface{}) (int, error)

Printf 相当于 ctx.LocalePrinter.Printf

func (*Context) Println added in v0.40.0

func (ctx *Context) Println(v ...interface{}) (int, error)

Println 相当于 ctx.LocalePrinter.Println

func (*Context) ServeContent added in v0.40.0

func (ctx *Context) ServeContent(buf io.ReadSeeker, name string, mod time.Time, headers map[string]string)

ServeContent 将一块内存中的内容转换为文件提供下载

功能与 http.ServeContent 相同,提供了可自定义报头的功能。

func (*Context) ServeFS added in v0.40.0

func (ctx *Context) ServeFS(f fs.FS, p, index string, headers map[string]string) error

ServeFS 提供基于 fs.FS 的文件下载服

p 表示文件地址,用户应该保证 p 的正确性; 如果 p 是目录,则会自动读 p 目录下的 index 文件, 如果 index 为空,则采用 DefaultIndexPage 作为其默认值。

func (*Context) Sprint added in v0.40.0

func (ctx *Context) Sprint(v ...interface{}) string

Sprint 相当于 ctx.LocalePrinter.Sprint

func (*Context) Sprintf added in v0.40.0

func (ctx *Context) Sprintf(key message.Reference, v ...interface{}) string

Sprintf 相当于 ctx.LocalePrinter.Sprintf

func (*Context) Sprintln added in v0.40.0

func (ctx *Context) Sprintln(v ...interface{}) string

Sprintln 相当于 ctx.LocalePrinter.Sprintln

func (*Context) Unmarshal added in v0.40.0

func (ctx *Context) Unmarshal(v interface{}) error

Unmarshal 将提交的内容转换成 v 对象

func (*Context) Upload added in v0.40.0

func (ctx *Context) Upload(field string, u *upload.Upload) ([]string, error)

Upload 执行上传文件的相关操作

返回的是文件列表

type Result added in v0.40.0

type Result interface {
	// Add 添加详细的错误信息
	//
	// 相同的 key 应该能关联多个 val 值。
	Add(key string, val ...string)

	// Set 设置详细的错误信息
	//
	// 如果已经相同的 key,会被覆盖。
	Set(key string, val ...string)

	// HasFields 是否存在详细的错误信息
	//
	// 如果有通过 Add 添加内容,那么应该返回 true
	HasFields() bool

	// Status HTTP 状态码
	//
	// 最终会经此值作为 HTTP 状态会返回给用户
	Status() int
}

Result 自定义错误代码的实现接口

一般是对客户端提交数据 400 的具体反馈信息。 用户可以根据自己的需求,展示自定义的错误码以及相关的错误信息格式。 该对象最终也是调用 MarshalFunc 进行解码输出。 只要该对象同时实现了 Result 接口即可。

比如类似以下的错误内容:

{
    'message': 'error message',
    'code': 4000001,
    'detail':[
        {'field': 'username': 'message': '已经存在相同用户名'},
        {'field': 'username': 'message': '已经存在相同用户名'},
    ]
}

func DefaultBuilder added in v0.40.0

func DefaultBuilder(status, code int, message string) Result

DefaultBuilder 默认的 BuildResultFunc 实现

定义了以下格式的返回信息:

JSON:

{
    'message': 'error message',
    'code': 4000001,
    'fields':[
        {'name': 'username': 'message': ['名称过短', '不能包含特殊符号']},
        {'name': 'password': 'message': ['不能为空']},
    ]
}

XML:

<result code="400">
    <message>error message</message>
    <field name="username">
        <message>名称过短</message>
        <message>不能包含特殊符号</message>
    </field>
    <field name="password"><message>不能为空</message></field>
</result>

YAML:

message: 'error message'
code: 40000001
fields:
  - name: username
    message:
      - 名称过短
      - 不能包含特殊符号
  - name: password
    message:
      - 不能为空

FormData:

message=errormessage&code=4000001&fields.username=名称过短&fields.username=不能包含特殊符号&fields.password=不能为空

type ResultFields added in v0.42.0

type ResultFields = validation.Messages

ResultFields 表示字段的错误信息列表

原始类型为 map[string][]string

Directories

Path Synopsis
Package form 用于处理 www-form-urlencoded 编码 func read(ctx *web.Context) { vals := urls.Values{} ctx.Read(vals) } func write(ctx *web.Context) { vals := urls.Values{} vals.Add("name", "caixw") ctx.Render(http.StatusOK, vals, nil) } form 用户可以通过定义 form 标签自定义输出的名称,比如: type Username struct { Name string `form:"name"` Age int } 转换成 form-data 可能是以下样式: name=jjj&age=18 该方式对数据类型有一定限制: 1.
Package form 用于处理 www-form-urlencoded 编码 func read(ctx *web.Context) { vals := urls.Values{} ctx.Read(vals) } func write(ctx *web.Context) { vals := urls.Values{} vals.Add("name", "caixw") ctx.Render(http.StatusOK, vals, nil) } form 用户可以通过定义 form 标签自定义输出的名称,比如: type Username struct { Name string `form:"name"` Age int } 转换成 form-data 可能是以下样式: name=jjj&age=18 该方式对数据类型有一定限制: 1.
Package gob 提供 GOB 格式的编解码
Package gob 提供 GOB 格式的编解码
Package html 提供输出 HTML 内容的 content.MarshalFunc 函数 mt := content.NewContent() tpl := template.ParseFiles(...) mgr := html.New(tpl) mt.Add("text/html", mgr.Marshal, nil) func handle(ctx *web.Context) Responser { return Object(200, html.Tpl("index", map[string]interface{}{...}), nil) }
Package html 提供输出 HTML 内容的 content.MarshalFunc 函数 mt := content.NewContent() tpl := template.ParseFiles(...) mgr := html.New(tpl) mt.Add("text/html", mgr.Marshal, nil) func handle(ctx *web.Context) Responser { return Object(200, html.Tpl("index", map[string]interface{}{...}), nil) }
Package text 针对文本内容的编解码实现
Package text 针对文本内容的编解码实现
testobject
Package testobject 用于测试 mimetype 的对象
Package testobject 用于测试 mimetype 的对象

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL