components

package
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2023 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// OK 成功返回
	OK = codes.OK

	// CodeCanceled 表示操作已取消(通常由调用者取消)
	CodeCanceled = codes.Canceled

	// CodeUnknown 未知错误
	CodeUnknown = codes.Unknown

	// CodeInvalidArgument 客户端传递了一个无效的参数
	CodeInvalidArgument = codes.InvalidArgument

	// CodeDeadlineExceeded 表示操作在完成前过期。对于改变系统状态的操作,即使操作成功完成,也可能会返回此错误。
	// 例如,来自服务器的成功响应可能已延迟足够长的时间以使截止日期到期
	CodeDeadlineExceeded = codes.DeadlineExceeded

	// CodeNotFound 表示未找到某些请求的实体(例如文件或目录)
	CodeNotFound = codes.NotFound

	// CodeAlreadyExists 表示创建实体的尝试失败,因为实体已经存在
	CodeAlreadyExists = codes.AlreadyExists

	// CodePermissionDenied 表示调用者没有执行指定操作的权限
	CodePermissionDenied = codes.PermissionDenied

	// CodeResourceExhausted 表示某些资源已用尽,可能是每个用户的配额,或者可能是整个文件系统空间不足
	CodeResourceExhausted = codes.ResourceExhausted

	// CodeFailedPrecondition 操作被拒绝,因为系统未处于操作执行所需的状态。
	// 例如,要删除的目录可能是非空的,rmdir 操作应用于非目录等
	CodeFailedPrecondition = codes.FailedPrecondition

	// CodeAborted 操作已取消,通常是由于并发问题,如排序器检查失败、事务中止
	CodeAborted = codes.Aborted

	// CodeOutRange 超出范围的操作
	CodeOutRange = codes.OutOfRange

	// CodeUnimplemented 未实现或不支持的操作
	CodeUnimplemented = codes.Unimplemented

	// CodeInternal 系统内部错误
	CodeInternal = codes.Internal

	// CodeUnavailable 表示该服务当前不可用。这很可能是一种瞬态情况,可以通过回退重试来纠正。
	// 请注意,重试非幂等操作是不安全的
	CodeUnavailable = codes.Unavailable

	// CodeDataLoss 表示不可恢复的数据丢失或损坏
	CodeDataLoss = codes.DataLoss

	// CodeUnauthenticated 表示请求没有用于操作的有效身份验证凭据
	CodeUnauthenticated = codes.Unauthenticated
)
View Source
const (
	EnvPro  = `pro`
	EnvDev  = `dev`
	EnvTest = `test`
)
View Source
const (
	ModeWait    = 1
	ModeMaxTime = 2
	ModeError   = 3
)
View Source
const (
	ZhCn = `zh_CN`
)

Variables

View Source
var (
	ErrInvalidPaddingChar = errors.New(`invalid padding char`)
	ErrAesDecrypt         = errors.New(`aes decrypt error`)
)
View Source
var (
	ErrDataEmpty          = errors.New("data is empty")
	ErrDataFormat         = errors.New("invalid data format")
	ErrOutOfRange         = errors.New("out of range")
	ErrAlphanumeric       = errors.New("alphanumeric must be [a-zA-Z0-9] and not repeat")
	ErrAlphanumericLength = errors.New("alphanumeric length must be [50, 62]")
	ErrTimeBack           = errors.New("time go back")
	ErrMachineId          = errors.New("illegal machine id")
)
View Source
var (
	ErrMsgMapEmpty = errors.New("msgMap can not empty")
	ErrNoYamlFiles = errors.New("there are no `.yml` or `.yaml` files in this directory")
	ErrNoJsonFiles = errors.New("there are no `.json` files in this directory")
)
View Source
var ErrNoServer = errors.New("no server")

Functions

func Code2Id

func Code2Id(code []byte) (id int64, err error)

func Code6

func Code6(id int64) (code []byte, err error)

func Code6String

func Code6String(id int64) (code string, err error)

func Code6To8

func Code6To8(code6 []byte) (code8 []byte, err error)

func Code8

func Code8(id int64) (code []byte, err error)

func Code8String

func Code8String(id int64) (code string, err error)

func CodeString2Id

func CodeString2Id(code string) (id int64, err error)

func CodeString6To8

func CodeString6To8(code6 string) (code8 string, err error)

func CreatePkcs1Keys

func CreatePkcs1Keys(keyLength int) (privateKey, publicKey string)

CreatePkcs1Keys 生成pkcs1格式公钥私钥

func CreatePkcs8Keys

