server

package
v0.39.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2021 License: MIT Imports: 41 Imported by: 2

Documentation

Index

Constants

View Source
const ModuleFuncName = "Module"

ModuleFuncName 插件中的用于获取模块信息的函数名

NOTE: 必须为可导出的函数名称

Variables

View Source
var ErrInited = errors.New("模块已经初始化")

ErrInited 当模块被多次初始化时返回此错误

Functions

func ExitContext

func ExitContext(status int)

ExitContext 以指定的状态码退出当前协程

status 表示输出的状态码,如果为 0,则不会作任何状态码输出。

ExitContext 最终是以 panic 的形式退出,所以如果你的代码里截获了 panic, 那么 ExitContext 并不能达到退出当前请求的操作。

与 Contet.Error 的不同在于: Contet.Error 不会主动退出当前协程,而 ExitContext 则会触发 panic,退出当前协程。

Types

type CTXSanitizer

type CTXSanitizer interface {
	CTXSanitize(*Context) result.Fields
}

CTXSanitizer 提供对数据的验证和修正

但凡对象实现了该接口,那么在 Context.Read 和 Queries.Object 中会在解析数据成功之后,调用该接口进行数据验证。

type Context

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

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

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

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

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

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

	// 与当前对话相关的时区
	Location *time.Location

	// 保存 Context 在存续期间的可复用变量
	//
	// 这是比 context.Value 更经济的传递变量方式。
	//
	// 如果仅需要在多个请求中传递参数,可直接使用 Server.Vars。
	Vars map[interface{}]interface{}
	// contains filtered or unexported fields
}

Context 是对当次 HTTP 请求内容的封装

func NewContext

func NewContext(w http.ResponseWriter, r *http.Request) *Context

NewContext 构建 *Context 实例

func (*Context) Body

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

Body 获取用户提交的内容

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

func (*Context) ClientIP

func (ctx *Context) ClientIP() string

ClientIP 返回客户端的 IP 地址

NOTE: 包含了端口部分。

获取顺序如下:

  • X-Forwarded-For 的第一个元素
  • Remote-Addr 报头
  • X-Read-IP 报头

func (*Context) Created

func (ctx *Context) Created(v interface{}, location string)

Created 201

func (*Context) Critical

func (ctx *Context) Critical(status int, v ...interface{})

Critical 输出日志到 CRITICAL 通道并向用户输出指定状态码的页面

若是输出日志的过程中出错,则 panic 若没有错误信息,则仅向客户端输出一条状态码信息。

输出的内容,会根据 status 的值,在 errorhandler 中查找相应的响应内容, 即使该值小于 400。

func (*Context) Criticalf

func (ctx *Context) Criticalf(status int, format string, v ...interface{})

Criticalf 输出日志到 CRITICAL 通道并向用户输出指定状态码的页面

若是输出日志的过程中出错,则 panic 若没有错误信息,则仅向客户端输出一条状态码信息。

输出的内容,会根据 status 的值,在 errorhandler 中查找相应的响应内容, 即使该值小于 400。

func (*Context) Error

func (ctx *Context) Error(status int, v ...interface{})

Error 输出日志到 ERROR 通道并向用户输出指定状态码的页面

若是输出日志的过程中出错,则 panic 若没有错误信息,则仅向客户端输出一条状态码信息。

输出的内容,会根据 status 的值,在 errorhandler 中查找相应的响应内容, 即使该值小于 400。

func (*Context) Errorf

func (ctx *Context) Errorf(status int, format string, v ...interface{})

Errorf 输出日志到 ERROR 通道并向用户输出指定状态码的页面

若是输出日志的过程中出错,则 panic 若没有错误信息,则仅向客户端输出一条状态码信息。

输出的内容,会根据 status 的值,在 errorhandler 中查找相应的响应内容, 即使该值小于 400。

func (*Context) Exit

func (ctx *Context) Exit(status int)

Exit 以指定的状态码退出当前协程

func (*Context) Fprint

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

Fprint 相当于 ctx.LocalePrinter.Fprint

func (*Context) Fprintf

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

Fprintf 相当于 ctx.LocalePrinter.Fprintf

func (*Context) Fprintln

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

Fprintln 相当于 ctx.LocalePrinter.Fprintln

func (*Context) Marshal

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

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

