base

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2022 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EncodeJson = "_json"
	EncodeForm = "_form"
	EncodeRaw  = "_raw"
)
View Source
const HttpHeaderService = "SERVICE"
View Source
const HttpUrlPressureTestKey = "_call_uri"

Variables

This section is empty.

Functions

func HealthProbe

func HealthProbe() gin.HandlerFunc

func InitHttp

func InitHttp(opts *TransportOption)

初始化全局的transport

func InitMysqlClient

func InitMysqlClient(conf MysqlConf) (client *gorm.DB, err error)

func NewESClient

func NewESClient(cfg ElasticClientConfig) (*elastic.Client, error)

func ReadyProbe

func ReadyProbe() gin.HandlerFunc

func RegHealthProbe

func RegHealthProbe(h gin.HandlerFunc)

func RegReadyProbe

func RegReadyProbe(h gin.HandlerFunc)

func RegisterProf

func RegisterProf()

func RenderJson

func RenderJson(ctx *gin.Context, code int, msg string, data interface{})

func RenderJsonAbort

func RenderJsonAbort(ctx *gin.Context, err error)

func RenderJsonFail

func RenderJsonFail(ctx *gin.Context, err error)

func RenderJsonSucc

func RenderJsonSucc(ctx *gin.Context, data interface{})

func SetErrPrintfMsg

func SetErrPrintfMsg(err *Error, v ...interface{})

func StackLogger

func StackLogger(ctx *gin.Context, err error)

打印错误栈

Types

type ApiClient

type ApiClient struct {
	Service        string        `yaml:"service"`
	AppKey         string        `yaml:"appkey"`
	AppSecret      string        `yaml:"appsecret"`
	Domain         string        `yaml:"domain"`
	Timeout        time.Duration `yaml:"timeout"`
	ConnectTimeout time.Duration `yaml:"connectTimeout"`
	Retry          int           `yaml:"retry"`
	HttpStat       bool          `yaml:"httpStat"`
	Host           string        `yaml:"host"`
	Proxy          string        `yaml:"proxy"`
	BasicAuth      struct {
		Username string `yaml:"username"`
		Password string `yaml:"password"`
	}

	HTTPClient *http.Client
	// contains filtered or unexported fields
}

func (*ApiClient) GetTransPort

func (client *ApiClient) GetTransPort() *http.Transport

func (*ApiClient) HttpGet

func (client *ApiClient) HttpGet(ctx *gin.Context, path string, opts HttpRequestOptions) (*ApiResult, error)

func (*ApiClient) HttpPost

func (client *ApiClient) HttpPost(ctx *gin.Context, path string, opts HttpRequestOptions) (*ApiResult, error)

func (*ApiClient) HttpPostJson

func (client *ApiClient) HttpPostJson(ctx *gin.Context, path string, opts HttpRequestOptions) (*ApiResult, error)

deprecated , use HttpPost instead

type ApiResult

type ApiResult struct {
	HttpCode int
	Response []byte
	Ctx      *gin.Context
}

type BackOffPolicy

type BackOffPolicy func(attemptCount int) time.Duration

重试策略

type DefaultRender

type DefaultRender struct {
	ErrNo  int         `json:"errNo"`
	ErrMsg string      `json:"errMsg"`
	Data   interface{} `json:"data"`
}

default render

type ElasticClientConfig

type ElasticClientConfig struct {
	Addr     string `yaml:"addr"`
	Service  string `yaml:"service"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`

	Sniff       bool `yaml:"sniff"`
	HealthCheck bool `yaml:"healthCheck"`
	Gzip        bool `yaml:"gzip"`

	Decoder       elastic.Decoder
	RetryStrategy elastic.Retrier
	HttpClient    *http.Client
	Other         []elastic.ClientOptionFunc
}

type Error

type Error struct {
	ErrNo  int
	ErrMsg string
}

func NewBaseError

func NewBaseError(code int, message string) *Error

func NewError

func NewError(code int, message, userMsg string) Error

func (Error) Equal

func (err Error) Equal(e error) bool

func (Error) Error

func (err Error) Error() string

func (Error) Sprintf

func (err Error) Sprintf(v ...interface{}) Error

func (Error) Wrap

func (err Error) Wrap(core error) error

func (Error) WrapPrint

