Documentation
¶
Index ¶
Constants ¶
View Source
const ( MethodGet = http.MethodGet MethodHead = http.MethodHead MethodPost = http.MethodPost MethodPut = http.MethodPut MethodPatch = http.MethodPatch // RFC 5789 MethodDelete = http.MethodDelete MethodConnect = http.MethodConnect MethodOptions = http.MethodOptions MethodTrace = http.MethodTrace MethodAny = "ANY" //for all methods support )
View Source
const ( // HeaderContentType HTTP Header 中 Content-Type 的 Key HeaderContentType = "Content-Type" // HeaderContentLength HTTP Header 中 Content-Length 的 Key HeaderContentLength = "Content-Length" )
View Source
const ( Worker = "worker" Master = "master" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ContextKey ¶
type ContextKey struct {
Name string
}
ContextKey is a value for use with context.WithValue. It's used as a pointer, so it fits in an interface{} without allocation. This technique for defining context keys was copied from Go 1.7's new use of context in net/http.
func (*ContextKey) String ¶
func (k *ContextKey) String() string
type HandlerFunc ¶
HandlerFunc 标准的HandlerFunc
var NotFoundHandler HandlerFunc = func(ctx context.Context, req Request) Response { return NopResponse(http.StatusNotFound) }
NotFoundHandler 404默认处理器
func (HandlerFunc) Handler ¶
func (h HandlerFunc) Handler() http.Handler
Handler HandlerFunc自身转换为http.Handler
type MSeed ¶
type MSeed interface {
Router
// HTTPServer 返回实例的 *http.Server
HTTPServer() *http.Server
// 设置默认的http server
SetHTTPServer(srv *http.Server) MSeed
// Run 启动HTTPServer
//
// addrs 监听的端口,非必选,但如果没有通过HTTPServer pointer修改且值不给可能导致服务启动失败
Run(addrs ...string) error
// Run 启动HTTPServer
//
// certFile https certFile
// keyFile https keyFile
// addrs 监听的端口,非必选,但如果没有通过HTTPServer pointer修改且值不给可能导致服务启动失败
RunTLS(certFile, keyFile string, addrs ...string) error
}
type MiddleWareQueue ¶
type MiddleWareQueue interface {
Next(ctx context.Context, w http.ResponseWriter, req *http.Request) bool
}
MiddleWareQueue 中间件执行队列
func NewMiddleWareQueue ¶
func NewMiddleWareQueue(funcs ...MiddlewareFunc) MiddleWareQueue
NewMiddleWareQueue 创建一个中间件执行队列
type MiddlewareFunc ¶
type MiddlewareFunc func(ctx context.Context, w http.ResponseWriter, req *http.Request, next MiddleWareQueue) bool
MiddlewareFunc 中间件执行器
type MiddlewareFuncs ¶
type MiddlewareFuncs []MiddlewareFunc
MiddlewareFuncs 中间件执行器队列示例
func (MiddlewareFuncs) Next ¶
func (ms MiddlewareFuncs) Next(ctx context.Context, w http.ResponseWriter, req *http.Request) bool
Next 触发下一个 MiddleWareFunc 或者是业务 handler
执行顺序: filter1 -> filter2 -> filter3 若 filter2 返回 false,调用将终止,即 filter3 不会被执行
type Request ¶
type Request interface {
// HTTPRequest 返回原始 *http.Request
HTTPRequest() *http.Request
// Query 获取GET方式传递的参数
Query(name string) (value string, has bool)
// QueryDefault 获取GET方式传递的参数如果没有那么返回默认值/空值
QueryDefault(name string, defaultValue ...string) (value string)
// PostForm 获取POST方式传递的参数
PostForm(name string) (value string, has bool)
// PostFormDefault 获取POST方式传递的参数如果没有那么返回默认值/空值
PostFormDefault(name string, defaultValue ...string) (value string)
// Param 获取GET/POST方式传递的参数
Param(name string) (value string, has bool)
// ParamDefault 获取GET/POST方式传递的参数如果没有那么返回默认值/空值
ParamDefault(name string, defaultValue ...string) (value string)
// Header 获取Header传递的参数
Header(name string) (value string, has bool)
// HeaderDefault 获取Header方式传递的参数如果没有那么返回默认值/空值
HeaderDefault(name string, defaultValue ...string) (value string)
// Cookie 获取Cookie方式传递的参数
Cookie(name string) (value *http.Cookie, has bool)
// RemoteAddr 获取客户端的请求地址
RemoteAddr() string
// JsonUnmarshal json序列化参数到目标数据
JsonUnmarshal(dst interface{}) error
}
type Response ¶
type Response interface {
WriteTo(w http.ResponseWriter) error
}
func HtmlResponse ¶
func JsonResponse ¶
JsonResponse 返回JsonResponse
type RouteMapper ¶
RouteMapper 路由匹配器
type Router ¶
type Router interface {
http.Handler
// Use 注册过滤器(中间件)
//
// 可用于给 server 增加切片的功能
// 如可以创建注册一个用于校验是否登录的 中间件
// 注册的中间件会对此后的 handler 生效,在此之前注册的 handler 方法不会生效
Use(ms ...MiddlewareFunc) Router
// HandleStd 以http.Handler方式注册业务handler
//
// method 是http方法,如GET、POST,也可以使用逗号来连接同时传入多个,如 "GET,POST"
// 也可以用特殊的 ANY,会自动注册所有( ANY 的取值详见 MethodAny )
// handler 是业务的逻辑
// ms 是该接口特有的中间件函数
HandleStd(methods string, path string, handler http.Handler, ms ...MiddlewareFunc)
// HandleFunc 以HandlerFunc方式注册业务handler
//
// method 是http方法,如GET、POST,也可以使用逗号来连接同时传入多个,如 "GET,POST"
// 也可以用特殊的 ANY,会自动注册所有( ANY 的取值详见 MethodAny )
// handler 是业务的逻辑
// ms 是该接口特有的中间件函数
HandleFunc(methods string, path string, handlerFunc HandlerFunc, ms ...MiddlewareFunc)
// Group 路由分组
//
// 如 可以将 /user/xxx 系列分成一个分组
// prefix路由前缀,如 "/user"
// ms 是该分组的中间件函数
Group(prefix string, f func(r Router), ms ...MiddlewareFunc)
}
Router 路由器
Source Files
¶
Click to show internal directories.
Click to hide internal directories.