若 v 是一个 nil 值,则不会向客户端输出任何内容; 若是需要正常输出一个 nil 类型到客户端(比如JSON 中的 null), 可以使用 content.Nil 变量代替。

NOTE: 如果需要指定一个特定的 Content-Type 和 Content-Language, 可以在 headers 中指定,否则使用当前的编码和语言名称。

通过 Marshal 输出的内容,即使 status 的值大于 399, 依然能正常输出 v 的内容,而不是转向 errorhandler 中的相关内容。

func (*Context) NewResult

func (ctx *Context) NewResult(code int) *Result

NewResult 返回 Result 实例

如果找不到 code 对应的错误信息,则会直接 panic。

func (*Context) NewResultWithFields

func (ctx *Context) NewResultWithFields(code int, fields result.Fields) *Result

NewResultWithFields 返回 Result 实例

如果找不到 code 对应的错误信息,则会直接 panic。

func (*Context) NoContent

func (ctx *Context) NoContent()

NoContent 204

func (*Context) NotFound

func (ctx *Context) NotFound()

NotFound 404

接受统一的 errorhandler 模板支配

func (*Context) NotImplemented

func (ctx *Context) NotImplemented()

NotImplemented 501

接受统一的 errorhandler 模板支配

func (*Context) Now

func (ctx *Context) Now() time.Time

Now 返回当前时间

与 time.Now() 的区别在于 Now() 基于当前时区

func (*Context) ParamID

func (ctx *Context) ParamID(key string, code int) (int64, bool)

ParamID 获取地址参数中表示 key 的值并并转换成大于 0 的 int64

相对于 Context.ParamInt64(),该值必须大于 0。

NOTE: 若需要获取多个参数,使用 Context.Params 会更方便。

func (*Context) ParamInt64

func (ctx *Context) ParamInt64(key string, code int) (int64, bool)

ParamInt64 取地址参数中的 key 表示的值 int64 类型值

NOTE: 若需要获取多个参数,可以使用 Context.Params 获取会更方便。

func (*Context) ParamString

func (ctx *Context) ParamString(key string, code int) (string, bool)

ParamString 取地址参数中的 key 表示的 string 类型值

NOTE: 若需要获取多个参数,可以使用 Context.Params 获取会更方便。

func (*Context) Params

func (ctx *Context) Params() *Params

Params 声明一个新的 Params 实例

func (*Context) ParseTime

func (ctx *Context) ParseTime(layout, value string) (time.Time, error)

ParseTime 分析基于当前时区的时间

func (*Context) Print

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

Print 相当于 ctx.LocalePrinter.Print

func (*Context) Printf

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

Printf 相当于 ctx.LocalePrinter.Printf

func (*Context) Println

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

Println 相当于 ctx.LocalePrinter.Println

func (*Context) Queries

func (ctx *Context) Queries() (*Queries, error)

Queries 声明一个新的 Queries 实例

func (*Context) QueryObject

func (ctx *Context) QueryObject(v interface{}, code int) (ok bool)

QueryObject 将查询参数解析到一个对象中

功能有点类似于 Context.Read,只不过当前是从查询参数中读取数据到对象。

如果 URL 有问题,导致无法正确解析查询参数的数据,则会直接返回 422 的错误码给用户。

func (*Context) Read

func (ctx *Context) Read(v interface{}, code int) (ok bool)

Read 从客户端读取数据并转换成 v 对象

功能与 Unmarshal() 相同,只不过 Read() 在出错时, 会直接调用 Error() 处理:输出 422 的状态码, 并返回一个 false,告知用户转换失败。 如果是数据类型验证失败,则会输出以 code 作为错误代码的错误信息, 并返回 false,作为执行失败的通知。

func (*Context) Render

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

Render 将 v 渲染给客户端

功能与 Marshal() 相同,只不过 Render() 在出错时, 会直接调用 Error() 处理,输出 500 的状态码。

如果需要具体控制出错后的处理方式,可以使用 Marshal 函数。

通过 Render 输出的内容,即使 status 的值大于 399, 依然能正常输出 v 的内容,而不是转向 errorhandler 中的相关内容, 但是渲染出错时,依然转换 errorhandler。

func (*Context) ResetContent

func (ctx *Context) ResetContent()

ResetContent 205

func (*Context) ServeContent

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

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