func CreatePkcs8Keys(keyLength int) (privateKey, publicKey string)

CreatePkcs8Keys 生成pkcs8格式公钥私钥

func MarshalPkcs8PrivateKey

func MarshalPkcs8PrivateKey(key *rsa.PrivateKey) []byte

func Max6

func Max6() int64

func Max8

func Max8() int64

Types

type Aes

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

Aes Aes加密

func NewAes

func NewAes(key, iv string) (a *Aes, err error)

NewAes 实例化Aes

func NewAesWithBytes

func NewAesWithBytes(key, iv []byte) (a *Aes, err error)

NewAesWithBytes 实例化Aes

func (*Aes) CbcDecrypt

func (a *Aes) CbcDecrypt(secretData []byte) (data []byte, err error)

CbcDecrypt cbc解密

func (*Aes) CbcEncrypt

func (a *Aes) CbcEncrypt(plain []byte) (secretData []byte)

CbcEncrypt cbc加密

type Config

type Config struct {
	Name      string        `json:"name" yaml:"name"`
	Addr      string        `json:"addr" yaml:"addr"`
	PprofAddr string        `json:"pprofAddr" yaml:"pprofAddr"`
	Env       string        `json:"env" yaml:"env"`
	Ver       string        `json:"ver" yaml:"ver"`
	Logger    logger.Option `json:"logger" yaml:"logger"`
	Params    JsonParam     `json:"params" yaml:"params"`
}

func (*Config) IsEnv

func (c *Config) IsEnv(env string) bool

type Context

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

Context 上下文

func AcquireCtx

func AcquireCtx(handlers []Handler) *Context

AcquireCtx 申请Context

func (*Context) Abort

func (c *Context) Abort()

func (*Context) Close

func (c *Context) Close()

Close 释放Context

func (*Context) Deadline

func (c *Context) Deadline() (deadline time.Time, ok bool)

func (*Context) Done

func (c *Context) Done() <-chan struct{}

func (*Context) Err

func (c *Context) Err() error

func (*Context) Get

func (c *Context) Get(key string) (value any, exists bool)

Get 获取数据

func (*Context) GetBool

func (c *Context) GetBool(key string) (value bool, exists bool)

GetBool 获取bool值

func (*Context) GetInt

func (c *Context) GetInt(key string) (value int, exists bool)

GetInt 获取int值

func (*Context) GetInt64

func (c *Context) GetInt64(key string) (value int64, exists bool)

GetInt64 获取int64值

func (*Context) GetInt8

func (c *Context) GetInt8(key string) (value int8, exists bool)

GetInt8 获取bool值

func (*Context) GetString

func (c *Context) GetString(key string) (value string, exists bool)

GetString 获取string值

func (*Context) IsAborted

func (c *Context) IsAborted() bool

func (*Context) Next

func (c *Context) Next()

func (*Context) Set

func (c *Context) Set(key string, value any)

Set 存储数据

func (*Context) Value

func (c *Context) Value(key any) any

type GetMachineId

type GetMachineId func() (id int64, err error)

GetMachineId 获取机器Id

func GetMachineIdByEnv

func GetMachineIdByEnv(key string) GetMachineId

GetMachineIdByEnv 根据环境变量获取机器Id

func GetMachineIdByIp

func GetMachineIdByIp() GetMachineId

GetMachineIdByIp 根据Ip获取机器Id

type Handler

type Handler func(ctx *Context)

type HashRing

type HashRing interface {
	// Store 存储servers
	Store(servers ...kind.CanHash)
	// Get 获取server
	Get(key any) (server kind.CanHash, err error)
	// Index 根据index获取server
	Index(index int) (server kind.CanHash, err error)
	// Add 添加server
	Add(server kind.CanHash)
	// Remove 移除server
	Remove(server kind.CanHash)
	// Length 获取servers长度
	Length() int
	// Range 遍历servers
	Range(handler func(index int, server kind.CanHash, hitCount uint64) (handled bool))
}

HashRing Hash环

func NewHashRing

func NewHashRing(servers ...kind.CanHash) HashRing

type I18n

type I18n interface {
	// T 翻译
	T(key, language string) (msg string)
}

I18n 多语言接口

func NewI18n

func NewI18n(defaultLanguage string, msgMap map[string]map[string]string) (i I18n, err error)

NewI18n 实例化多语言

func NewI18nFromJson

func NewI18nFromJson(defaultLanguage string, dir string) (i I18n, err error)

NewI18nFromJson 从Json文件配置实例化多语言

func NewI18nFromYaml

func NewI18nFromYaml(defaultLanguage string, dir string) (i I18n, err error)