func (err Error) WrapPrint(core error, message string, user ...interface{}) error

func (Error) WrapPrintf

func (err Error) WrapPrintf(core error, format string, message ...interface{}) error

type HttpRequestOptions

type HttpRequestOptions struct {
	// 通用请求体,可通过Encode来对body做编码
	RequestBody interface{}
	// 针对 RequestBody 的编码
	Encode string
	// 老的请求data,httpGet / httPost 仍支持
	Data map[string]string
	// httpPostJson 参数
	JsonBody interface{}
	// 请求头指定
	Headers map[string]string
	// cookie 设定
	Cookies map[string]string

	/*
		httpGet / httPost 默认 application/x-www-form-urlencoded
		httpPostJson 默认 application/json
	*/
	BodyType string
	// 重试策略,可不指定,默认使用`defaultRetryPolicy`(只有在`api.yaml`中指定retry>0 时生效)
	RetryPolicy RetryPolicy
	// 重试间隔机制,可不指定,默认使用`defaultBackOffPolicy`(只有在`api.yaml`中指定retry>0 时生效)
	BackOffPolicy BackOffPolicy
}

func (*HttpRequestOptions) GetBackOffPolicy

func (o *HttpRequestOptions) GetBackOffPolicy() BackOffPolicy

func (*HttpRequestOptions) GetContentType

func (o *HttpRequestOptions) GetContentType() (cType string)

func (*HttpRequestOptions) GetData

func (o *HttpRequestOptions) GetData() (string, error)

func (*HttpRequestOptions) GetJsonData

func (o *HttpRequestOptions) GetJsonData() (string, error)

func (*HttpRequestOptions) GetRequestData

func (o *HttpRequestOptions) GetRequestData() (encodeData string, err error)

func (*HttpRequestOptions) GetRetryPolicy

func (o *HttpRequestOptions) GetRetryPolicy() RetryPolicy

func (*HttpRequestOptions) GetUrlData

func (o *HttpRequestOptions) GetUrlData() (string, error)

type MysqlConf

type MysqlConf struct {
	Service         string        `yaml:"service"`
	DataBase        string        `yaml:"database"`
	Addr            string        `yaml:"addr"`
	User            string        `yaml:"user"`
	Password        string        `yaml:"password"`
	Charset         string        `yaml:"charset"`
	MaxIdleConns    int           `yaml:"maxidleconns"`
	MaxOpenConns    int           `yaml:"maxopenconns"`
	ConnMaxLifeTime time.Duration `yaml:"connMaxLifeTime"`
	ConnTimeOut     time.Duration `yaml:"connTimeOut"`
	WriteTimeOut    time.Duration `yaml:"writeTimeOut"`
	ReadTimeOut     time.Duration `yaml:"readTimeOut"`
}

type PprofConfig

type PprofConfig struct {
	Enable bool `yaml:"enable"`
}

type Probe

type Probe struct {
	// contains filtered or unexported fields
}

type RedisClient

type RedisClient struct {
	*redis.Redis
}

func InitRedisClient

func InitRedisClient(conf RedisConf) *RedisClient

deprecated use golib/redis.InitRedisClient() instead

func (*RedisClient) Do

func (r *RedisClient) Do(ctx *gin.Context, commandName string, args ...interface{}) (reply interface{}, err error)

type RedisConf

type RedisConf struct {
	Service      string        `yaml:"service"`
	Addr         string        `yaml:"addr"`
	Password     string        `yaml:"password"`
	MaxIdle      int           `yaml:"maxIdle"`
	MaxActive    int           `yaml:"maxActive"`
	IdleTimeout  time.Duration `yaml:"idleTimeout"`
	ConnTimeOut  time.Duration `yaml:"connTimeOut"`
	ReadTimeOut  time.Duration `yaml:"readTimeOut"`
	WriteTimeOut time.Duration `yaml:"writeTimeOut"`
}

deprecated use golib/redis.Conf instead

type RetryPolicy

type RetryPolicy func(resp *http.Response, err error) bool

retry 策略

type TransportOption

type TransportOption struct {
	MaxIdleConns        int
	MaxIdleConnsPerHost int
	IdleConnTimeout     time.Duration
	CustomTransport     *http.Transport
}

Jump to

Keyboard shortcuts

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