文件可能提供连续的下载功能,其状态码是未定的, 所以提供了一个类似于 Render 的变体专门用于下载功能。

buf 实现 io.ReadSeeker 接口的内存块; name 下载时,显示的文件; headers 文件报头内容。

func (*Context) ServeFile

func (ctx *Context) ServeFile(p, index string, headers map[string]string)

ServeFile 提供文件下载

文件可能提供连续的下载功能,其状态码是未定的, 所以提供了一个类似于 Render 的变体专门用于下载功能。

p 指向本地文件的地址; index 如果 pat 是一个目录的话,则会访问 path 下的 index 指定的文件,默认为 index.html; headers 额外显示的报头内容。

func (*Context) ServeFileFS

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

ServeFileFS 提供文件下载服务

基于 fs.FS 接口获取 p 指向的文件,其它功能与 Context.ServeFile 相同。

func (*Context) Server

func (ctx *Context) Server() *Server

Server 获取关联的 context.Server 实例

func (*Context) Sprint

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

Sprint 相当于 ctx.LocalePrinter.Sprint

func (*Context) Sprintf

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

Sprintf 相当于 ctx.LocalePrinter.Sprintf

func (*Context) Sprintln

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

Sprintln 相当于 ctx.LocalePrinter.Sprintln

func (*Context) Unmarshal

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

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

func (*Context) Upload

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

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

返回的是文件列表

type Filter

type Filter func(HandlerFunc) HandlerFunc

Filter 针对 Context 的中间件

Filter 和 github.com/issue9/middleware.Middleware 本质上没有任何区别, 都是作用于 http.Handler 上的中间件,只因参数不同,且两者不能交替出现, 派生出两套类型。

保证针对 middleware.Middleware 的 AddMiddlewares 方法, 可以最大限度地利用现有的通用中间件,而 AddFilter 方便用户编写针对 Context 的中间件,且 Context 提供了 http.Handler 不存在的功能。

type HandlerFunc

type HandlerFunc func(*Context)

HandlerFunc 路由项处理函数原型

func FilterHandler

func FilterHandler(next HandlerFunc, filter ...Filter) HandlerFunc

FilterHandler 将过滤器应用于处理函数 next

type Module

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

Module 表示模块信息

模块可以作为代码的一种组织方式。将一组关联的功能合并为一个模块。

func NewModule

func NewModule(id, desc string, deps ...string) *Module

NewModule 声明一个新的模块

id 模块名称,需要全局唯一; desc 模块的详细信息; deps 表示当前模块的依赖模块名称,可以是插件中的模块名称。

func (*Module) AddAt

func (m *Module) AddAt(title string, f scheduled.JobFunc, t time.Time, delay bool)

AddAt 添加新的定时任务

f 表示服务的运行函数; title 是对该服务的简要说明; t 指定的时间点; delay 是否在任务执行完之后,才计算下一次的执行时间点。

func (*Module) AddCron

func (m *Module) AddCron(title string, f scheduled.JobFunc, spec string, delay bool)

AddCron 添加新的定时任务

f 表示服务的运行函数; title 是对该服务的简要说明; spec cron 表达式,支持秒; delay 是否在任务执行完之后,才计算下一次的执行时间点。

func (*Module) AddFilters

func (m *Module) AddFilters(filter ...Filter) *Module

AddFilters 添加过滤器

按给定参数的顺序反向依次调用。

func (*Module) AddInit

func (m *Module) AddInit(title string, f func() error)

AddInit 添加一个初始化函数

title 该初始化函数的名称。

func (*Module) AddJob

func (m *Module) AddJob(title string, f scheduled.JobFunc, scheduler schedulers.Scheduler, delay bool)

AddJob 添加新的计划任务

f 表示服务的运行函数; title 是对该服务的简要说明; scheduler 计划任务的时间调度算法实现; delay 是否在任务执行完之后,才计算下一次的执行时间点。

func (*Module) AddService

func (m *Module) AddService(title string, f service.Func)

AddService 添加新的服务

f 表示服务的运行函数; title 是对该服务的简要说明。

func (*Module) AddTicker

func (m *Module) AddTicker(title string, f scheduled.JobFunc, dur time.Duration, imm, delay bool)

AddTicker 添加新的定时任务