NewI18nFromYaml 从Yaml文件配置实例化多语言

type IdCode

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

func NewIdCode

func NewIdCode(alphanumericSet []byte, salt6, salt8 int64) (*IdCode, error)

func (*IdCode) Code2Id

func (ic *IdCode) Code2Id(code []byte) (id int64, err error)

func (*IdCode) Code6

func (ic *IdCode) Code6(id int64) (code []byte, err error)

func (*IdCode) Code6String

func (ic *IdCode) Code6String(id int64) (code string, err error)

func (*IdCode) Code6To8

func (ic *IdCode) Code6To8(code6 []byte) (code8 []byte, err error)

func (*IdCode) Code8

func (ic *IdCode) Code8(id int64) (code []byte, err error)

func (*IdCode) Code8String

func (ic *IdCode) Code8String(id int64) (code string, err error)

func (*IdCode) CodeString2Id

func (ic *IdCode) CodeString2Id(code string) (id int64, err error)

func (*IdCode) CodeString6To8

func (ic *IdCode) CodeString6To8(code6 string) (code8 string, err error)

func (*IdCode) Max6

func (ic *IdCode) Max6() int64

func (*IdCode) Max8

func (ic *IdCode) Max8() int64

type JsonParam

type JsonParam map[string]any

func UnmarshalJsonParam

func UnmarshalJsonParam(data []byte) (JsonParam, error)

UnmarshalJsonParam _

func (JsonParam) Exists

func (jp JsonParam) Exists(key string) bool

Exists key是否存在,返回bool值

func (JsonParam) Float64

func (jp JsonParam) Float64(key string) float64

Float64 获取float64,如果不是float64,返回0

func (JsonParam) Int

func (jp JsonParam) Int(key string) int

Int 获取Int,如果不是int,返回0

func (JsonParam) Int64

func (jp JsonParam) Int64(key string) int64

Int64 获取Int64,如果不是float64,返回0

func (JsonParam) Int64Slice

func (jp JsonParam) Int64Slice(key string) []int64

Int64Slice 获取[]int64,获取失败返回nil

func (JsonParam) IntSlice

func (jp JsonParam) IntSlice(key string) []int

IntSlice 获取[]int,获取失败,返回nil

func (JsonParam) JsonMarshal

func (jp JsonParam) JsonMarshal() []byte

JsonMarshal _

func (JsonParam) String

func (jp JsonParam) String(key string) string

String 获取string值,如果不是string类型,返回""

func (JsonParam) StringSlice

func (jp JsonParam) StringSlice(key string) []string

StringSlice 获取[]string,如果不是[]string返回nil

func (JsonParam) Uint32Slice

func (jp JsonParam) Uint32Slice(key string) []uint32

Uint32Slice 获取[]uint32,获取失败返回nil

type Mysql

type Mysql struct {
	*sql.DB
	Options MysqlOption
}

func NewMysql

func NewMysql(opt MysqlOption) (mysql *Mysql, err error)

type MysqlOption

type MysqlOption struct {
	DbName                string `json:"dbName" yaml:"dbName"`
	Host                  string `json:"host" yaml:"host"`
	Port                  uint32 `json:"port" yaml:"port"`
	UserName              string `json:"userName" yaml:"userName"`
	Password              string `json:"password" yaml:"password"`
	CharSet               string `json:"charSet" yaml:"charSet"`
	MaxIdleConns          int    `json:"maxIdleConns" yaml:"maxIdleConns"`
	MaxOpenConns          int    `json:"maxOpenConns" yaml:"maxOpenConns"`
	ConnMaxIdleTimeSecond int64  `json:"connMaxIdleTimeSecond" yaml:"connMaxIdleTimeSecond"`
	ConnMaxLifetimeSecond int64  `json:"connMaxLifetimeSecond" yaml:"connMaxLifetimeSecond"`
}

func DefaultMysqlOption

func DefaultMysqlOption() MysqlOption

func MysqlOptionWithJson

func MysqlOptionWithJson(conf string) (opt MysqlOption, err error)

func MysqlOptionWithYaml

func MysqlOptionWithYaml(conf string) (opt MysqlOption, err error)

func (*MysqlOption) Dsn

func (mo *MysqlOption) Dsn() string

type Node

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

type NodeList

type NodeList []Node

func (NodeList) Len

func (n NodeList) Len() int

func (NodeList) Less

func (n NodeList) Less(i, j int) bool

func (NodeList) Swap

func (n NodeList) Swap(i, j int)

type Package

