Documentation
¶
Index ¶
- Constants
- Variables
- func ExitContext(status int)
- type CTXSanitizer
- type Context
- func (ctx *Context) Body() (body []byte, err error)
- func (ctx *Context) ClientIP() string
- func (ctx *Context) Created(v interface{}, location 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) NewResultWithFields(code int, fields result.Fields) *Result
- func (ctx *Context) NoContent()
- func (ctx *Context) NotFound()
- func (ctx *Context) NotImplemented()
- func (ctx *Context) Now() time.Time
- func (ctx *Context) ParamID(key string, code int) (int64, bool)
- func (ctx *Context) ParamInt64(key string, code int) (int64, bool)
- func (ctx *Context) ParamString(key string, code int) (string, bool)
- func (ctx *Context) Params() *Params
- func (ctx *Context) ParseTime(layout, value string) (time.Time, error)
- 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, error)
- func (ctx *Context) QueryObject(v interface{}, code int) (ok bool)
- func (ctx *Context) Read(v interface{}, code int) (ok bool)
- func (ctx *Context) Render(status int, v interface{}, headers map[string]string)
- func (ctx *Context) ResetContent()
- func (ctx *Context) ServeContent(buf io.ReadSeeker, name string, mod time.Time, headers map[string]string)
- func (ctx *Context) ServeFile(p, index string, headers map[string]string)
- func (ctx *Context) ServeFileFS(f fs.FS, p, index string, headers map[string]string)
- func (ctx *Context) Server() *Server
- 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 Filter
- type HandlerFunc
- type Module
- func (m *Module) AddAt(title string, f scheduled.JobFunc, t time.Time, delay bool)
- func (m *Module) AddCron(title string, f scheduled.JobFunc, spec string, delay bool)
- func (m *Module) AddFilters(filter ...Filter) *Module
- func (m *Module) AddInit(title string, f func() error)
- func (m *Module) AddJob(title string, f scheduled.JobFunc, scheduler schedulers.Scheduler, delay bool)
- func (m *Module) AddService(title string, f service.Func)
- func (m *Module) AddTicker(title string, f scheduled.JobFunc, dur time.Duration, imm, delay bool)
- func (m *Module) Delete(path string, h HandlerFunc) *Module
- func (m *Module) Deps() []string
- func (m *Module) Description() string
- func (m *Module) Get(path string, h HandlerFunc) *Module
- func (m *Module) Handle(path string, h HandlerFunc, method ...string) *Module
- func (m *Module) ID() string
- func (m *Module) NewTag(tag string) Tag
- func (m *Module) Options(path, allow string) *Module
- func (m *Module) Patch(path string, h HandlerFunc) *Module
- func (m *Module) Post(path string, h HandlerFunc) *Module
- func (m *Module) Prefix(prefix string, filter ...Filter) *Prefix
- func (m *Module) Put(path string, h HandlerFunc) *Module
- func (m *Module) Remove(path string, method ...string) *Module
- func (m *Module) Resource(pattern string, filter ...Filter) *Resource
- func (m *Module) Tags() []string
- type ModuleFunc
- type ModuleInfo
- type Options
- type Params
- func (p *Params) Bool(key string) bool
- func (p *Params) Errors() result.Fields
- func (p *Params) Float64(key string) float64
- func (p *Params) HasErrors() bool
- func (p *Params) ID(key string) int64
- 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) 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 Prefix
- func (p *Prefix) Delete(path string, h HandlerFunc) *Prefix
- func (p *Prefix) Get(path string, h HandlerFunc) *Prefix
- func (p *Prefix) Handle(path string, h HandlerFunc, method ...string) error
- func (p *Prefix) Options(path, allow string) *Prefix
- func (p *Prefix) Patch(path string, h HandlerFunc) *Prefix
- func (p *Prefix) Post(path string, h HandlerFunc) *Prefix
- func (p *Prefix) Put(path string, h HandlerFunc) *Prefix
- func (p *Prefix) Remove(path string, method ...string)
- func (p *Prefix) Resource(pattern string, filter ...Filter) *Resource
- type Queries
- func (q *Queries) Bool(key string, def bool) bool
- func (q *Queries) Errors() result.Fields
- 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) Object(v interface{})
- func (q *Queries) Result(code int) *Result
- func (q *Queries) String(key, def string) string
- type Resource
- func (r *Resource) Delete(h HandlerFunc) *Resource
- func (r *Resource) Get(h HandlerFunc) *Resource
- func (r *Resource) Handle(h HandlerFunc, method ...string) error
- func (r *Resource) Options(allow string) *Resource
- func (r *Resource) Patch(h HandlerFunc) *Resource
- func (r *Resource) Post(h HandlerFunc) *Resource
- func (r *Resource) Put(h HandlerFunc) *Resource
- func (r *Resource) Remove(method ...string)
- type Result
- type Router
- func (router *Router) Delete(path string, h HandlerFunc) *Router
- func (router *Router) Get(path string, h HandlerFunc) *Router
- func (router *Router) Handle(path string, h HandlerFunc, method ...string) error
- func (router *Router) Mux() *mux.Mux
- func (router *Router) NewRouter(name string, url *url.URL, matcher mux.Matcher, filter ...Filter) (*Router, bool)
- func (router *Router) Options(path, allow string) *Router
- func (router *Router) Patch(path string, h HandlerFunc) *Router
- func (router *Router) Path(pattern string, params map[string]string) (string, error)
- func (router *Router) Post(path string, h HandlerFunc) *Router
- func (router *Router) Prefix(prefix string, filter ...Filter) *Prefix
- func (router *Router) Put(path string, h HandlerFunc) *Router
- func (router *Router) Remove(path string, method ...string)
- func (router *Router) Resource(pattern string, filter ...Filter) *Resource
- func (router *Router) Static(p, dir, index string) error
- func (router *Router) StaticFS(p string, f fs.FS, index string) error
- func (router *Router) URL(pattern string, params map[string]string) (string, error)
- type Server
- func (srv *Server) AddCompressTypes(types ...string)
- func (srv *Server) AddFilters(filter ...Filter)
- func (srv *Server) AddMiddlewares(middleware ...middleware.Middleware)
- func (srv *Server) AddModule(module ...*Module) error
- func (srv *Server) AddModuleFunc(module ...ModuleFunc) error
- func (srv *Server) AddResultMessage(status, code int, key message.Reference, v ...interface{})
- func (srv *Server) Cache() cache.Cache
- func (srv *Server) Close(shutdownTimeout time.Duration) error
- func (srv *Server) DeleteCompressTypes(types ...string)
- func (srv *Server) Get(key interface{}) interface{}
- func (srv *Server) InitTag(tag string) error
- func (srv *Server) LoadPlugin(path string) error
- func (srv *Server) LoadPlugins(glob string) error
- func (srv *Server) Location() *time.Location
- func (srv *Server) Logs() *logs.Logs
- func (srv *Server) Mimetypes() *content.Mimetypes
- func (srv *Server) Modules() []*ModuleInfo
- func (srv *Server) Name() string
- func (srv *Server) NewContext(w http.ResponseWriter, r *http.Request) *Context
- func (srv *Server) NewLocalePrinter(tag language.Tag) *message.Printer
- func (srv *Server) Now() time.Time
- func (srv *Server) Open(name string) (fs.File, error)
- func (srv *Server) ParseTime(layout, value string) (time.Time, error)
- func (srv *Server) ResultMessages(p *message.Printer) map[int]string
- func (srv *Server) Router() *Router
- func (srv *Server) Serve() (err error)
- func (srv *Server) Services() *service.Manager
- func (srv *Server) Set(key, val interface{})
- func (srv *Server) SetCompressAlgorithm(name string, w compress.WriterFunc) error
- func (srv *Server) SetDebugger(pprof, vars string) (err error)
- func (srv *Server) SetErrorHandle(h errorhandler.HandleFunc, status ...int)
- func (srv *Server) SetRecovery(f recovery.RecoverFunc, status int)
- func (srv *Server) Tags() map[string][]string
- func (srv *Server) Uptime() time.Time
- func (srv *Server) Version() string
- type Tag
Constants ¶
const ModuleFuncName = "Module"
ModuleFuncName 插件中的用于获取模块信息的函数名
NOTE: 必须为可导出的函数名称
Variables ¶
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 ¶
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) ClientIP ¶
ClientIP 返回客户端的 IP 地址
NOTE: 包含了端口部分。
获取顺序如下:
- X-Forwarded-For 的第一个元素
- Remote-Addr 报头
- X-Read-IP 报头
func (*Context) Critical ¶
Critical 输出日志到 CRITICAL 通道并向用户输出指定状态码的页面
若是输出日志的过程中出错,则 panic 若没有错误信息,则仅向客户端输出一条状态码信息。
输出的内容,会根据 status 的值,在 errorhandler 中查找相应的响应内容, 即使该值小于 400。
func (*Context) Criticalf ¶
Criticalf 输出日志到 CRITICAL 通道并向用户输出指定状态码的页面
若是输出日志的过程中出错,则 panic 若没有错误信息,则仅向客户端输出一条状态码信息。
输出的内容,会根据 status 的值,在 errorhandler 中查找相应的响应内容, 即使该值小于 400。
func (*Context) Error ¶
Error 输出日志到 ERROR 通道并向用户输出指定状态码的页面
若是输出日志的过程中出错,则 panic 若没有错误信息,则仅向客户端输出一条状态码信息。
输出的内容,会根据 status 的值,在 errorhandler 中查找相应的响应内容, 即使该值小于 400。
func (*Context) Errorf ¶
Errorf 输出日志到 ERROR 通道并向用户输出指定状态码的页面
若是输出日志的过程中出错,则 panic 若没有错误信息,则仅向客户端输出一条状态码信息。
输出的内容,会根据 status 的值,在 errorhandler 中查找相应的响应内容, 即使该值小于 400。
func (*Context) Marshal ¶
Marshal 将 v 解码并发送给客户端
若 v 是一个 nil 值,则不会向客户端输出任何内容; 若是需要正常输出一个 nil 类型到客户端(比如JSON 中的 null), 可以使用 content.Nil 变量代替。
NOTE: 如果需要指定一个特定的 Content-Type 和 Content-Language, 可以在 headers 中指定,否则使用当前的编码和语言名称。
通过 Marshal 输出的内容,即使 status 的值大于 399, 依然能正常输出 v 的内容,而不是转向 errorhandler 中的相关内容。
func (*Context) NewResultWithFields ¶
NewResultWithFields 返回 Result 实例
如果找不到 code 对应的错误信息,则会直接 panic。
func (*Context) NotImplemented ¶
func (ctx *Context) NotImplemented()
NotImplemented 501
接受统一的 errorhandler 模板支配
func (*Context) ParamID ¶
ParamID 获取地址参数中表示 key 的值并并转换成大于 0 的 int64
相对于 Context.ParamInt64(),该值必须大于 0。
NOTE: 若需要获取多个参数,使用 Context.Params 会更方便。
func (*Context) ParamInt64 ¶
ParamInt64 取地址参数中的 key 表示的值 int64 类型值
NOTE: 若需要获取多个参数,可以使用 Context.Params 获取会更方便。
func (*Context) ParamString ¶
ParamString 取地址参数中的 key 表示的 string 类型值
NOTE: 若需要获取多个参数,可以使用 Context.Params 获取会更方便。
func (*Context) QueryObject ¶
QueryObject 将查询参数解析到一个对象中
功能有点类似于 Context.Read,只不过当前是从查询参数中读取数据到对象。
如果 URL 有问题,导致无法正确解析查询参数的数据,则会直接返回 422 的错误码给用户。
func (*Context) Read ¶
Read 从客户端读取数据并转换成 v 对象
功能与 Unmarshal() 相同,只不过 Read() 在出错时, 会直接调用 Error() 处理:输出 422 的状态码, 并返回一个 false,告知用户转换失败。 如果是数据类型验证失败,则会输出以 code 作为错误代码的错误信息, 并返回 false,作为执行失败的通知。
func (*Context) Render ¶
Render 将 v 渲染给客户端
功能与 Marshal() 相同,只不过 Render() 在出错时, 会直接调用 Error() 处理,输出 500 的状态码。
如果需要具体控制出错后的处理方式,可以使用 Marshal 函数。
通过 Render 输出的内容,即使 status 的值大于 399, 依然能正常输出 v 的内容,而不是转向 errorhandler 中的相关内容, 但是渲染出错时,依然转换 errorhandler。
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 ¶
ServeFile 提供文件下载
文件可能提供连续的下载功能,其状态码是未定的, 所以提供了一个类似于 Render 的变体专门用于下载功能。
p 指向本地文件的地址; index 如果 pat 是一个目录的话,则会访问 path 下的 index 指定的文件,默认为 index.html; headers 额外显示的报头内容。
func (*Context) ServeFileFS ¶
ServeFileFS 提供文件下载服务
基于 fs.FS 接口获取 p 指向的文件,其它功能与 Context.ServeFile 相同。
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 (*Module) AddAt ¶
AddAt 添加新的定时任务
f 表示服务的运行函数; title 是对该服务的简要说明; t 指定的时间点; delay 是否在任务执行完之后,才计算下一次的执行时间点。
func (*Module) AddCron ¶
AddCron 添加新的定时任务
f 表示服务的运行函数; title 是对该服务的简要说明; spec cron 表达式,支持秒; delay 是否在任务执行完之后,才计算下一次的执行时间点。
func (*Module) AddJob ¶
func (m *Module) AddJob(title string, f scheduled.JobFunc, scheduler schedulers.Scheduler, delay bool)
AddJob 添加新的计划任务
f 表示服务的运行函数; title 是对该服务的简要说明; scheduler 计划任务的时间调度算法实现; delay 是否在任务执行完之后,才计算下一次的执行时间点。
func (*Module) AddTicker ¶
AddTicker 添加新的定时任务
f 表示服务的运行函数; title 是对该服务的简要说明; imm 是否立即执行一次该任务; delay 是否在任务执行完之后,才计算下一次的执行时间点。
func (*Module) Delete ¶
func (m *Module) Delete(path string, h HandlerFunc) *Module
Delete 添加 DELETE 请求处理项
func (*Module) Handle ¶
func (m *Module) Handle(path string, h HandlerFunc, method ...string) *Module
Handle 添加路由项
func (*Module) NewTag ¶
NewTag 为当前模块生成特定名称的子模块
若已经存在,则直接返回该子模块。
Tag 是依赖关系与当前模块相同,但是功能完全独立的模块, 一般用于功能更新等操作。
func (*Module) Patch ¶
func (m *Module) Patch(path string, h HandlerFunc) *Module
Patch 添加 Patch 请求处理
type ModuleInfo ¶
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 ¶
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) MustInt64 ¶
MustInt64 获取参数 key 所代表的值并转换成 int64
若不存在或是转换出错,则返回 def 作为其默认值。 仅在类型转换出错时,才会向 errors 写入错误信息。
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) Handle ¶
func (p *Prefix) Handle(path string, h HandlerFunc, method ...string) error
Handle 添加路由项
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) Object ¶
func (q *Queries) Object(v interface{})
Object 将查询参数解析到一个对象中
具体的文档信息可以参考 https://github.com/issue9/query
type Resource ¶
type Resource struct {
// contains filtered or unexported fields
}
Resource 同一资源的不同请求方法的管理
func (*Resource) Handle ¶
func (r *Resource) Handle(h HandlerFunc, method ...string) error
Handle 添加路由项
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) Handle ¶
func (router *Router) Handle(path string, h HandlerFunc, method ...string) error
Handle 添加路由请求项
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) Patch ¶
func (router *Router) Patch(path string, h HandlerFunc) *Router
Patch 添加 PATCH 请求处理项
func (*Router) Path ¶
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) Static ¶
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) URL ¶
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 (*Server) AddCompressTypes ¶
AddCompressTypes 指定哪些内容可以进行压缩传输
默认情况下是所有内容都将进行压缩传输, * 表示所有; text/* 表示以 text/ 开头的类型; text/plain 表示具体类型 text/plain;
func (*Server) AddMiddlewares ¶
func (srv *Server) AddMiddlewares(middleware ...middleware.Middleware)
AddMiddlewares 添加中间件
func (*Server) AddModuleFunc ¶
func (srv *Server) AddModuleFunc(module ...ModuleFunc) error
AddModuleFunc 从 ModuleFunc 添加模块
func (*Server) AddResultMessage ¶
AddResultMessage 添加一条错误信息
status 指定了该错误代码反馈给客户端的 HTTP 状态码; code 错误代码; key 和 v 表示错误信息的翻译项内容,最终会传递给 message.Printer.Sprintf 进行翻译;
func (*Server) DeleteCompressTypes ¶
DeleteCompressTypes 删除指定内容的压缩支持
仅用于删除通过 AddType 添加的内容。
NOTE: 如果指定 * 之后的所有内容都将不支持。
func (*Server) LoadPlugin ¶
LoadPlugin 将指定的插件当作模块进行加载
path 为插件的路径;
插件必须是以 buildmode=plugin 的方式编译的,且要求其引用的 github.com/issue9/web 版本与当前的相同。 LoadPlugin 会在插件中查找固定名称和类型的函数名(参考 ModuleFunc 和 ModuleFuncName), 如果存在,会调用该方法将插件加载到 Server 对象中,否则返回相应的错误信息。
func (*Server) NewContext ¶
NewContext 构建 *Context 实例
如果 Accept 的内容与当前配置无法匹配, 则退出(panic)并输出 NotAcceptable 状态码。
func (*Server) NewLocalePrinter ¶
NewLocalePrinter 返回指定语言的 message.Printer
func (*Server) Router ¶
Router 返回操作路由项的实例
路由地址基于 root 的值, 且所有的路由都会自动应用通过 Server.AddFilters 和 Server.AddMiddlewares 添加的中间件。
func (*Server) SetCompressAlgorithm ¶
func (srv *Server) SetCompressAlgorithm(name string, w compress.WriterFunc) error
SetCompressAlgorithm 设置压缩的算法
默认情况下,支持 gzip、deflate 和 br 三种。 如果 w 为 nil,表示删除对该算法的支持。
func (*Server) SetDebugger ¶
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。