kate

package module
v1.0.38 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2023 License: Apache-2.0 Imports: 16 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"
)
View Source
const HeaderTraceId = "X-Trace-Id"

Variables

View Source
var (
	// ErrSuccess indicates api success
	ErrSuccess        = NewError(errnoSuccess, "成功")
	ErrServerInternal = NewError(errnoInternal, "服务器内部错误")
)
View Source
var Recovery = MiddlewareFunc(recoveryFunc)

Recovery implements the recovery wrapper middleware

View Source
var TraceId = MiddlewareFunc(traceIdFunc)

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

func (h *BaseHandler) EncodeJson(v any) ([]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.33

Ok writes out a success response without data, used typically in an `update` api.

func (*BaseHandler) OkData added in v1.0.33

func (h *BaseHandler) OkData(ctx context.Context, w http.ResponseWriter, data any)

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 any) error

ParseRequest parses and validates the api request

func (*BaseHandler) WriteJson added in v1.0.33

func (h *BaseHandler) WriteJson(w http.ResponseWriter, v any) 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

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 DELETE

DELETE only allow DELETE method

func GET

GET only allow GET method

HEAD only allow HEAD method

func MethodOnly

func MethodOnly(method string, h ContextHandler) ContextHandler

MethodOnly is used to restrict http method for standard http router

func OPTIONS

func OPTIONS(h ContextHandler) ContextHandler

OPTIONS only allow OPTIONS method

func PATCH

PATCH only allow PATCH method

func POST

POST only allow POST method

func PUT

PUT only allow PUT method

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 any) 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() any
}

ErrorInfoWithData defines the error type with extra data

func NewErrorWithData added in v1.0.5

func NewErrorWithData(code int, message string, data any) ErrorInfoWithData

NewErrorWithData create a errWithData instance

type Middleware

type Middleware interface {
	Proxy(handler ContextHandler) ContextHandler
}

Middleware defines the middleware interface

func Cached added in v1.0.26

func Cached(size int, ttl time.Duration) Middleware

Cached implements the cached middleware

func Logging added in v1.0.6

func Logging(logger *zap.Logger) Middleware

Logging implements the request in/out logging middleware

type MiddlewareFunc added in v1.0.30

type MiddlewareFunc func(ContextHandler) ContextHandler

MiddlewareFunc defines the middleware func

func (MiddlewareFunc) Proxy added in v1.0.30

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