f 表示服务的运行函数; title 是对该服务的简要说明; imm 是否立即执行一次该任务; delay 是否在任务执行完之后,才计算下一次的执行时间点。

func (*Module) Delete

func (m *Module) Delete(path string, h HandlerFunc) *Module

Delete 添加 DELETE 请求处理项

func (*Module) Deps

func (m *Module) Deps() []string

Deps 模块的依赖项

func (*Module) Description

func (m *Module) Description() string

Description 模块的详细说明

func (*Module) Get

func (m *Module) Get(path string, h HandlerFunc) *Module

Get 添加 GET 请求处理项

func (*Module) Handle

func (m *Module) Handle(path string, h HandlerFunc, method ...string) *Module

Handle 添加路由项

func (*Module) ID

func (m *Module) ID() string

ID 唯一 ID

func (*Module) NewTag

func (m *Module) NewTag(tag string) Tag

NewTag 为当前模块生成特定名称的子模块

若已经存在,则直接返回该子模块。

Tag 是依赖关系与当前模块相同,但是功能完全独立的模块, 一般用于功能更新等操作。

func (*Module) Options

func (m *Module) Options(path, allow string) *Module

Options 指定 OPTIONS 请求的返回内容

func (*Module) Patch

func (m *Module) Patch(path string, h HandlerFunc) *Module

Patch 添加 Patch 请求处理

func (*Module) Post

func (m *Module) Post(path string, h HandlerFunc) *Module

Post 添加 POST 请求处理项

func (*Module) Prefix

func (m *Module) Prefix(prefix string, filter ...Filter) *Prefix

Prefix 返回特定前缀的路由设置对象

func (*Module) Put

func (m *Module) Put(path string, h HandlerFunc) *Module

Put 添加 PUT 请求处理项

func (*Module) Remove

func (m *Module) Remove(path string, method ...string) *Module

Remove 删除路由项

func (*Module) Resource

func (m *Module) Resource(pattern string, filter ...Filter) *Resource

Resource 生成 Resource 对象

func (*Module) Tags

func (m *Module) Tags() []string

Tags 与当前模块关联的子标签

type ModuleFunc

type ModuleFunc func(*Server) (*Module, error)

ModuleFunc 安装插件的函数签名

type ModuleInfo

type ModuleInfo struct {
	ID          string
	Description string
	Deps        []string
}

ModuleInfo 模块的描述信息

type Options

type Options struct {
	// 项目默认可存取的文件系统
	//
	// 默认情况下为可执行文件所在的目录。
	FS fs.FS

	// 服务器的时区
	//
	// 默认值为 time.Local
	Location *time.Location

	// 当前使用的本地化组件
	//
	// 默认情况下会引用 golang.org/x/text/message.DefaultCatalog 对象。
	//
	// golang.org/x/text/message/catalog 提供了 NewBuilder 和 NewFromMap
	// 等方式构建 Catalog 接口实例。
	Catalog catalog.Catalog

	// 指定生成 Result 数据的方法
	//
	// 默认情况下指向  result.DefaultBuilder。
	ResultBuilder result.BuildFunc

	// 缓存系统
	//
	// 默认值为内存类型。
	Cache cache.Cache

	// 初始化 MUX 的参数
	DisableHead    bool
	DisableOptions bool
	SkipCleanPath  bool

	// 可以对 http.Server 的内容进行个性
	//
	// NOTE: 对 http.Server.Handler 的修改不会启作用,该值始终会指向 Server.middlewares
	HTTPServer func(*http.Server)

	// 网站的根目录
	//
	// 可以带上域名:https://example.com/api;或是仅路径部分 /api;
	// 两者的区别在于 Router.URL 返回的内容,前者带域名部分,后者不带。
	Root string
	// contains filtered or unexported fields
}

Options 初始化 Server 的参数

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

func (p *Params) Bool(key string) bool

Bool 获取参数 key 所代表的值并转换成 bool

最终会调用 strconv.ParseBool 进行转换, 也只有该方法中允许的字符串会被正确转换。

func (*Params) Errors

func (p *Params) Errors() result.Fields

Errors 返回所有的错误信息

func (*Params) Float64

func (p *Params) Float64(key string) float64

Float64 获取参数 key 所代表的值并转换成 float64

func (*Params) HasErrors

func (p *Params) HasErrors() bool