type Package struct {
	Id    uint16    `json:"id"`
	Name  string    `json:"name"`
	Param JsonParam `json:"param"`
}

func (*Package) Pack

func (p *Package) Pack() []byte

func (*Package) Unpack

func (p *Package) Unpack(data []byte) error

type Rsa

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

Rsa Rsa

func NewRsa

func NewRsa(publicKey, privateKey string) (r *Rsa, err error)

NewRsa 实例化Rsa

func NewRsaBytes

func NewRsaBytes(public, private []byte) (r *Rsa, err error)

NewRsaBytes 实例化Rsa

func NewRsaWithPkcs1

func NewRsaWithPkcs1(publicKey, privateKey string) (r *Rsa, err error)

NewRsaWithPkcs1 pkcs1实例化Rsa

func NewRsaWithPkcs1Bytes

func NewRsaWithPkcs1Bytes(public, private []byte) (r *Rsa, err error)

NewRsaWithPkcs1Bytes pkc1实例化Rsa

func NewRsaWithPkcs8

func NewRsaWithPkcs8(publicKey, privateKey string) (r *Rsa, err error)

NewRsaWithPkcs8 pkcs8实例化Rsa

func NewRsaWithPkcs8Bytes

func NewRsaWithPkcs8Bytes(public, private []byte) (r *Rsa, err error)

func (*Rsa) Decrypt

func (r *Rsa) Decrypt(secretData []byte) ([]byte, error)

Decrypt 解密

func (*Rsa) Encrypt

func (r *Rsa) Encrypt(data []byte) ([]byte, error)

Encrypt 加密

func (*Rsa) Sign

func (r *Rsa) Sign(data []byte, algorithmSign crypto.Hash) ([]byte, error)

Sign 数据签名

func (*Rsa) Verify

func (r *Rsa) Verify(data []byte, sign []byte, algorithmSign crypto.Hash) bool

Verify 数据验签

type SnowFlake

type SnowFlake interface {
	// Id 生成id
	Id() (int64, error)
	// Info 根据id获取信息
	Info(id int64) (timestamp int64, machineId uint8, index int16)
}

SnowFlake 雪花算法接口,1位0,41位毫秒时间戳,8位机器码,14位递增值

func NewSF

func NewSF(mode uint8, id uint8, beginSeconds int64) (sfl SnowFlake, err error)

NewSF 实例化雪花算法

func NewSFByIp

func NewSFByIp(mode uint8, beginSeconds int64) (sfl SnowFlake, err error)

NewSFByIp ip方式实例化雪花算法

func NewSFByMachineFunc

func NewSFByMachineFunc(mode uint8, machindFunc GetMachineId, beginSeconds int64) (sfl SnowFlake, err error)

NewSFByMachineFunc GetMachineId方式实例化雪花算法

type Status

type Status struct {
	Code codes.Code `json:"code"`
	Msg  string     `json:"msg"`
	Flag uint8      `json:"flag"`
	Data any        `json:"data"`
}

func StatusOk

func StatusOk(data any) *Status

StatusOk 创建携带数据的Status

func StatusWithCode

func StatusWithCode(code codes.Code) *Status

StatusWithCode 指定Code获取一个Status

func StatusWithCodeMsg

func StatusWithCodeMsg(code codes.Code, msg string) *Status

StatusWithCodeMsg 实例化Status

func StatusWithJsonUnmarshal

func StatusWithJsonUnmarshal(data []byte) (*Status, error)

StatusWithJsonUnmarshal 指定json []byte获取一个Status

func (*Status) Close

func (s *Status) Close()

Close 释放Status到sync.Pool

func (*Status) ConvertGrpcStatus

func (s *Status) ConvertGrpcStatus(details ...proto.Message) (*status.Status, error)

ConvertGrpcStatus 转换为grpc状态码

func (*Status) Error

func (s *Status) Error() string

Error 转换为error

func (*Status) IsCode

func (s *Status) IsCode(code codes.Code) bool

IsCode 判断是否是指定Code

func (*Status) IsOK

func (s *Status) IsOK() bool

IsOK 操作是否OK

func (*Status) JsonMarshal

func (s *Status) JsonMarshal() []byte

JsonMarshal _

func (*Status) ToError

func (s *Status) ToError() error

func (*Status) WithData

func (s *Status) WithData(data any) *Status

WithData 附加自定义Data

func (*Status) WithFlag

func (s *Status) WithFlag(flag uint8) *Status

WithFlag 附加flag

func (*Status) WithMsg

func (s *Status) WithMsg(msg string) *Status

WithMsg 附加自定义message

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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