Documentation ¶
Index ¶
- Constants
- Variables
- func EncodeJSON(v interface{}) ([]byte, error)
- func Error(ctx context.Context, w http.ResponseWriter, err interface{})
- func Logging(logger *zap.Logger) kate.Middleware
- func OK(ctx context.Context, w http.ResponseWriter)
- func OKData(ctx context.Context, w http.ResponseWriter, data interface{})
- func Recovery(h kate.ContextHandler) kate.ContextHandler
- func Start(upgrader *tableflip.Upgrader, logger *zap.Logger)
- func Stop()
- func TraceID(h kate.ContextHandler) kate.ContextHandler
- func WriteJSON(w http.ResponseWriter, v interface{}) error
- type BaseHandler
- func (h *BaseHandler) EncodeJSON(v interface{}) ([]byte, error)
- func (h *BaseHandler) Error(ctx context.Context, w http.ResponseWriter, err interface{})
- 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 *kate.Request, req interface{}) error
- func (h *BaseHandler) WriteJSON(w http.ResponseWriter, v interface{}) error
- type ErrorInfo
- type ErrorInfoWithData
- type HelloHandler
- type Result
Constants ¶
const ( // HeaderContentLength the header name of `Content-Length` HeaderContentLength = "Content-Length" // 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 EncodeJSON ¶
EncodeJSON is a wrapper of json.Marshal()
func Error ¶
func Error(ctx context.Context, w http.ResponseWriter, err interface{})
Error writes out an error response
func Logging ¶
func Logging(logger *zap.Logger) kate.Middleware
Logging implements the request in/out logging middleware
func OK ¶
func OK(ctx context.Context, w http.ResponseWriter)
OK writes out a success response without data, used typically in an `update` api.
func OKData ¶
func OKData(ctx context.Context, w http.ResponseWriter, data interface{})
OKData writes out a success response with data, used typically in an `get` api.
func Recovery ¶
func Recovery(h kate.ContextHandler) kate.ContextHandler
Recovery implements the recovery wrapper middleware
func TraceID ¶
func TraceID(h kate.ContextHandler) kate.ContextHandler
func WriteJSON ¶
func WriteJSON(w http.ResponseWriter, v interface{}) error
WriteJSON writes out an object which is serialized as json.
Types ¶
type BaseHandler ¶
type BaseHandler struct{}
BaseHandler is the enhanced version of ngs.BaseController
func (*BaseHandler) EncodeJSON ¶
func (h *BaseHandler) EncodeJSON(v interface{}) ([]byte, error)
EncodeJSON is a wrapper of json.Marshal()
func (*BaseHandler) Error ¶
func (h *BaseHandler) Error(ctx context.Context, w http.ResponseWriter, err interface{})
Error writes out an error response
func (*BaseHandler) OK ¶
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 ¶
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 ¶
ParseRequest parses and validates the api request nolint:lll,gocyclo
func (*BaseHandler) WriteJSON ¶
func (h *BaseHandler) WriteJSON(w http.ResponseWriter, v interface{}) error
WriteJSON writes out an object which is serialized as json.
type ErrorInfo ¶
ErrorInfo defines the error type
func ErrBadParam ¶
func ErrBadParam(v interface{}) ErrorInfo
ErrBadParam returns a instance of bad param ErrorInfo.
type ErrorInfoWithData ¶
ErrorInfoWithData defines the error type with extra data
func NewErrorWithData ¶
func NewErrorWithData(code int, message string, data interface{}) ErrorInfoWithData
NewErrorWithData create a errWithData instance
type HelloHandler ¶
type HelloHandler struct {
BaseHandler
}
func (*HelloHandler) ServeHTTP ¶
func (h *HelloHandler) ServeHTTP(ctx context.Context, w kate.ResponseWriter, r *kate.Request)