HasErrors 是否有错误内容存在

func (*Params) ID

func (p *Params) ID(key string) int64

ID 获取参数 key 所代表的值并转换成 int64

值必须大于 0,否则会输出错误信息,并返回零值。

func (*Params) Int64

func (p *Params) Int64(key string) int64

Int64 获取参数 key 所代表的值,并转换成 int64

func (*Params) MustBool

func (p *Params) MustBool(key string, def bool) bool

MustBool 获取参数 key 所代表的值并转换成 bool

若不存在或是转换出错,则返回 def 作为其默认值。 仅在类型转换出错时,才会向 errors 写入错误信息。

最终会调用 strconv.ParseBool 进行转换, 也只有该方法中允许的字符串会被正确转换。

func (*Params) MustFloat64

func (p *Params) MustFloat64(key string, def float64) float64

MustFloat64 获取参数 key 所代表的值并转换成 float64

若不存在或是转换出错,则返回 def 作为其默认值。 仅在类型转换出错时,才会向 errors 写入错误信息。

func (*Params) MustID

func (p *Params) MustID(key string, def int64) int64

MustID 获取参数 key 所代表的值并转换成 int64

值必须大于 0,否则会输出错误信息,并返回零值。

若不存在或是转换出错,则返回 def 作为其默认值。 仅在类型转换出错或是小于零时,才会向 errors 写入错误信息。

func (*Params) MustInt64

func (p *Params) MustInt64(key string, def int64) int64

MustInt64 获取参数 key 所代表的值并转换成 int64

若不存在或是转换出错,则返回 def 作为其默认值。 仅在类型转换出错时,才会向 errors 写入错误信息。

func (*Params) MustString

func (p *Params) MustString(key, def string) string

MustString 获取参数 key 所代表的值并转换成 string

若不存在或是转换出错,则返回 def 作为其默认值。

func (*Params) Result

func (p *Params) Result(code int) *Result

Result 转换成 Result 对象

code 是作为 Result.Code 从错误消息中查找,如果不存在,则 panic。 Params.errors 将会作为 Result.Fields 的内容。

func (*Params) String

func (p *Params) String(key string) string

String 获取参数 key 所代表的值并转换成 string

type Prefix

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

Prefix 带有统一前缀的路由管理

func (*Prefix) Delete

func (p *Prefix) Delete(path string, h HandlerFunc) *Prefix

Delete 添加 DELETE 请求处理项

func (*Prefix) Get

func (p *Prefix) Get(path string, h HandlerFunc) *Prefix

Get 添加 GET 请求处理项

func (*Prefix) Handle

func (p *Prefix) Handle(path string, h HandlerFunc, method ...string) error

Handle 添加路由项

func (*Prefix) Options

func (p *Prefix) Options(path, allow string) *Prefix

Options 指定 OPTIONS 请求的返回内容

func (*Prefix) Patch

func (p *Prefix) Patch(path string, h HandlerFunc) *Prefix

Patch 添加 Patch 请求处理

func (*Prefix) Post

func (p *Prefix) Post(path string, h HandlerFunc) *Prefix

Post 添加 POST 请求处理项

func (*Prefix) Put

func (p *Prefix) Put(path string, h HandlerFunc) *Prefix

Put 添加 PUT 请求处理项

func (*Prefix) Remove

func (p *Prefix) Remove(path string, method ...string)

Remove 删除路由项

func (*Prefix) Resource

func (p *Prefix) Resource(pattern string, filter ...Filter) *Resource

Resource 生成 Resource 对象

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
}

func (*Queries) Bool

func (q *Queries) Bool(key string, def bool) bool

Bool 从查询参数中获取指定名称的值

若不存在则返回 def 作为其默认值。

func (*Queries) Errors

func (q *Queries) Errors() result.Fields

Errors 所有的错误信息

func (*Queries) Float64

func (q *Queries) Float64(key string, def float64) float64

Float64 从查询参数中获取指定名称的值

若不存在则返回 def 作为其默认值。

func (*Queries) HasErrors

func (q *Queries) HasErrors() bool

HasErrors 是否存在错误内容

func (*Queries) Int

func (q *Queries) Int(key string, def int) int

Int 从查询参数中获取指定名称的值

若不存在则返回 def 作为其默认值。 若是无法转换,则会保存错误信息

