kate

package module
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2023 License: Apache-2.0 Imports: 14 Imported by: 1

README

kate service framework

Documentation

Index

Constants

View Source
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

View Source
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

func StdHandler(ctx context.Context, h ContextHandler, maxBodyBytes int64) http.Handler

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

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 ContextHandler

type ContextHandler interface {
	ServeHTTP(context.Context, ResponseWriter, *Request)
}

ContextHandler defines the handler interface

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

type ErrorInfo interface {
	error
	Code() int
}

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.

func NewError added in v1.0.5

func NewError(code int, message string) ErrorInfo

NewError create an errSimple instance

type ErrorInfoWithData added in v1.0.5

type ErrorInfoWithData interface {
	error
	Code() int
	Data() interface{}
}

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 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

type Router struct {
	*http.ServeMux
	// contains filtered or unexported fields
}

Router defines the standard http outer

func NewRouter

func NewRouter(ctx context.Context, logger *zap.Logger) *Router

NewRouter create a http router

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

func (r *Router) HandleFunc(pattern string, h func(context.Context, ResponseWriter, *Request))

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

func (r *Router) SetMaxBodyBytes(n int64)

SetMaxBodyBytes set the body size limit

func (*Router) StdHandle

func (r *Router) StdHandle(pattern string, h http.Handler)

StdHandle register a standard http handler for the specified path

Directories

Path Synopsis
log
orm
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
csv

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL