lib

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2021 License: Apache-2.0 Imports: 39 Imported by: 10

Documentation

Index

Constants

View Source
const (
	DEFAULT_TTL    = 5 * time.Minute
	CLEAN_INTERVAL = 30 * time.Minute
)
View Source
const (
	LevelNone   = 0x00
	LevelError  = 0x01
	LevelWarn   = 0x02
	LevelAccess = 0x04
	LevelInfo   = 0x08
	LevelDebug  = 0x10
	LevelAll    = 0xFF
)

Log levels

View Source
const (
	CACHE_TYPE_LOCAL = iota
	CACHE_TYPE_REDIS
)
View Source
const (
	RAND_KIND_NUM   = 0 // 纯数字
	RAND_KIND_LOWER = 1 // 小写字母
	RAND_KIND_UPPER = 2 // 大写字母
	RAND_KIND_ALL   = 3 // 数字、大小写字母
)
View Source
const ACTION_SUFFIX = "Action"

controller中以此后缀结尾的方法会参与路由

View Source
const DEFAULT_ACTION = "Index"
View Source
const DEFAULT_CONTROLLER = "Index"

默认controller/action

Variables

View Source
var (
	//多语言时指定默认语言
	DEFAULT_LANG = "CN"
	//成功
	ERR_SUC = &Error{0, "OK"}

	//系统错误码
	ERR_SYSTEM         = &Error{10, "系统错误"}
	ERR_METHOD_INVALID = &Error{11, "请求不合法: %+v"}
	ERR_FREQ           = &Error{12, "接口访问过于频繁: %+v"}
	ERR_RPCAUTH        = &Error{13, "rpc认证失败:%+v"}
	ERR_OTHER          = &Error{14, "%+v"}
)
View Source
var Conf = &Config{}
View Source
var LocalCache = &localCache{}
View Source
var Logger = &FileLogger{}
View Source
var Mysql = &MysqlProxy{c: map[string]*MysqlClient{}}
View Source
var Redis = &RedisProxy{c: map[string]*RedisClient{}}
View Source
var TIME_ZONE = "Local" // Asia/Shanghai, UTC

在项目代码中指定时区

Functions

func AsBool

func AsBool(b interface{}, def ...bool) bool

强制转换为bool

func AsInt

func AsInt(num interface{}, def ...int) int

强制转换为int

func AsString

func AsString(str interface{}, def ...string) string

强制转换为string

func Concat

func Concat(str ...string) string

拼接字符串

func Cost

func Cost(start_time time.Time) int

从start_time开始的消耗时间, 单位毫秒

func Crc32

func Crc32(str string) int

func Date

func Date(times ...int) string

返回2013-01-20 格式的日期, 可以指定时间戳,默认当前时间

func DateTime

func DateTime(times ...int) string

返回2013-01-20 10:20:00 格式的时间, 可以指定时间戳,默认当前时间

func GetCookie

func GetCookie(r *http.Request, key string) string

func GetIp

func GetIp(r *http.Request) string

func GetLocalIp

func GetLocalIp() string

获取本机ip

func GetMapNode

func GetMapNode(m interface{}, keys ...string) (value interface{}, find bool)

获取MAP的某个节点

func InArray

func InArray(search interface{}, arr interface{}, stricts ...bool) bool

同php in_array

func Interceptor

func Interceptor(guard bool, errmsg *Error, fmts ...interface{})

捕获异常时,可同时返回data(通过fmts参数最后一个类型为map的值)

func Ip2long

func Ip2long(ipstr string) (ip int)

x.x.x.x格式的IP转换为数字

func Join

func Join(strs interface{}, seps ...string) string

[]int, []string, []interface, 连接成字符串

func JsonDecode

func JsonDecode(data string) interface{}

func JsonEncode

func JsonEncode(data interface{}) string

func Long2ip

func Long2ip(ip int) string

数字格式的IP转换为x.x.x.x

func MD5

func MD5(str string) string

func MkTime

func MkTime(t ...int) int

生成时间戳 参数:小时,分,秒,月,日,年

func Now

func Now() int

unix时间戳

func Rand

func Rand(size int, kind int) []byte

随机字符串

func RandStr

func RandStr(size int, kind ...int) string

func SetCookie

func SetCookie(rw http.ResponseWriter, key, val string, lifetime int, options ...interface{})

lifetime<0时删除cookie options: domain,secure,httponly,path

func Sha1

func Sha1(str string) string

func StrToTime

func StrToTime(datetime string) int

日期时间字符串转化为时间戳

func ToFloat

func ToFloat(num string, size ...int) float64

string转换为float64

func ToInt

func ToInt(num string) int

string转换为int

func ToIntArray

func ToIntArray(nums []string) []int

string的切片转换为int切片

func ToString

func ToString(num int) string

int转换为string

func ToStringArray

func ToStringArray(nums []int) []string

int的切片转换为string切片

func VersionCompare

func VersionCompare(ver, oldver string) bool

判断ver是否大于oldver

Types

type CliServer

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

func NewCliServer

func NewCliServer() *CliServer

func (*CliServer) AddController

func (this *CliServer) AddController(c interface{})

func (*CliServer) Run

func (this *CliServer) Run()

run with endless

type Config

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

func (*Config) Get

func (this *Config) Get(keys ...string) string

func (*Config) GetAll

func (this *Config) GetAll(section string) map[string]string

func (*Config) GetBool

func (this *Config) GetBool(keys ...string) bool

func (*Config) GetInt

func (this *Config) GetInt(keys ...string) int

func (*Config) GetInt64

func (this *Config) GetInt64(keys ...string) int64

func (*Config) GetJson

func (this *Config) GetJson(keys ...string) interface{}

func (*Config) GetJsonBool

func (this *Config) GetJsonBool(keys ...string) bool

func (*Config) GetJsonInt

func (this *Config) GetJsonInt(keys ...string) int

func (*Config) GetJsonMap

func (this *Config) GetJsonMap(keys ...string) map[string]string

func (*Config) GetJsonMapInt

func (this *Config) GetJsonMapInt(keys ...string) map[string]int

func (*Config) GetJsonSlice

func (this *Config) GetJsonSlice(keys ...string) []string

func (*Config) GetJsonSliceInt

func (this *Config) GetJsonSliceInt(keys ...string) []int

func (*Config) GetJsonSliceMap

func (this *Config) GetJsonSliceMap(keys ...string) []map[string]string

func (*Config) GetJsonString

func (this *Config) GetJsonString(keys ...string) string

func (*Config) GetSlice

func (this *Config) GetSlice(keys ...string) []string

func (*Config) GetSliceInt

func (this *Config) GetSliceInt(keys ...string) []int

func (*Config) Init

func (this *Config) Init(configFile string) error

func (*Config) Load

func (this *Config) Load(configFile string) error

type Error

type Error struct {
	Code int
	Msg  interface{}
}

func (*Error) Error

func (this *Error) Error() string

func (*Error) GetCode

func (this *Error) GetCode() int

func (*Error) GetMessage

func (this *Error) GetMessage() string

type Errorf

type Errorf struct {
	Code int
	Msg  interface{}
	// contains filtered or unexported fields
}

格式化输出错误信息 用于 Interceptor 拦截抛错 国际化产品,多语言时,Msg 可以设置为map[string]string ,如:{"CN":"系统错误", "EN":"system error"}

func (*Errorf) Error

func (this *Errorf) Error() string

func (*Errorf) GetCode

func (this *Errorf) GetCode() int

func (*Errorf) GetData

func (this *Errorf) GetData() map[string]interface{}

func (*Errorf) GetMessage

func (this *Errorf) GetMessage(langs ...string) string

type Executor

type Executor interface {
	QueryRow(query string, args ...interface{}) *sql.Row
	Exec(query string, args ...interface{}) (sql.Result, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
}

type FileLogger

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

func NewLogger

func NewLogger(logpath, name string, level int) *FileLogger

func (*FileLogger) Access

func (this *FileLogger) Access(v ...interface{})

func (*FileLogger) Debug

func (this *FileLogger) Debug(v ...interface{})

func (*FileLogger) Error

func (this *FileLogger) Error(v ...interface{})

func (*FileLogger) Info

func (this *FileLogger) Info(v ...interface{})

func (*FileLogger) Init

func (this *FileLogger) Init(logpath, logname string, loglevel int)

func (*FileLogger) Other

func (this *FileLogger) Other(logname string, v ...interface{})

func (*FileLogger) Warn

func (this *FileLogger) Warn(v ...interface{})

type HttpClient

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

func NewHttpClient

func NewHttpClient(timeouts ...int) *HttpClient

func (*HttpClient) Get

func (this *HttpClient) Get(requrl string, headers ...http.Header) (*httpResponse, error)

func (*HttpClient) Post

func (this *HttpClient) Post(requrl string, post_data interface{}, headers ...http.Header) (*httpResponse, error)

func (*HttpClient) Request

func (this *HttpClient) Request(method, requrl string, post_data interface{}, headers ...http.Header) (*httpResponse, error)

post_data 支持map[string]interface{} 和 io.Reader 两种参数类型

type HttpServer

type HttpServer struct {
	HttpAddr string
	HttpPort int
	Timeout  int
	// contains filtered or unexported fields
}

http服务监听,路由

func NewHttpServer

func NewHttpServer(addr string, port int, timout int, pprof bool) *HttpServer

func (*HttpServer) AddController

func (this *HttpServer) AddController(c interface{}, group ...string)

func (*HttpServer) Run

func (this *HttpServer) Run()
func (this *HttpServer) Run() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	addr := fmt.Sprintf("%s:%d", this.HttpAddr, this.HttpPort)
	s := &http.Server{
		Addr:         addr,
		Handler:      this.handler,
		ReadTimeout:  time.Duration(this.Timeout) * time.Millisecond,
		WriteTimeout: time.Duration(this.Timeout) * time.Millisecond,
	}
	log.Println("HttpServer Listen: ", addr)
	log.Println(s.ListenAndServe())
}

run with endless

type MAP

type MAP = map[string]interface{}

简化业务代码,直接使用MAP替代map[string]interface{}

type MysqlClient

type MysqlClient struct {
	ID           string
	Host         string
	User         string
	Password     string
	Database     string
	Charset      string
	MaxOpenConns int
	MaxIdleConns int
	Debug        bool
	// contains filtered or unexported fields
}

func (*MysqlClient) Begin

func (this *MysqlClient) Begin(is_readonly bool) *MysqlClient

func (*MysqlClient) Commit

func (this *MysqlClient) Commit()

func (*MysqlClient) Execute

func (this *MysqlClient) Execute(_sql string, val ...interface{}) int

