Documentation
¶
Index ¶
- Constants
- Variables
- func CheckResponse(_ context.Context, res *http.Response) error
- func Do(client *http.Client, req *http.Request, target interface{}) error
- func NewClient(ctx context.Context, opts ...ClientOption) (*http.Client, error)
- func NewClientContext(ctx context.Context, info ClientInfo) context.Context
- func NewServerContext(ctx context.Context, info ServerInfo) context.Context
- func NewTransport(ctx context.Context, opts ...ClientOption) (http.RoundTripper, error)
- func PathValue(req *http.Request, key string) string
- func Process(h HandleOptions, w http.ResponseWriter, r *http.Request, ...)
- func Vars(req *http.Request) map[string]string
- type ClientInfo
- type ClientOption
- type DecodeErrorFunc
- type DecodeRequestFunc
- type Delims
- type EncodeErrorFunc
- type EncodeResponseFunc
- type HTMLOptions
- type HandleOption
- type HandleOptions
- type HealthFunc
- type HealthHandler
- type Node
- type RenderOptions
- type Router
- func (m *Router) DELETE(path string, handler http.HandlerFunc)
- func (m *Router) GET(path string, handler http.HandlerFunc)
- func (m *Router) GetRequestRoute(req *http.Request) string
- func (m *Router) HEAD(path string, handler http.HandlerFunc)
- func (m *Router) Handle(path string, handler http.Handler)
- func (m *Router) HandleFunc(path string, handler http.HandlerFunc)
- func (m *Router) NotFound(handler http.Handler)
- func (m *Router) NotFoundFunc(handler http.HandlerFunc)
- func (m *Router) OPTIONS(path string, handler http.HandlerFunc)
- func (m *Router) PATCH(path string, handler http.HandlerFunc)
- func (m *Router) POST(path string, handler http.HandlerFunc)
- func (m *Router) PUT(path string, handler http.HandlerFunc)
- func (m *Router) Prefix(p string) *Router
- func (m *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)
- func (m *Router) SubRoute(path string, router http.Handler)
- type Server
- func (s *Server) Endpoint() (*url.URL, error)
- func (s *Server) Handle(path string, h http.Handler)
- func (s *Server) HandleFunc(path string, h http.HandlerFunc)
- func (s *Server) PathPrefix(prefix string, h http.Handler)
- func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request)
- func (s *Server) Start(ctx context.Context) error
- func (s *Server) Stop(ctx context.Context) error
- type ServerInfo
- type ServerOption
- type TemplateFile
- type TemplateFileSystem
- type TemplateSet
- type Token
- type TplFile
- type TplFileSystem
- type TplRender
- func (r *TplRender) HTML(status int, name string, data interface{}, htmlOpt ...HTMLOptions)
- func (r *TplRender) HTMLSet(status int, setName, tplName string, data interface{}, htmlOpt ...HTMLOptions)
- func (r *TplRender) HTMLSetBytes(setName, tplName string, data interface{}, htmlOpt ...HTMLOptions) ([]byte, error)
Constants ¶
const ( //PARAM 如果路由有参数,则值存储在 Kind 中 PARAM = 2 //SUB 如果路由是子路由器,则值存储在 Kind 中 SUB = 4 //WC 如果路由有通配符,则值存储在 Kind 中 WC = 8 //REGEX 如果路由包含正则表达式,则值存储在 Kind 中 REGEX = 16 )
const DefaultTplSetName = "DEFAULT"
const SupportPackageIsVersion1 = true
SupportPackageIsVersion1 不应从任何其他代码中引用这些常量.
Variables ¶
var (
TemplateEnv = "development"
)
Functions ¶
func CheckResponse ¶
CheckResponse 如果响应状态代码不是2xx,则返回错误(*Error类型).
func NewClientContext ¶
func NewClientContext(ctx context.Context, info ClientInfo) context.Context
NewClientContext 返回一个带有值的新Context.
func NewServerContext ¶
func NewServerContext(ctx context.Context, info ServerInfo) context.Context
NewServerContext 返回一个带有值的新Context.
func NewTransport ¶
func NewTransport(ctx context.Context, opts ...ClientOption) (http.RoundTripper, error)
NewTransport 创建一个http.RoundTripper.
func Process ¶
func Process(h HandleOptions, w http.ResponseWriter, r *http.Request, next func(ctx context.Context, req interface{}) (interface{}, error))
Types ¶
type ClientInfo ¶
ClientInfo 表示HTTP客户端信息.
func FromClientContext ¶
func FromClientContext(ctx context.Context) (info ClientInfo, ok bool)
FromClientContext 返回存储在ctx中的传输值(如果有).
type ClientOption ¶
type ClientOption func(*clientOptions)
ClientOption 是HTTP客户端选项.
func WithMiddleware ¶
func WithMiddleware(m middleware.Middleware) ClientOption
WithMiddleware 与客户端中间件.
type DecodeErrorFunc ¶
DecodeErrorFunc 是解码错误功能.
type DecodeRequestFunc ¶
DecodeRequestFunc 是解码请求函数.
type EncodeErrorFunc ¶
type EncodeErrorFunc func(http.ResponseWriter, *http.Request, error)
EncodeErrorFunc 是编码错误处理函数.
type EncodeResponseFunc ¶
type EncodeResponseFunc func(http.ResponseWriter, *http.Request, interface{}) error
EncodeResponseFunc 是编码响应函数.
type HTMLOptions ¶
type HTMLOptions struct {
Layout string // 布局模板名称.覆盖Options.Layout.
}
HTMLOptions 是用于覆盖特定HTML调用的某些呈现选项的结构
type HandleOption ¶
type HandleOption func(*HandleOptions)
HandleOption 是句柄选项.
func RequestDecoder ¶
func RequestDecoder(dec DecodeRequestFunc) HandleOption
RequestDecoder 带请求解码器.
func ResponseEncoder ¶
func ResponseEncoder(en EncodeResponseFunc) HandleOption
ResponseEncoder 带有响应编码器.
type HandleOptions ¶
type HandleOptions struct { Decode DecodeRequestFunc Encode EncodeResponseFunc Error EncodeErrorFunc Middleware middleware.Middleware }
HandleOptions 是处理器选项集.
func DefaultHandleOptions ¶
func DefaultHandleOptions() HandleOptions
DefaultHandleOptions 返回默认的处理器选项.
type HealthFunc ¶
HealthFunc 包装心跳检查方法.
HealthFunc 如果资源状况良好,则返回nil;如果资源状况不健康,则返回non-nil错误. HealthFunc 必须安全地从多个goroutine调用.
type HealthHandler ¶
type HealthHandler struct {
// contains filtered or unexported fields
}
HealthHandler 是一个HTTP处理器,报告了一组Checkers的成功.零值代表始终健康.
func (*HealthHandler) AddChecker ¶
func (h *HealthHandler) AddChecker(name string, c HealthFunc)
AddChecker 向处理程序添加新的检查.
func (*HealthHandler) AddObserver ¶
func (h *HealthHandler) AddObserver(name string, c HealthFunc)
AddObserver 向处理程序添加新的检查,但不会使整个状态失败.
func (*HealthHandler) ServeHTTP ¶
func (h *HealthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP 如果健康,则返回200,否则返回500.
type Node ¶ added in v0.0.4
type Node struct { Path string // 是节点 URL Method string // 在路由上定义 HTTP 方法 Size int //是路径的长度 Kind int Token Token // 是路径每个部分的值,分隔符为`/` Pattern map[int]string // 是关于路由的内容信息, 如果它有一个路由变量 Compile map[int]*regexp.Regexp Tag map[int]string Handler http.Handler // 是这条路由的处理者 // contains filtered or unexported fields }
Node 有效路由所需的信息
func CurrentRoute ¶ added in v0.0.4
CurrentRoute 返回当前请求的匹配路由(如果有). 这仅在匹配路由的处理程序内部调用时才有效,因为匹配的路由存储在请求上下文中,该上下文在处理程序返回后被清除.
type RenderOptions ¶
type RenderOptions struct { Directory string // 加载模板目标.默认为"templates". AppendDirectories []string // 附加目录会覆盖默认模板. Layout string // 布局模板名称. 如果为""代表不会渲染布局.默认是"". Extensions []string // 用于从中解析模板文件的扩展. 默认值为[".tmpl", ".html"]. Funcs []template.FuncMap // Funcs是FuncMap的一部分,可在编译时应用于模板. 这对于助手功能很有用. 默认是[]. Delims Delims // 将定界符设置为Delims结构中的指定字符串. Charset string // 将给定的字符集附加到Content-Type标头.默认是"UTF-8". HTMLContentType string // 允许将输出更改为XHTML而不是HTML.默认是"text/html". TemplateFileSystem // TemplateFileSystem是用于支持任何模板文件系统实现的接口. }
RenderOptions 表示用于指定Render中间件的配置选项的结构。.
type Router ¶ added in v0.0.4
type Router struct { CaseSensitive bool // contains filtered or unexported fields }
Router 是一个 http.Handler 可用于通过可配置的路由将请求分派到不同的处理程序函数
func NewRouter ¶ added in v0.0.4
func NewRouter(routers ...router) *Router
NewRouter 创建一个指向 Router 实例
func (*Router) DELETE ¶ added in v0.0.4
func (m *Router) DELETE(path string, handler http.HandlerFunc)
DELETE 使用 DELETE 方法向路由器添加新路由
func (*Router) GET ¶ added in v0.0.4
func (m *Router) GET(path string, handler http.HandlerFunc)
GET 使用 GET 方法向路由器添加新路由
func (*Router) GetRequestRoute ¶ added in v0.0.4
GetRequestRoute returns the route of given Request
func (*Router) HEAD ¶ added in v0.0.4
func (m *Router) HEAD(path string, handler http.HandlerFunc)
HEAD 使用 HEAD 方法向路由器添加新路由
func (*Router) HandleFunc ¶ added in v0.0.4
func (m *Router) HandleFunc(path string, handler http.HandlerFunc)
HandleFunc 用于传递 func(http.ResponseWriter, *Http.Request) 而不是 http.Handler
func (*Router) NotFoundFunc ¶ added in v0.0.4
func (m *Router) NotFoundFunc(handler http.HandlerFunc)
NotFoundFunc 路由器自定义 404 处理程序
func (*Router) OPTIONS ¶ added in v0.0.4
func (m *Router) OPTIONS(path string, handler http.HandlerFunc)
OPTIONS 使用 OPTIONS 方法向路由器添加新路由
func (*Router) PATCH ¶ added in v0.0.4
func (m *Router) PATCH(path string, handler http.HandlerFunc)
PATCH 使用 PATCH 方法向路由器添加新路由
func (*Router) POST ¶ added in v0.0.4
func (m *Router) POST(path string, handler http.HandlerFunc)
POST 使用 POST 方法向路由器添加新路由
func (*Router) PUT ¶ added in v0.0.4
func (m *Router) PUT(path string, handler http.HandlerFunc)
PUT 使用 PUT 方法向路由器添加新路由
func (*Router) Prefix ¶ added in v0.0.4
Prefix set a default prefix for all routes registered on the router
type Server ¶
Server 是一个HTTP服务器包装器.
func (*Server) HandleFunc ¶
func (s *Server) HandleFunc(path string, h http.HandlerFunc)
HandleFunc 使用URL的匹配器注册新路由.
func (*Server) PathPrefix ¶
PathPrefix 使用匹配器为URL路径前缀注册新路由.
type ServerInfo ¶
type ServerInfo struct { Request *http.Request Response http.ResponseWriter }
ServerInfo 表示HTTP服务器信息.
func FromServerContext ¶
func FromServerContext(ctx context.Context) (info ServerInfo, ok bool)
FromServerContext 返回存储在ctx中的传输值(如果有).
type TemplateFile ¶
TemplateFile 表示具有名称且可以读取的模板文件的接口.
type TemplateFileSystem ¶
type TemplateFileSystem interface { ListFiles() []TemplateFile Get(string) (io.Reader, error) }
TemplateFileSystem 表示能够列出所有文件的模板文件系统的接口.
type TemplateSet ¶
type TemplateSet struct {
// contains filtered or unexported fields
}
TemplateSet 表示类型为*template.Template的模板集。
func (*TemplateSet) GetDir ¶
func (ts *TemplateSet) GetDir(name string) string
func (*TemplateSet) Set ¶
func (ts *TemplateSet) Set(name string, opt *RenderOptions) *template.Template
type Token ¶ added in v0.0.4
type Token struct { Tokens []string //每个令牌的字符串值 Size int // 令牌数量 // contains filtered or unexported fields }
Token 分割路由路径的所有值
type TplFile ¶
type TplFile struct {
// contains filtered or unexported fields
}
TplFile 实现TemplateFile接口.
func NewTplFile ¶
NewTplFile 创建具有给定名称和数据的新模板文件.
type TplFileSystem ¶
type TplFileSystem struct {
// contains filtered or unexported fields
}
TplFileSystem 实现TemplateFileSystem接口.
func NewTemplateFileSystem ¶
func NewTemplateFileSystem(opt RenderOptions, omitData bool) TplFileSystem
NewTemplateFileSystem 使用给定的选项创建新的模板文件系统.
func (TplFileSystem) ListFiles ¶
func (fs TplFileSystem) ListFiles() []TemplateFile
type TplRender ¶
type TplRender struct { http.ResponseWriter *TemplateSet Opt *RenderOptions Charset string // contains filtered or unexported fields }
func (*TplRender) HTML ¶
func (r *TplRender) HTML(status int, name string, data interface{}, htmlOpt ...HTMLOptions)
func (*TplRender) HTMLSet ¶
func (r *TplRender) HTMLSet(status int, setName, tplName string, data interface{}, htmlOpt ...HTMLOptions)
func (*TplRender) HTMLSetBytes ¶
func (r *TplRender) HTMLSetBytes(setName, tplName string, data interface{}, htmlOpt ...HTMLOptions) ([]byte, error)