layer

package
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2021 License: MIT Imports: 32 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HttpHeaderService = "SERVICE"
	// trace 日志前缀标识(放在[]zap.Field的第一个位置提高效率)
	TopicType = "_tp"
	// 业务日志名字
	LogNameServer = "server"
	// access 日志文件名字
	LogNameAccess = "access"
	// module 日志文件名字
	LogNameModule = "module"

	TraceHeaderKey      = "Uber-Trace-Id"
	LogIDHeaderKey      = "X_BD_LOGID"
	LogIDHeaderKeyLower = "x_bd_logid"
)

Variables

View Source
var (
	//StackLogger func(ctx *gin.Context, err error)
	ErrMsgMap                  map[int]string
	NmqResponseStatusCodeError = 500
	NmqRetryError              = 34002
)

Functions

func ErrorToRanderJson

func ErrorToRanderJson(err error) puzzle.DefaultRender

func GetFormatRequestTime

func GetFormatRequestTime(time time.Time) string

func GetRequestCost

func GetRequestCost(start, end time.Time) float64

func GetRequestData

func GetRequestData(requestBody interface{}) (encodeData string, err error)

func GetUrlData

func GetUrlData(data map[string]string) (string, error)

Types

type Api

type Api struct {
	Flow
	ApiConf ApiConf
}

func (*Api) HttpGet

func (entity *Api) HttpGet(uri string, params map[string]string, header map[string]string) (data []byte, err error)

func (*Api) HttpPost

func (entity *Api) HttpPost(uri string, params map[string]string, header map[string]string) (data []byte, err error)

func (*Api) HttpPostJSON

func (entity *Api) HttpPostJSON(uri string, params map[string]interface{}, header map[string]string) (data []byte, err error)

func (*Api) MakeRequest

func (entity *Api) MakeRequest(method, url string, data io.Reader, headers map[string]string, cookies map[string]string, bodyType string) (*http.Request, error)

func (*Api) PostData

func (entity *Api) PostData(uri string, params map[string]interface{}) (data []byte, err error)

func (*Api) PostDataJson

func (entity *Api) PostDataJson(uri string, params map[string]interface{}, header map[string]string) (data []byte, err error)

func (*Api) PostForm

func (entity *Api) PostForm(uri string, params map[string]interface{}, header *map[string]string) ([]byte, error)

func (*Api) PreUse

func (entity *Api) PreUse(args ...interface{})

func (*Api) RalPost

func (entity *Api) RalPost(uri string, params map[string]interface{}, header map[string]string) (*puzzle.DefaultRender, error)

func (*Api) Request

func (entity *Api) Request(method string, uri string, reqBody []byte, header map[string]string, cookies map[string]string, contentType string) (data []byte, err error)

type ApiConf

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

func (*ApiConf) GetTransPort

func (entity *ApiConf) GetTransPort() *http.Transport

type ApiRequest

type ApiRequest struct {
	Req *http.Request

	HTTPClient *http.Client
	// 重试策略,可不指定,默认使用`defaultRetryPolicy`(只有在`api.yaml`中指定retry>0 时生效)
	RetryPolicy RetryPolicy
	// 重试间隔机制,可不指定,默认使用`defaultBackOffPolicy`(只有在`api.yaml`中指定retry>0 时生效)
	BackOffPolicy BackOffPolicy
	// contains filtered or unexported fields
}

func (*ApiRequest) GetBackOffPolicy

func (entity *ApiRequest) GetBackOffPolicy() BackOffPolicy

func (*ApiRequest) GetRetryPolicy

func (entity *ApiRequest) GetRetryPolicy() RetryPolicy

type BackOffPolicy

type BackOffPolicy func(attemptCount int) time.Duration

重试策略

type BaseError

type BaseError interface {
}

type Controller

type Controller struct {
	Flow
}

func (*Controller) Action

func (entity *Controller) Action()

func (*Controller) BindParam

func (entity *Controller) BindParam(req interface{}) bool

func (*Controller) BindParamError

func (entity *Controller) BindParamError(req interface{}) error

func (*Controller) RenderHttpError

func (entity *Controller) RenderHttpError(errNo int, errDetail ...interface{})

func (*Controller) RenderJsonAbort

func (entity *Controller) RenderJsonAbort(err error)

func (*Controller) RenderJsonFail

func (entity *Controller) RenderJsonFail(err error)

func (*Controller) RenderJsonSucc

func (entity *Controller) RenderJsonSucc(data interface{})

type Dao

type Dao struct {
	Flow
	// contains filtered or unexported fields
}

func (*Dao) Create

func (entity *Dao) Create(value interface{}) error

func (*Dao) GetDB

func (entity *Dao) GetDB(args ...string) *gorm.DB

func (*Dao) GetTable

func (entity *Dao) GetTable() string

func (*Dao) PreUse

func (entity *Dao) PreUse(args ...interface{})

默认第一个参数为db,可利用该特性批量处理事务

func (*Dao) Printf

func (entity *Dao) Printf(msg string, args ...interface{})

func (*Dao) SetDB

func (entity *Dao) SetDB(db *gorm.DB)