func (*Queries) Int64

func (q *Queries) Int64(key string, def int64) int64

Int64 从查询参数中获取指定名称的值

若不存在则返回 def 作为其默认值。

func (*Queries) Object

func (q *Queries) Object(v interface{})

Object 将查询参数解析到一个对象中

具体的文档信息可以参考 https://github.com/issue9/query

func (*Queries) Result

func (q *Queries) Result(code int) *Result

Result 转换成 Result 对象

code 是作为 Result.Code 从错误消息中查找,如果不存在,则 panic。 Queries.errors 将会作为 Result.Fields 的内容。

func (*Queries) String

func (q *Queries) String(key, def string) string

String 从查询参数中获取指定名称的值

若不存在则返回 def 作为其默认值。

type Resource

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

Resource 同一资源的不同请求方法的管理

func (*Resource) Delete

func (r *Resource) Delete(h HandlerFunc) *Resource

Delete 添加 DELETE 请求处理项

func (*Resource) Get

func (r *Resource) Get(h HandlerFunc) *Resource

Get 添加 GET 请求处理项

func (*Resource) Handle

func (r *Resource) Handle(h HandlerFunc, method ...string) error

Handle 添加路由项

func (*Resource) Options

func (r *Resource) Options(allow string) *Resource

Options 指定 OPTIONS 请求的返回内容

func (*Resource) Patch

func (r *Resource) Patch(h HandlerFunc) *Resource

Patch 添加 PATCH 请求处理项

func (*Resource) Post

func (r *Resource) Post(h HandlerFunc) *Resource

Post 添加 POST 请求处理项

func (*Resource) Put

func (r *Resource) Put(h HandlerFunc) *Resource

Put 添加 PUT 请求处理项

func (*Resource) Remove

func (r *Resource) Remove(method ...string)

Remove 删除路由项

type Result

type Result struct {
	result.Result
	// contains filtered or unexported fields
}

Result result.Result 与 Context 相结合的实现

func (*Result) Render

func (rslt *Result) Render()

Render 渲染内容

func (*Result) RenderAndExit

func (rslt *Result) RenderAndExit()

RenderAndExit 渲染内容并退出当前的请求处理

type Router

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

Router 路由管理

func (*Router) Delete

func (router *Router) Delete(path string, h HandlerFunc) *Router

Delete 添加 DELETE 请求处理项

func (*Router) Get

func (router *Router) Get(path string, h HandlerFunc) *Router

Get 添加 GET 请求处理项

func (*Router) Handle

func (router *Router) Handle(path string, h HandlerFunc, method ...string) error

Handle 添加路由请求项

func (*Router) Mux

func (router *Router) Mux() *mux.Mux

Mux 返回 mux.Mux 实例

func (*Router) NewRouter

func (router *Router) NewRouter(name string, url *url.URL, matcher mux.Matcher, filter ...Filter) (*Router, bool)

NewRouter 构建基于 matcher 匹配的路由操作实例

不会应用通过 Server.AddFilters 添加的中间件,但是会应用 Server.AddMiddlewares 添加的中间件。

func (*Router) Options

func (router *Router) Options(path, allow string) *Router

Options 添加 OPTIONS 请求处理项

忽略 Filter 类型的是间件,如果有需要,可以采用 Handle 处理 Options 请求。

func (*Router) Patch

func (router *Router) Patch(path string, h HandlerFunc) *Router

Patch 添加 PATCH 请求处理项

func (*Router) Path

func (router *Router) Path(pattern string, params map[string]string) (string, error)

Path 返回相对于域名的绝对路由地址

功能与 mux.URL 相似,但是加上了关联的域名地址的根路径。比如根地址是 https://example.com/blog pattern 为 /posts/{id},则返回为 /blog/posts/1。 如果 params 为空的话,则会直接将 pattern 作为从 mux 转换之后的内容与 router.root 合并返回。

func (*Router) Post

func (router *Router) Post(path string, h HandlerFunc) *Router

Post 添加 POST 请求处理项

func (*Router) Prefix

func (router *Router) Prefix(prefix string, filter ...Filter) *Prefix

Prefix 返回特定前缀的路由设置对象

func (*Router) Put

func (router *Router) Put(path string, h HandlerFunc) *Router

Put 添加 PUT 请求处理项

