utils

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2018 License: Apache-2.0 Imports: 25 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 (
	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

Functions

func EndlessServe

func EndlessServe(bind string, handler http.Handler)

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 InitLogrus

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

InitLogrus set logrus 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 MockRedis

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

MockRedis provide a mock redis server for testing

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