func (*Dao) SetTable

func (entity *Dao) SetTable(tableName string)

func (*Dao) Update

func (entity *Dao) Update(model interface{}, attrs interface{}, query interface{}, args ...interface{}) error

Update selected Fields, if attrs is an object, it will ignore default value field; if attrs is map, it will ignore unchanged field.

type Domain

type Domain struct {
	Flow
}

type Flow

type Flow struct {
	zap.SugaredLogger
	// contains filtered or unexported fields
}

func (*Flow) CountArgs

func (entity *Flow) CountArgs() int

func (*Flow) GetAllArgs

func (entity *Flow) GetAllArgs() []interface{}

func (*Flow) GetArgs

func (entity *Flow) GetArgs(idx int) interface{}

func (*Flow) GetContext

func (entity *Flow) GetContext() *gin.Context

func (*Flow) GetLog

func (entity *Flow) GetLog() *zap.SugaredLogger

func (*Flow) GetLogCtx

func (entity *Flow) GetLogCtx() *puzzle.LogCtx

func (*Flow) LogDebug

func (entity *Flow) LogDebug(args ...interface{})

提供给业务使用的server log 日志打印方法

func (*Flow) LogDebugf

func (entity *Flow) LogDebugf(format string, args ...interface{})

func (*Flow) LogError

func (entity *Flow) LogError(args ...interface{})

func (*Flow) LogErrorf

func (entity *Flow) LogErrorf(format string, args ...interface{})

func (*Flow) LogInfo

func (entity *Flow) LogInfo(args ...interface{})

func (*Flow) LogInfof

func (entity *Flow) LogInfof(format string, args ...interface{})

func (*Flow) LogWarn

func (entity *Flow) LogWarn(args ...interface{})

func (*Flow) LogWarnf

func (entity *Flow) LogWarnf(format string, args ...interface{})

func (*Flow) PreUse

func (entity *Flow) PreUse(args ...interface{})

参数为Use的可选参数部分

func (*Flow) SetArgs

func (entity *Flow) SetArgs(idx int, arg interface{}) *Flow

func (*Flow) SetContext

func (entity *Flow) SetContext(ctx *gin.Context) *Flow

func (*Flow) SetLog

func (entity *Flow) SetLog(log *zap.SugaredLogger) *Flow

func (*Flow) SetLogCtx

func (entity *Flow) SetLogCtx(logCtx *puzzle.LogCtx) *Flow

func (*Flow) Use

func (entity *Flow) Use(newFlow IFlow, args ...interface{}) interface{}

use 支持可选参数

type IApi

type IApi interface {
	IFlow
}

type IConsumer

type IConsumer interface {
	IFlow
	Run(args []string) error
	Setup(sarama.ConsumerGroupSession) error
	Cleanup(sarama.ConsumerGroupSession) error
	ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error
}

type IController

type IController interface {
	IFlow
	BindParam(req interface{}) bool
	Action()
	RenderJsonFail(err error)
	RenderJsonSucc(data interface{})
	RenderJsonAbort(err error)
	RenderHttpError(errNo int, errDetail ...interface{})
}

type IDao

type IDao interface {
	IFlow
	GetDB() *gorm.DB
	SetDB(db *gorm.DB)
	SetTable(tableName string)
	GetTable() string
}

type IDomain

type IDomain interface {
	IFlow
}

type IFlow

type IFlow interface {
	GetContext() *gin.Context
	SetContext(*gin.Context) *Flow
	GetArgs(idx int) interface{}
	SetArgs(idx int, arg interface{}) *Flow
	GetAllArgs() []interface{}
	Use(newFlow IFlow, args ...interface{}) interface{}
	PreUse(args ...interface{})
	GetLogCtx() *puzzle.LogCtx
	SetLogCtx(logCtx *puzzle.LogCtx) *Flow
	GetLog() *zap.SugaredLogger
	SetLog(log *zap.SugaredLogger) *Flow
}

type INMQConsumer

type INMQConsumer interface {
	IFlow
	Process() (interface{}, error)
}

type IService

type IService interface {
	IFlow
}

type ITask

type ITask interface {
	IFlow
	Run(args []string) error
}

type KFKConsumer

type KFKConsumer struct {
	Flow

	Ready  chan bool
	Topics []string
	// contains filtered or unexported fields
}

func (*KFKConsumer) Cleanup

func (entity *KFKConsumer) Cleanup(sarama.ConsumerGroupSession) error

func (*KFKConsumer) ConsumeClaim

func (entity *KFKConsumer) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error

func (*KFKConsumer) LogAndExit

func (entity *KFKConsumer) LogAndExit(format string, args ...interface{})

func (*KFKConsumer) LoopKafka

func (entity *KFKConsumer) LoopKafka(handler ConsumeHandler)

func (*KFKConsumer) Setup

type NmqConsumer

type NmqConsumer struct {
	Flow
}

type RetryPolicy

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

retry 策略

type Service

type Service struct {
	Flow
}

type Task

type Task struct {
	Flow
}

func (*Task) Run

func (entity *Task) Run(args []string) error

Jump to

Keyboard shortcuts

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