components

package
v3.0.11 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2025 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ZhCn = `zh_CN`
	En   = `en`
)
View Source
const (
	ModeWait    = 1
	ModeMaxTime = 2
	ModeError   = 3
)

Variables

View Source
var (
	ErrInvalidPaddingChar = errors.New(`invalid padding char`)
	ErrAesDecrypt         = errors.New(`aes decrypt error`)
)
View Source
var (
	DefaultBase62Charset = []byte("M01EK4aFzPcHNwZeUgAfXi3SjTkYmnGpqrJsuR8VohW2LvQ9xy7bB5CtD6dI")
	DefaultBase62, _     = NewBase62(DefaultBase62Charset)
)
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 (
	DefaultIdCode, _ = NewIdCode(DefaultBase62, defaultSalt)
)
View Source
var (
	DefaultLang = ZhCn
)
View Source
var (
	DefaultSF, _ = NewSFByIp(ModeWait, defaultSFBegin.Unix())
)
View Source
var ErrNoServer = errors.New("no server")

Functions

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

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 Base62 added in v3.0.8

type Base62 interface {
	Encode(value uint64, length int) []byte
	Decode(data []byte) (uint64, error)
}

func NewBase62 added in v3.0.8

func NewBase62(charset []byte) (Base62, error)

type Chain

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

func NewChain

func NewChain(handlers ...Handler) *Chain

func (*Chain) Run

func (c *Chain) Run()

func (*Chain) RunWithCtx

func (c *Chain) RunWithCtx(ctx *Context)

func (*Chain) Use

func (c *Chain) Use(handlers ...Handler)

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) Event

func (c *Context) Event() *Event

func (*Context) Get

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

Get 获取数据

func (*Context) GetBool

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

GetBool 获取bool值

func (*Context) GetInt

func (c *Context) GetInt(key string, defaultVal int64) (value int64)

GetInt 获取int64值

func (*Context) GetString

func (c *Context) GetString(key, defaultVal string) (value string)

GetString 获取string值

func (*Context) GetUint

func (c *Context) GetUint(key string, defaultVal uint64) (value uint64)

GetUint 获取uint64值

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) SetEvent

func (c *Context) SetEvent(event *Event)

func (*Context) Value

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

type Event

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

func NewEvent added in v3.0.10

func NewEvent(name string, data any) *Event

func (*Event) Data

func (e *Event) Data() any

func (*Event) Name

func (e *Event) Name() string

func (*Event) ParamData added in v3.0.10

func (e *Event) ParamData() kind.JsonParam

type EventManager

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

func (*EventManager) Has

func (em *EventManager) Has(name string) bool

func (*EventManager) On

func (em *EventManager) On(name string, handlers ...Handler)

func (*EventManager) Trigger

func (em *EventManager) Trigger(name string, data any)

func (*EventManager) TriggerWithCtx added in v3.0.9

func (em *EventManager) TriggerWithCtx(name string, ctx *Context)

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(key string) (msg string)
	Tl(key, language string) (msg string)
}

I18n 多语言接口

func NewI18n

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

NewI18n 实例化多语言

func NewI18nFromJson

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

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

func NewI18nFromYaml

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

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

type IdCode

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

func NewIdCode

func NewIdCode(base62 Base62, salt int64) (*IdCode, error)

func NewIdCodeWithCharset added in v3.0.8

func NewIdCodeWithCharset(charset []byte, salt int64) (*IdCode, error)

func (*IdCode) Code2Id

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

func (*IdCode) CodeString2Id

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

func (*IdCode) Id2Code32 added in v3.0.8

func (ic *IdCode) Id2Code32(id uint32) (code []byte, err error)

func (*IdCode) Id2Code64 added in v3.0.8

func (ic *IdCode) Id2Code64(id uint64) (code []byte, err error)

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 kind.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) DecryptOAEP added in v3.0.7

func (r *Rsa) DecryptOAEP(secretData []byte, hash hash.Hash, label []byte) ([]byte, error)

DecryptOAEP 解密

func (*Rsa) Encrypt

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

Encrypt 加密

func (*Rsa) EncryptOAEP added in v3.0.7

func (r *Rsa) EncryptOAEP(data []byte, hash hash.Hash, label []byte) ([]byte, error)

EncryptOAEP 加密

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)
	// TimeBegin 时间戳起点
	TimeBegin() time.Time
}

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方式实例化雪花算法

Jump to

Keyboard shortcuts

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