Execute {{{

func (*MysqlClient) FuncParam

func (this *MysqlClient) FuncParam(param interface{}) string

拼装参数时,作为可执行字符,而不是字符串值

func (*MysqlClient) GetAll

func (this *MysqlClient) GetAll(_sql string, val ...interface{}) []map[string]interface{}

func (*MysqlClient) GetOne

func (this *MysqlClient) GetOne(_sql string, val ...interface{}) interface{}

GetOne {{{

func (*MysqlClient) GetRow

func (this *MysqlClient) GetRow(_sql string, val ...interface{}) map[string]interface{}

GetRow {{{

func (*MysqlClient) Init

func (this *MysqlClient) Init()

Init {{{

func (*MysqlClient) Insert

func (this *MysqlClient) Insert(table string, vals map[string]interface{}) int

Insert{{{

func (*MysqlClient) Replace

func (this *MysqlClient) Replace(table string, vals map[string]interface{}) int

Replace {{{

func (*MysqlClient) Rollback

func (this *MysqlClient) Rollback()

func (*MysqlClient) SetDebug

func (this *MysqlClient) SetDebug(open bool)

func (*MysqlClient) Update

func (this *MysqlClient) Update(table string, vals map[string]interface{}, where string, val ...interface{}) int

Update{{{

type MysqlExplain

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

func (*MysqlExplain) DrawConsole

func (this *MysqlExplain) DrawConsole()

type MysqlProxy

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

func (*MysqlProxy) Add

func (this *MysqlProxy) Add(conf_name string)

func (*MysqlProxy) Get

func (this *MysqlProxy) Get(conf_name string) *MysqlClient

type RedisClient

type RedisClient struct {
	Host     string
	Password string
	Timeout  int
	Poolsize int
	// contains filtered or unexported fields
}

func NewRedis

func NewRedis(conf_name string) (*RedisClient, error)

通过配置文件名字实例化

func NewRedisClient

func NewRedisClient(config map[string]string) (*RedisClient, error)

通过配置信息实例化

func (*RedisClient) Blpop

func (this *RedisClient) Blpop(key string, timeout int) (val []string, err error)

{{{ Blpop

func (*RedisClient) Brpop

func (this *RedisClient) Brpop(key string, timeout int) (val []string, err error)

{{{ Brpop

func (*RedisClient) Call

func (this *RedisClient) Call(cmd string, args ...interface{}) (resp *redis.Resp, err error)

__call {{{ Call

func (*RedisClient) Decr

func (this *RedisClient) Decr(key string) (val int, err error)

{{{ decr

func (*RedisClient) Decrby

func (this *RedisClient) Decrby(key string, increment int) (val int, err error)

{{{ decrby

func (*RedisClient) Del

func (this *RedisClient) Del(key string) (err error)

{{{ Del

func (*RedisClient) DelAll

func (this *RedisClient) DelAll(keys []string) (err error)

{{{ DelAll

func (*RedisClient) Exists

func (this *RedisClient) Exists(key string) (bool, error)

{{{ Exists

func (*RedisClient) Expire

func (this *RedisClient) Expire(key string, expire int) error

{{{ Expire

func (*RedisClient) ExpireAt

func (this *RedisClient) ExpireAt(key string, timestamp int)

{{{ ExpireAt

func (*RedisClient) Get

func (this *RedisClient) Get(key string) (val string, err error)

{{{ Get

func (*RedisClient) Hdel

func (this *RedisClient) Hdel(key, field interface{}) error

{{{ Hdel

func (*RedisClient) HdelAll

func (this *RedisClient) HdelAll(key string)

{{{ HdelAll

func (*RedisClient) Hexists

func (this *RedisClient) Hexists(key string) (bool, error)

{{{ Hexists

func (*RedisClient) Hget

func (this *RedisClient) Hget(key string, field interface{}) (val string, err error)

{{{ Hget

func (*RedisClient) HgetAll

func (this *RedisClient) HgetAll(key string) (val map[string]string, err error)

{{{ HgetAll

func (*RedisClient) Hincrby

func (this *RedisClient) Hincrby(key string, field interface{}, increment int) (val int, err error)

{{{ Hincrby

func (*RedisClient) HincrbyFloat

func (this *RedisClient) HincrbyFloat(key string, field interface{}, increment interface{}) (val float64, err error)

{{{ HincrbyFloat

func (*RedisClient) Hkeys

func (this *RedisClient) Hkeys(key string) (val []string, err error)

{{{ Hkeys

func (*RedisClient) Hmget

func (this *RedisClient) Hmget(key string, fields interface{}) (val []string, err error)

{{{ Hmget

func (*RedisClient) Hmset

func (this *RedisClient) Hmset(key string, val interface{}) (err error)

{{{ Hmset

func (*RedisClient) Hscan

func (this *RedisClient) Hscan(key string, cursor, pattern, count interface{}) (val []string, err error)

{{{ Hscan

func (*RedisClient) Hset

func (this *RedisClient) Hset(key string, field interface{}, val interface{}) error

hash {{{ Hset

func (*RedisClient) Hsetnx

func (this *RedisClient) Hsetnx(key string, field interface{}, val interface{}) (err error)

{{{ Hsetnx

func (*RedisClient) Incr

func (this *RedisClient) Incr(key string) (val int, err error)

{{{ incr

func (*RedisClient) Incrby

func (this *RedisClient) Incrby(key string, increment int) (val int, err error)

{{{ incrby

func (*RedisClient) IncrbyFloat

func (this *RedisClient) IncrbyFloat(key string, increment interface{}) (val float64, err error)

{{{ incrbyfloat

func (*RedisClient) Init

func (this *RedisClient) Init() error

{{{ Init

func (*RedisClient) Keys

func (this *RedisClient) Keys(key string) (val []string, err error)

{{{ Keys

func (*RedisClient) Llen

func (this *RedisClient) Llen(key string) (val int, err error)

{{{ Llen

func (*RedisClient) Lpop

func (this *RedisClient) Lpop(key string) (val string, err error)

{{{ Lpop

func (*RedisClient) Lpush

func (this *RedisClient) Lpush(key string, val interface{}) error

{{{ Lpush

func (*RedisClient) Lrange

func (this *RedisClient) Lrange(key string, start, stop int) (val []string, err error)

{{{ Lrange key start stop

func (*RedisClient) Mget

func (this *RedisClient) Mget(keys []string) (val []string, err error)

{{{ Mget

func (*RedisClient) Rpop

func (this *RedisClient) Rpop(key string) (val string, err error)

{{{ Rpop

func (*RedisClient) Rpush

func (this *RedisClient) Rpush(key string, val interface{}) error

list {{{ Rpush

func (*RedisClient) Sadd

func (this *RedisClient) Sadd(key string, val interface{}) error

sets {{{ Sadd

func (*RedisClient) Scan

func (this *RedisClient) Scan(cursor, pattern, count string) (val []string, err error)

{{{ Scan

func (*RedisClient) Scard

func (this *RedisClient) Scard(key string) (val int, err error)

{{{ Scard

func (*RedisClient) Set

func (this *RedisClient) Set(key string, val interface{}) error

{{{ Set

func (*RedisClient) Setex

func (this *RedisClient) Setex(key string, secs int, val interface{}) error

{{{ Setex

func (*RedisClient) SisMember

func (this *RedisClient) SisMember(key string, member interface{}) (bool, error)

{{{ SisMember

func (*RedisClient) Smembers

func (this *RedisClient) Smembers(key string) (val []string, err error)

{{{ Smembers

func (*RedisClient) Spop

func (this *RedisClient) Spop(key, count int) (val []string, err error)

{{{ Spop

func (*RedisClient) SrandMember

func (this *RedisClient) SrandMember(key, count int) (val []string, err error)

{{{ SrandMember

func (*RedisClient) Srem

func (this *RedisClient) Srem(key string, member interface{}) (val int, err error)

{{{ Srem

func (*RedisClient) Sscan

func (this *RedisClient) Sscan(key string, cursor, pattern, count interface{}) (val []string, err error)

{{{ Sscan

func (*RedisClient) Ttl

func (this *RedisClient) Ttl(key string) (int, error)

{{{ Ttl

func (*RedisClient) Zadd

func (this *RedisClient) Zadd(key string, score int, val interface{}) error

zset {{{ Zadd

func (*RedisClient) Zcard

func (this *RedisClient) Zcard(key string) (val int, err error)

{{{ Zcard

func (*RedisClient) Zincrby

func (this *RedisClient) Zincrby(key string, increment int, val interface{}) error

{{{ Zincrby

func (*RedisClient) Zrange

func (this *RedisClient) Zrange(key string, start, stop int, withscores bool) (val []string, err error)

{{{ Zrange key start stop [WITHSCORES]

func (*RedisClient) ZrangeByScore

func (this *RedisClient) ZrangeByScore(key string, min, max, start, step int, withscores bool) (val []string, err error)

{{{ ZrangeByScore key min max [WITHSCORES] [LIMIT offset count] 返回有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )的成员。按 score 值递增(从小到大)次序排列。

func (*RedisClient) ZrangeBytes

func (this *RedisClient) ZrangeBytes(key string, start, stop int, withscores bool) (val [][]byte, err error)

{{{ ZrangeBytes key start stop [WITHSCORES]

func (*RedisClient) Zrank

func (this *RedisClient) Zrank(key string, member interface{}) (val int, err error)

{{{ Zrank

func (*RedisClient) Zrem

func (this *RedisClient) Zrem(key string, member interface{}) (val int, err error)

{{{ Zrem

func (*RedisClient) ZremrangeByScore

func (this *RedisClient) ZremrangeByScore(key string, min, max int) (err error)

{{{ ZremrangeByScore

func (*RedisClient) Zrevrange

func (this *RedisClient) Zrevrange(key string, start, stop int, withscores bool) (val []string, err error)

{{{ Zrevrange key start stop [WITHSCORES]

func (*RedisClient) ZrevrangeByScore

func (this *RedisClient) ZrevrangeByScore(key string, start, step int, withscores bool) (val []string, err error)

{{{ ZrevrangeByScore key max min [WITHSCORES] [LIMIT offset count]

func (*RedisClient) ZrevrangeBytes

func (this *RedisClient) ZrevrangeBytes(key string, start, stop int, withscores bool) (val [][]byte, err error)

{{{ ZrevrangeBytes key start stop [WITHSCORES]

func (*RedisClient) Zrevrank

func (this *RedisClient) Zrevrank(key string, member interface{}) (val int, err error)

{{{ Zrevrank

func (*RedisClient) Zscore

func (this *RedisClient) Zscore(key string, member interface{}) (val int, err error)

{{{ Zscore key member

type RedisProxy

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

type Restrict

type Restrict struct {
	Rule      string
	Freq      int //频率
	Interval  int //周期
	Whitelist []string
	Blacklist []string
	CacheType int
	RedisConf string
}

func NewRestrict

func NewRestrict(rule string, freq, interval int) *Restrict

func (*Restrict) Add

func (this *Restrict) Add(uniqid string) bool

func (*Restrict) AddBlacklist

func (this *Restrict) AddBlacklist(uniqid []string)

func (*Restrict) AddWhitelist

func (this *Restrict) AddWhitelist(uniqid []string)

func (*Restrict) Check

func (this *Restrict) Check(uniqid string, update bool) bool

*

  • 封禁检查
  • @param $uniqid 封禁检查值
  • @param bool $update 是否检查同时更新次数值,默认只检查是否触犯封禁次数
  • @return bool true 达到封禁次数 false 未达到封禁次数

func (*Restrict) Delete

func (this *Restrict) Delete(uniqid string)

func (*Restrict) Surplus

func (this *Restrict) Surplus(uniqid string) int

func (*Restrict) UseRedis

func (this *Restrict) UseRedis(conf_name string)

type RpcServer

type RpcServer struct {
	Port    int
	Timeout int
	// contains filtered or unexported fields
}

http服务监听,路由

func NewRpcServer

func NewRpcServer(port int, timout int, pprof bool) *RpcServer

func (*RpcServer) AddController

func (this *RpcServer) AddController(c interface{})

func (*RpcServer) Run

func (this *RpcServer) Run()
func (this *RpcServer) Run() {
	//runtime.GOMAXPROCS(runtime.NumCPU())
	lis, err := net.Listen("tcp", fmt.Sprintf(":%d", this.Port))

	if err != nil {
		panic(err)
	}

	s := grpc.NewServer()
	pb.RegisterYGOServiceServer(s, this.hanlder)
	fmt.Println("RpcServer Listen: ", this.Port)
	s.Serve(lis)
}

run with endless

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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