Documentation ¶
Index ¶
- Constants
- Variables
- func Handle(ctx context.Context, h ContextHandler, maxBodyBytes int64) httprouter.Handle
- func StdHandler(ctx context.Context, h ContextHandler, maxBodyBytes int64) http.Handler
- type BaseHandler
- func (h *BaseHandler) EncodeJSON(v interface{}) ([]byte, error)
- func (h *BaseHandler) Error(ctx context.Context, w http.ResponseWriter, err error)
- func (h *BaseHandler) OK(ctx context.Context, w http.ResponseWriter)
- func (h *BaseHandler) OKData(ctx context.Context, w http.ResponseWriter, data interface{})
- func (h *BaseHandler) ParseRequest(ctx context.Context, r *Request, req interface{}) error
- func (h *BaseHandler) WriteJSON(w http.ResponseWriter, v interface{}) error
- type Chain
- type ContextHandler
- func DELETE(h ContextHandler) ContextHandler
- func GET(h ContextHandler) ContextHandler
- func HEAD(h ContextHandler) ContextHandler
- func MethodOnly(method string, h ContextHandler) ContextHandler
- func OPTIONS(h ContextHandler) ContextHandler
- func PATCH(h ContextHandler) ContextHandler
- func POST(h ContextHandler) ContextHandler
- func PUT(h ContextHandler) ContextHandler
- func Recovery(h ContextHandler) ContextHandler
- func TraceID(h ContextHandler) ContextHandler
- type ContextHandlerFunc
- type ErrorInfo
- type ErrorInfoWithData
- type Middleware
- type RESTRouter
- func (r *RESTRouter) DELETE(pattern string, h ContextHandler)
- func (r *RESTRouter) GET(pattern string, h ContextHandler)
- func (r *RESTRouter) HEAD(pattern string, h ContextHandler)
- func (r *RESTRouter) Handle(method, pattern string, h ContextHandler)
- func (r *RESTRouter) HandleFunc(method, pattern string, h func(context.Context, ResponseWriter, *Request))
- func (r *RESTRouter) OPTIONS(pattern string, h ContextHandler)
- func (r *RESTRouter) PATCH(pattern string, h ContextHandler)
- func (r *RESTRouter) POST(pattern string, h ContextHandler)
- func (r *RESTRouter) PUT(pattern string, h ContextHandler)
- func (r *RESTRouter) SetMaxBodyBytes(n int64)
- type Request
- type ResponseWriter
- type Result
- type Router
- func (r *Router) DELETE(pattern string, h ContextHandler)
- func (r *Router) GET(pattern string, h ContextHandler)
- func (r *Router) HEAD(pattern string, h ContextHandler)
- func (r *Router) Handle(pattern string, h ContextHandler)
- func (r *Router) HandleFunc(pattern string, h func(context.Context, ResponseWriter, *Request))
- func (r *Router) OPTIONS(pattern string, h ContextHandler)
- func (r *Router) PATCH(pattern string, h ContextHandler)
- func (r *Router) POST(pattern string, h ContextHandler)
- func (r *Router) PUT(pattern string, h ContextHandler)
- func (r *Router) SetMaxBodyBytes(n int64)
- func (r *Router) StdHandle(pattern string, h http.Handler)
Constants ¶
const ( // HeaderContentType the header name of `Content-Type` HeaderContentType = "Content-Type" // MIMEApplicationJSON the application type for json MIMEApplicationJSON = "application/json" // MIMEApplicationJSONCharsetUTF8 the application type for json of utf-8 encoding MIMEApplicationJSONCharsetUTF8 = "application/json; charset=UTF-8" )
Variables ¶
var ( // ErrSuccess indicates api success ErrSuccess = NewError(errnoSuccess, "成功") ErrServerInternal = NewError(errnoInternal, "服务器内部错误") )
Functions ¶
func Handle ¶
func Handle(ctx context.Context, h ContextHandler, maxBodyBytes int64) httprouter.Handle
Handle adapt the ContextHandler to httprouter.Handle func
func StdHandler ¶
StdHandler adapt ContextHandler to http.Handler interface
Types ¶
type BaseHandler ¶ added in v1.0.5
type BaseHandler struct{}
BaseHandler is the enhanced version of ngs.BaseController
func (*BaseHandler) EncodeJSON ¶ added in v1.0.5
func (h *BaseHandler) EncodeJSON(v interface{}) ([]byte, error)
EncodeJSON is a wrapper of json.Marshal()
func (*BaseHandler) Error ¶ added in v1.0.5
func (h *BaseHandler) Error(ctx context.Context, w http.ResponseWriter, err error)
Error writes out an error response
func (*BaseHandler) OK ¶ added in v1.0.5
func (h *BaseHandler) OK(ctx context.Context, w http.ResponseWriter)
OK writes out a success response without data, used typically in an `update` api.
func (*BaseHandler) OKData ¶ added in v1.0.5
func (h *BaseHandler) OKData(ctx context.Context, w http.ResponseWriter, data interface{})
OKData writes out a success response with data, used typically in an `get` api.
func (*BaseHandler) ParseRequest ¶ added in v1.0.5
func (h *BaseHandler) ParseRequest(ctx context.Context, r *Request, req interface{}) error
ParseRequest parses and validates the api request
func (*BaseHandler) WriteJSON ¶ added in v1.0.5
func (h *BaseHandler) WriteJSON(w http.ResponseWriter, v interface{}) error
WriteJSON writes out an object which is serialized as json.
type Chain ¶
type Chain struct {
// contains filtered or unexported fields
}
Chain is the middleware chain
func NewChain ¶
func NewChain(middlewares ...Middleware) Chain
NewChain create a new middleware chain
func (Chain) Append ¶
func (c Chain) Append(middlewares ...Middleware) Chain
Append return a new middleware chain with new middleware appended
func (Chain) Then ¶
func (c Chain) Then(h ContextHandler) ContextHandler
Then return a handler wrapped by the middleware chain
func (Chain) ThenFunc ¶
func (c Chain) ThenFunc(h func(context.Context, ResponseWriter, *Request)) ContextHandler
ThenFunc return a handler wrapped by the middleware chain
type ContextHandler ¶
type ContextHandler interface {
ServeHTTP(context.Context, ResponseWriter, *Request)
}
ContextHandler defines the handler interface
func MethodOnly ¶
func MethodOnly(method string, h ContextHandler) ContextHandler
MethodOnly is a middleware to restrict http method for standard http router
func Recovery ¶ added in v1.0.6
func Recovery(h ContextHandler) ContextHandler
Recovery implements the recovery wrapper middleware
func TraceID ¶ added in v1.0.6
func TraceID(h ContextHandler) ContextHandler
type ContextHandlerFunc ¶
type ContextHandlerFunc func(context.Context, ResponseWriter, *Request)
ContextHandlerFunc defines the handler func adapter
func (ContextHandlerFunc) ServeHTTP ¶
func (h ContextHandlerFunc) ServeHTTP(ctx context.Context, w ResponseWriter, r *Request)
ServeHTTP implements the ContextHandler interface
type ErrorInfo ¶ added in v1.0.5
ErrorInfo defines the error type
func ErrBadParam ¶ added in v1.0.5
func ErrBadParam(v interface{}) ErrorInfo
ErrBadParam returns an instance of bad param ErrorInfo.
type ErrorInfoWithData ¶ added in v1.0.5
ErrorInfoWithData defines the error type with extra data
func NewErrorWithData ¶ added in v1.0.5
func NewErrorWithData(code int, message string, data interface{}) ErrorInfoWithData
NewErrorWithData create a errWithData instance
type Middleware ¶
type Middleware func(ContextHandler) ContextHandler
Middleware defines the middleware func
func Logging ¶ added in v1.0.6
func Logging(logger *zap.Logger) Middleware
Logging implements the request in/out logging middleware
type RESTRouter ¶
type RESTRouter struct { *httprouter.Router // contains filtered or unexported fields }
RESTRouter define the REST router
func NewRESTRouter ¶
func NewRESTRouter(ctx context.Context, logger *zap.Logger) *RESTRouter
NewRESTRouter create a REST router
func (*RESTRouter) DELETE ¶
func (r *RESTRouter) DELETE(pattern string, h ContextHandler)
DELETE register a handler for DELETE request
func (*RESTRouter) GET ¶
func (r *RESTRouter) GET(pattern string, h ContextHandler)
GET register a handler for GET request
func (*RESTRouter) HEAD ¶
func (r *RESTRouter) HEAD(pattern string, h ContextHandler)
HEAD register a handler for HEAD request
func (*RESTRouter) Handle ¶
func (r *RESTRouter) Handle(method, pattern string, h ContextHandler)
Handle register a http handler for the specified method and path
func (*RESTRouter) HandleFunc ¶
func (r *RESTRouter) HandleFunc(method, pattern string, h func(context.Context, ResponseWriter, *Request))
HandleFunc register a http handler for the specified method and path
func (*RESTRouter) OPTIONS ¶
func (r *RESTRouter) OPTIONS(pattern string, h ContextHandler)
OPTIONS register a handler for OPTIONS request
func (*RESTRouter) PATCH ¶
func (r *RESTRouter) PATCH(pattern string, h ContextHandler)
PATCH register a handler for PATCH request
func (*RESTRouter) POST ¶
func (r *RESTRouter) POST(pattern string, h ContextHandler)
POST register a handler for POST request
func (*RESTRouter) PUT ¶
func (r *RESTRouter) PUT(pattern string, h ContextHandler)
PUT register a handler for PUT request
func (*RESTRouter) SetMaxBodyBytes ¶
func (r *RESTRouter) SetMaxBodyBytes(n int64)
SetMaxBodyBytes set the body size limit
type Request ¶
type Request struct { *http.Request RestVars httprouter.Params RawBody []byte }
Request defines the http request
type ResponseWriter ¶
type ResponseWriter interface { http.ResponseWriter StatusCode() int RawBody() []byte }
ResponseWriter defines the response writer
type Result ¶ added in v1.0.5
type Result struct { ErrNO int `json:"errno"` ErrMsg string `json:"errmsg"` Data interface{} `json:"data,omitempty"` }
Result define the handle result for http request
type Router ¶
Router defines the standard http outer
func (*Router) DELETE ¶
func (r *Router) DELETE(pattern string, h ContextHandler)
DELETE register a handler for DELETE request
func (*Router) GET ¶
func (r *Router) GET(pattern string, h ContextHandler)
GET register a handler for GET request
func (*Router) HEAD ¶
func (r *Router) HEAD(pattern string, h ContextHandler)
HEAD register a handler for HEAD request
func (*Router) Handle ¶
func (r *Router) Handle(pattern string, h ContextHandler)
Handle register a http handler for the specified path
func (*Router) HandleFunc ¶
HandleFunc register a http handler for the specified path
func (*Router) OPTIONS ¶
func (r *Router) OPTIONS(pattern string, h ContextHandler)
OPTIONS register a handler for OPTIONS request
func (*Router) PATCH ¶
func (r *Router) PATCH(pattern string, h ContextHandler)
PATCH register a handler for PATCH request
func (*Router) POST ¶
func (r *Router) POST(pattern string, h ContextHandler)
POST register a handler for POST request
func (*Router) PUT ¶
func (r *Router) PUT(pattern string, h ContextHandler)
PUT register a handler for PUT request
func (*Router) SetMaxBodyBytes ¶
SetMaxBodyBytes set the body size limit
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
sqlbuilder
Package sqlbuilder is a flexible and powerful tool to build SQL string and associated args.
|
Package sqlbuilder is a flexible and powerful tool to build SQL string and associated args. |
Package redsync provides a Redis-based distributed mutual exclusion lock implementation as described in the post http://redis.io/topics/distlock.
|
Package redsync provides a Redis-based distributed mutual exclusion lock implementation as described in the post http://redis.io/topics/distlock. |
skel
|
|