utils

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2019 License: Apache-2.0 Imports: 33 Imported by: 0

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

View Source
const (
	RedisSingleInstanceMode = "single-instance"
	RedisSentinelMode       = "sentinel"
	RedisClusterMode        = "cluster"
	RedisAddrsSeparator     = ","
)

redis connection modes

View Source
const CtxLoggerKey = "clogger"

CtxLoggerKey define the logger keyname which in context

View Source
const (
	TimeFormat = "2006-01-02 15:04:05"
)

TimeFormat define the json time filed format

Variables

View Source
var (
	// Redis single instance and sentinel client
	Redis *redis.Client
	// RedisCluster redis cluster client
	RedisCluster *redis.ClusterClient
)

redis clients

View Source
var DB *gorm.DB

DB is *gorm.DB, use it to do orm

View Source
var GoRequest = gorequest.New()

GoRequest is a http client

View Source
var Logger *logrus.Entry

Logger logrus global logger, with PID field

Functions

func Assert added in v1.1.0

func Assert(tb testing.TB, condition bool, msg string, v ...interface{})

Assert fails the test if the condition is false.

func CtxLogger added in v1.1.0

func CtxLogger(c *gin.Context) *logrus.Entry

CtxLogger return a logger with requestid

func EndlessServe

func EndlessServe(bind string, handler http.Handler)

EndlessServe running a graceful (re)start server

func Equals added in v1.1.0

func Equals(tb testing.TB, exp, act interface{})

Equals fails the test if exp is not equal to act.

func GetLocalIP added in v1.1.0

func GetLocalIP() (string, error)

GetLocalIP 获取当前IP

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

func InitLogger(output io.Writer, logLevel string, logFormatter string)

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

func InitRedis(mode string, addr string, password string, db int, master string) error

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

func InitRedisClient(addr string, password string, db int) error

InitRedisClient init a single instance redis client named `Redis`

func InitRedisCluster

func InitRedisCluster(addrs []string, password string) error

InitRedisCluster init redis cluster client named `RedisCluster`

func InitRedisSentinel

func InitRedisSentinel(master string, addrs []string, password string, db int) error

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 MockRedis

func MockRedis() (*miniredis.Miniredis, error)

MockRedis provide a mock redis server for testing

func OK added in v1.1.0

func OK(tb testing.TB, err error)

OK fails the test if an err is not nil.

func StructToURLValues added in v1.1.0

func StructToURLValues(i interface{}) (values url.Values)

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

func TestingPOSTRequest(r http.Handler, path string, jsonStr string) *httptest.ResponseRecorder

TestingPOSTRequest perform a POST request with the handler for testing

Types

type GormLogger

type GormLogger struct{}

GormLogger custom gorm logger

func (*GormLogger) Print

func (*GormLogger) Print(values ...interface{})

Print define how to log

type JSONTime

type JSONTime struct {
	time.Time
}

JSONTime custom format for json time field

func (JSONTime) MarshalJSON

func (t JSONTime) MarshalJSON() ([]byte, error)

MarshalJSON on JSONTime format Time field with %Y-%m-%d %H:%M:%S

func (*JSONTime) Scan

func (t *JSONTime) Scan(v interface{}) error

Scan valueof time.Time

func (JSONTime) String

func (t JSONTime) String() string

String return %Y-%m-%d %H:%M:%S

func (*JSONTime) UnmarshalJSON

func (t *JSONTime) UnmarshalJSON(data []byte) error

UnmarshalJSON on JSONTime format Time field with %Y-%m-%d %H:%M:%S

func (JSONTime) Value

func (t JSONTime) Value() (driver.Value, error)

Value insert timestamp into mysql need this function.

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

func Paginate

func Paginate(itemsCount, pageNum, pageSize int) Pagination

Paginate 计算分页信息

type ViperOption

type ViperOption struct {
	Name    string
	Default interface{}
	Desc    string
}

ViperOption the InitViper option type

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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