http

package
v0.0.0-...-15bed1d Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2022 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrIsCode

func ErrIsCode(err error, code int) bool

IsCode reports whether any error in err's chain contains the given error code.

func IteratorErr

func IteratorErr(fn func(*Err) error) error

IteratorErr 遍历错误码

func MustRegisterErr

func MustRegisterErr(err *Err)

MustRegister 注册错误码

func NewHandler

func NewHandler(handler HandlerFunc) gin.HandlerFunc

func NewHandlerFuncChain

func NewHandlerFuncChain(handler ...HandlerFunc) gin.HandlerFunc

func OverWriteErr

func OverWriteErr(err *Err)

覆盖错误码,重定义系统错误

func SetClaim

func SetClaim(ctx context.Context, claim *Claim) context.Context

func SetConfig

func SetConfig(ctx context.Context, cfg *HTTPConf) context.Context

Types

type Claim

type Claim struct {
	ID            string // user id
	UserValue     string // user value
	Secret        string // secret for jwt
	Name          string // username
	Token         string
	ClientVersion string
	SinceTime     time.Time
	TraceID       string
	DBName        string                 // 分库
	TablePrefix   string                 // 分表
	Debug         bool                   // if debug user logger will very detailed
	FakeTime      string                 // faketime for debug mode user
	Extra         map[string]interface{} `json:"extra"`
}

func GetClaim

func GetClaim(ctx context.Context) (claim *Claim, ok bool)

type Context

type Context interface {
	Context() context.Context
	GinContext() *gin.Context

	Send(data interface{}, err error)
	SendOk(data ...interface{}) // SendOk(data interface{})
	SendErr(err error)
	SendErrCode(errcode int)
	SendParamErr(err ...error)
	SendInternalErr(err ...error)
	Stream(closer io.ReadCloser)
	HTML(tmpl string, data interface{})

	Bind(param interface{}) error
	BindJSON(param interface{}) error
	BindUri(param interface{}) error
	BindQuery(param interface{}) error
	BindHeader(obj interface{}) error
	Param(key string) string

	Next()
	Abort()
	AbortWithStatus(code int)
	AbortWithStatusJSON(code int, jsonObj interface{})

	GetHeader(key string) string
	Header(key, value string)
	Set(key string, value interface{})
	Get(key string) (interface{}, bool)

	GetClaim() *Claim
	SetClaim(claim *Claim)
}

func NewContext

func NewContext(ctx *gin.Context) Context

NewContext returns a new Context 1. 限制可以使用的方法; 2. 和添加默认处理;

func NewMockContext

func NewMockContext() Context

type Controller

type Controller interface {
	Register(route IRoutes)
}

type Engine

type Engine interface {
	Run(clean func()) error
	Shutdown() error
}

type Err

type Err struct {
	Code         int      // 错误码
	HTTP         int      // http status code
	Msg          string   // error message
	Ref          []string // reference to error code
	MetricIgnore bool     // 是否忽略metric
}

func NewErr

func NewErr(code int, msg string, httpStatus int, ingore bool, ref ...string) *Err

func ParseErr

func ParseErr(code int) *Err

ParseErr 解析错误码

func (*Err) Error

func (e *Err) Error() string

func (*Err) With

func (e *Err) With(err ...error) *Err

type ErrResponse

type ErrResponse struct {
	Success bool   `json:"success" example:"false"`
	Trace   string `json:"trace,omitempty" example:"uuid"`
	Code    int    `json:"code" example:"400000000"`
	Msg     string `json:"msg" example:"error message"`
}

type HTTPConf

type HTTPConf struct {
	Enable         bool   `default:"true"`
	Mode           string `default:"release" opts:"release,debug,test"`
	BaseRoute      string `default:""`
	ColorConsole   bool   `default:"false"`
	Port           int    `default:"8080"`
	Host           string `default:"127.0.0.1"`
	ReadSecond     int    `default:"30"`
	WriteSecond    int    `default:"30"`
	ConnSecond     int    `default:"30"`
	ShutdownSecond int    ` default:"30" validate:"min=1,max=60"`
	TLS            TLS
	Metrics        Metrics
}

func GetConfig

func GetConfig(ctx context.Context) (cfg *HTTPConf, ok bool)

func (*HTTPConf) NewEngine

func (c *HTTPConf) NewEngine(ctx context.Context, registerRoute RegisterRoute) Engine

func (*HTTPConf) String

func (c *HTTPConf) String() string

type HandlerFunc

type HandlerFunc func(ctx Context)

func RequestLogger

func RequestLogger(skip map[string]struct{}) HandlerFunc

type Health

type Health struct {
	Endpoint string `default:"/health"`
}

type IRoutes

type IRoutes interface {
	Use(...HandlerFunc)
	Group(string, ...HandlerFunc) IRoutes

	Handle(string, string, ...HandlerFunc)
	Any(string, ...HandlerFunc)
	GET(string, ...HandlerFunc)
	POST(string, ...HandlerFunc)
	DELETE(string, ...HandlerFunc)
	PATCH(string, ...HandlerFunc)
	PUT(string, ...HandlerFunc)
	OPTIONS(string, ...HandlerFunc)
	HEAD(string, ...HandlerFunc)

	StaticFile(string, string)
	Static(string, string)
	StaticFS(string, http.FileSystem)

	Context() context.Context
}

IRoutes implements IRoutes; it's a wrapper around a gin.IRouter

func NewGinRouter

func NewGinRouter(ctx context.Context, router gin.IRouter) IRoutes

type Metrics

type Metrics struct {
	Enable    bool   `default:"true"`
	Namespace string `default:""`
	Endpoint  string `default:"/metrics"`
}

type PageResponse

type PageResponse struct {
	Page     int64       `json:"page" example:"1"`
	PageSize int64       `json:"page_size" example:"10"`
	Total    int64       `json:"total" example:"0"`
	Data     interface{} `json:"data"`
}

func NewPaginationRes

func NewPaginationRes(total, page, pageSize int64, data interface{}) *PageResponse

type PaginationResponse

type PaginationResponse struct {
	Response
	Data PageResponse `json:"data"`
}

type RegisterRoute

type RegisterRoute func(IRoutes)

type Response

type Response struct {
	Trace   string      `json:"trace,omitempty" example:"uuid"`
	Success bool        `json:"success" example:"true"`
	Code    int         `json:"code" example:"0"`
	Msg     string      `json:"msg" example:"ok"`
	Data    interface{} `json:"data"`
}

func NewErrorResponse

func NewErrorResponse(code int, msg, traceID string, ref ...string) *Response

func NewResponse

func NewResponse(code int, msg string, data interface{}, traceID string) *Response

func NewSuccessResponse

func NewSuccessResponse(data interface{}, traceID string) *Response

type TLS

type TLS struct {
	Enable      bool   `default:"false"`
	LetsEncrypt bool   `default:"false"`
	CacheDir    string `default:"/var/cache/letsencrypt"`
	Domain      string `default:""`
	KeyFile     string
	CertFile    string
}

Directories

Path Synopsis
Package servermock is a generated GoMock package.
Package servermock is a generated GoMock package.

Jump to

Keyboard shortcuts

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