Documentation ¶
Overview ¶
Package utils save the package of third party package tools and the general tool code written by yourself write your general tool in the package
Index ¶
- Constants
- Variables
- func Assert(tb testing.TB, condition bool, msg string, v ...interface{})
- func CtxLogger(c *gin.Context) *logrus.Entry
- func EndlessServe(bind string, handler http.Handler)
- func Equals(tb testing.TB, exp, act interface{})
- func GetLocalIP() (string, error)
- func InitGormDB(engine, addr, name, username, password string, ...) error
- func InitLogger(output io.Writer, logLevel string, logFormatter string)
- func InitRedis(mode string, addr string, password string, db int, master string) error
- func InitRedisClient(addr string, password string, db int) error
- func InitRedisCluster(addrs []string, password string) error
- func InitRedisSentinel(master string, addrs []string, password string, db int) error
- func InitViper(configName string, envPrefix string, options []ViperOption) error
- func MockHTTPServer(f http.HandlerFunc) *httptest.Server
- func MockRedis() (*miniredis.Miniredis, error)
- func OK(tb testing.TB, err error)
- func StructToURLValues(i interface{}) (values url.Values)
- func TestingGETRequest(r http.Handler, path string) *httptest.ResponseRecorder
- func TestingPOSTRequest(r http.Handler, path string, jsonStr string) *httptest.ResponseRecorder
- type GormLogger
- type JSONTime
- type Pagination
- type ViperOption
Constants ¶
const ( RedisSingleInstanceMode = "single-instance" RedisSentinelMode = "sentinel" RedisClusterMode = "cluster" RedisAddrsSeparator = "," )
redis connection modes
const CtxLoggerKey = "clogger"
CtxLoggerKey define the logger keyname which in context
const (
TimeFormat = "2006-01-02 15:04:05"
)
TimeFormat define the json time filed format
Variables ¶
var ( // Redis single instance and sentinel client Redis *redis.Client // RedisCluster redis cluster client RedisCluster *redis.ClusterClient )
redis clients
var DB *gorm.DB
DB is *gorm.DB, use it to do orm
var GoRequest = gorequest.New()
GoRequest is a http client
var Logger *logrus.Entry
Logger logrus global logger, with PID field
Functions ¶
func EndlessServe ¶
EndlessServe running a graceful (re)start server
func InitGormDB ¶
func InitGormDB(engine, addr, name, username, password string, maxIdleConns, maxOpenConns, connMaxLifeMinutes int, logMode bool) error
InitGormDB init the grom DB engine is database type, such as mysql, sqlite3, etc. addr is database's address name is database dbname usename is database username password is dabase password maxIdleConns sets the maximum number of connections in the idle connection pool maxOpenConns sets the maximum number of open connections to the database. connMaxLifeMinutes sets the maximum amount of time(minutes) a connection may be reused logMode show detailed log
func InitLogger ¶ added in v1.1.0
InitLogger set logrus logger options logLevel set level which will be logged, values: debug, info(default), warning, error, fatal, panic logFormatter set log format, values: text(default), json
func InitRedis ¶
InitRedis init redis client by different mode `mode` is the redis running mode, single-instance, sentinel or cluster single-instance and sentinel mode init the global var which named `Redis`, cluster mode init the gloabel var which named `RedisCluster` `addr` is redis address. when mode is sentinel or cluster, the addr is a mutilple address separated by comman `password` is redis auth password `db` is redis db number, cluster mode don't use it `master` is redis sentinel master name, only need to be set on sentinel mode, others dont't use it
func InitRedisClient ¶
InitRedisClient init a single instance redis client named `Redis`
func InitRedisCluster ¶
InitRedisCluster init redis cluster client named `RedisCluster`
func InitRedisSentinel ¶
InitRedisSentinel init redis sentinel client also named `Redis`
func InitViper ¶
func InitViper(configName string, envPrefix string, options []ViperOption) error
InitViper init viper by default value, ENV, cmd flag and config file you can use switch to reload server when config file changed
func MockHTTPServer ¶ added in v1.1.0
func MockHTTPServer(f http.HandlerFunc) *httptest.Server
MockHTTPServer provide a mock http server for testing Usage: https://gist.github.com/axiaoxin/aa8014738c6a02ce4e66eb01168d24fe
func StructToURLValues ¶ added in v1.1.0
StructToURLValues 将结构体转换为url.Values,key为json tag,没有json tag则使用字段名称,传入指针参数
func TestingGETRequest ¶
func TestingGETRequest(r http.Handler, path string) *httptest.ResponseRecorder
TestingGETRequest perform a GET request with the handler for testing
func TestingPOSTRequest ¶
TestingPOSTRequest perform a POST request with the handler for testing
Types ¶
type JSONTime ¶
JSONTime custom format for json time field
func (JSONTime) MarshalJSON ¶
MarshalJSON on JSONTime format Time field with %Y-%m-%d %H:%M:%S
func (*JSONTime) UnmarshalJSON ¶
UnmarshalJSON on JSONTime format Time field with %Y-%m-%d %H:%M:%S
type Pagination ¶
type Pagination struct { // 数据总数 ItemsCount int `json:"itemsCount"` // 分页总数 PagesCount int `json:"pagesCount"` // 当前页码 PageNum int `json:"pageNum"` // 分页大小 PageSize int `json:"pageSize"` // 是否有上一页 HasPrev bool `json:"hasPrev"` // 是否有下一页 HasNext bool `json:"hasNext"` // 上一页页码 PrevPageNum int `json:"prevPageNum"` // 下一页页码 NextPageNum int `json:"nextPageNum"` }
Pagination Paginate return it
type ViperOption ¶
ViperOption the InitViper option type