func (*Router) Remove

func (router *Router) Remove(path string, method ...string)

Remove 删除指定的路由项

func (*Router) Resource

func (router *Router) Resource(pattern string, filter ...Filter) *Resource

Resource 生成资源项

func (*Router) Static

func (router *Router) Static(p, dir, index string) error

Static 添加静态路由

p 为路由地址,必须以命名参数结尾,比如 /assets/{path},之后可以通过此值删除路由项; index 可以在访问一个目录时指定默认访问的页面。 dir 为指向静态文件的路径;

如果要删除该静态路由,则可以将 path 传递给 Remove 进行删除。

比如在 Root 的值为 example.com/blog 时, 将参数指定为 /admin/{path} 和 ~/data/assets/admin 表示将 example.com/blog/admin/* 解析到 ~/data/assets/admin 目录之下。

func (*Router) StaticFS

func (router *Router) StaticFS(p string, f fs.FS, index string) error

func (*Router) URL

func (router *Router) URL(pattern string, params map[string]string) (string, error)

URL 构建完整的 URL

功能与 mux.URL 相似,但是加上了关联的域名地址。比如根地址是 https://example.com/blog pattern 为 /posts/{id},则返回为 https://example.com/blog/posts/1。 如果 params 为空的话,则会直接将 pattern 作为从 mux 转换之后的内容与 router.root 合并返回。

type Server

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

Server 提供了用于构建 Context 对象的基本数据

func GetServer

func GetServer(r *http.Request) *Server

GetServer 从请求中获取 *Server 实例

r 必须得是由 Server 生成的,否则会 panic。

func New

func New(name, version string, logs *logs.Logs, o *Options) (*Server, error)

New 返回 *Server 实例

name, version 表示服务的名称和版本号; 在初始化 Server 必须指定的参数,且有默认值的,由 Options 定义。

func (*Server) AddCompressTypes

func (srv *Server) AddCompressTypes(types ...string)

AddCompressTypes 指定哪些内容可以进行压缩传输

默认情况下是所有内容都将进行压缩传输, * 表示所有; text/* 表示以 text/ 开头的类型; text/plain 表示具体类型 text/plain;

func (*Server) AddFilters

func (srv *Server) AddFilters(filter ...Filter)

AddFilters 添加过滤器

func (*Server) AddMiddlewares

func (srv *Server) AddMiddlewares(middleware ...middleware.Middleware)

AddMiddlewares 添加中间件

func (*Server) AddModule

func (srv *Server) AddModule(module ...*Module) error

AddModule 添加模块

可以在运行过程中添加模块,该模块会在加载时直接初始化,前提是模块的依赖模块都已经初始化。

func (*Server) AddModuleFunc

func (srv *Server) AddModuleFunc(module ...ModuleFunc) error

AddModuleFunc 从 ModuleFunc 添加模块

func (*Server) AddResultMessage

func (srv *Server) AddResultMessage(status, code int, key message.Reference, v ...interface{})

AddResultMessage 添加一条错误信息

status 指定了该错误代码反馈给客户端的 HTTP 状态码; code 错误代码; key 和 v 表示错误信息的翻译项内容,最终会传递给 message.Printer.Sprintf 进行翻译;

func (*Server) Cache

func (srv *Server) Cache() cache.Cache

Cache 返回缓存的相关接口

func (*Server) Close

func (srv *Server) Close(shutdownTimeout time.Duration) error

Close 关闭服务

func (*Server) DeleteCompressTypes

func (srv *Server) DeleteCompressTypes(types ...string)

DeleteCompressTypes 删除指定内容的压缩支持

仅用于删除通过 AddType 添加的内容。

NOTE: 如果指定 * 之后的所有内容都将不支持。

func (*Server) Get

func (srv *Server) Get(key interface{}) interface{}

Get 返回指定键名的值

func (*Server) InitTag

func (srv *Server) InitTag(tag string) error

InitTag 初始化模块下的子标签

func (*Server) LoadPlugin

func (srv *Server) LoadPlugin(path string) error

LoadPlugin 将指定的插件当作模块进行加载

path 为插件的路径;

插件必须是以 buildmode=plugin 的方式编译的,且要求其引用的 github.com/issue9/web 版本与当前的相同。 LoadPlugin 会在插件中查找固定名称和类型的函数名(参考 ModuleFunc 和 ModuleFuncName), 如果存在,会调用该方法将插件加载到 Server 对象中,否则返回相应的错误信息。

func (*Server) LoadPlugins

func (srv *Server) LoadPlugins(glob string) error

LoadPlugins 加载所有的插件

如果 glob 为空,则不会加载任何内容,返回空值

func (*Server) Location

func (srv *Server) Location() *time.Location

Location 指定服务器的时区信息

func (*Server) Logs

func (srv *Server) Logs() *logs.Logs

Logs 返回关联的 logs.Logs 实例

func (*Server) Mimetypes

func (srv *Server) Mimetypes() *content.Mimetypes

Mimetypes 返回内容编解码的管理接口

func (*Server) Modules

func (srv *Server) Modules() []*ModuleInfo

Modules 当前系统使用的所有模块信息

func (*Server) Name

func (srv *Server) Name() string

Name 应用的名称

func (*Server) NewContext

func (srv *Server) NewContext(w http.ResponseWriter, r *http.Request) *Context

NewContext 构建 *Context 实例

如果 Accept 的内容与当前配置无法匹配, 则退出(panic)并输出 NotAcceptable 状态码。

func (*Server) NewLocalePrinter

func (srv *Server) NewLocalePrinter(tag language.Tag) *message.Printer

NewLocalePrinter 返回指定语言的 message.Printer

func (*Server) Now

func (srv *Server) Now() time.Time

Now 返回当前时间

与 time.Now() 的区别在于 Now() 基于当前时区

func (*Server) Open

func (srv *Server) Open(name string) (fs.File, error)

Open 实现 fs.FS 接口

func (*Server) ParseTime

func (srv *Server) ParseTime(layout, value string) (time.Time, error)

ParseTime 分析基于当前时区的时间

func (*Server) ResultMessages

func (srv *Server) ResultMessages(p *message.Printer) map[int]string

ResultMessages 错误信息列表

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

func (*Server) Router

func (srv *Server) Router() *Router

Router 返回操作路由项的实例

路由地址基于 root 的值, 且所有的路由都会自动应用通过 Server.AddFilters 和 Server.AddMiddlewares 添加的中间件。

func (*Server) Serve

func (srv *Server) Serve() (err error)

Serve 启动服务

会自动对模块进行初始化。

func (*Server) Services

func (srv *Server) Services() *service.Manager

Services 返回服务内容的管理接口

func (*Server) Set

func (srv *Server) Set(key, val interface{})

Set 保存指定键名的值

func (*Server) SetCompressAlgorithm

func (srv *Server) SetCompressAlgorithm(name string, w compress.WriterFunc) error

SetCompressAlgorithm 设置压缩的算法

默认情况下,支持 gzip、deflate 和 br 三种。 如果 w 为 nil,表示删除对该算法的支持。

func (*Server) SetDebugger

func (srv *Server) SetDebugger(pprof, vars string) (err error)

SetDebugger 设置调试地址

func (*Server) SetErrorHandle

func (srv *Server) SetErrorHandle(h errorhandler.HandleFunc, status ...int)

SetErrorHandle 设置指定状态码页面的处理函数

如果状态码已经存在处理函数,则修改,否则就添加。

func (*Server) SetRecovery

func (srv *Server) SetRecovery(f recovery.RecoverFunc, status int)

SetRecovery 设置在 panic 时的处理函数

f 为处理 panic 的函数; status 用于指定调用 ErrorHandler 中的哪个理函数展示给终端用户。 仅在 status > 0 时才会有效果。f 中也可以向终端输出状态码和页面内容, 如果在 f 也输出了相关的状态,同时又指定了 status,则会依次调用 status 和 f。

func (*Server) Tags

func (srv *Server) Tags() map[string][]string

Tags 返回所有的子模块名称

键名为模块名称,键值为该模块下的标签列表。

func (*Server) Uptime

func (srv *Server) Uptime() time.Time

Uptime 当前服务的运行时间

func (*Server) Version

func (srv *Server) Version() string

Version 应用的版本

type Tag

type Tag interface {
	AddInit(string, func() error)
}

Tag 表示与特定标签相关联的初始化函数列表

依附于模块,共享模块的依赖关系。一般是各个模块下的安装脚本使用。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