util

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2021 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Epoch is set to the twitter snowflake epoch of Nov 04 2010 01:42:54 UTC in milliseconds
	// You may customize this to set a different epoch for your application.
	Epoch int64 = 1288834974657

	// NodeBits holds the number of bits to use for Node
	// Remember, you have a total 22 bits to share between Node/Step
	NodeBits uint8 = 10

	// StepBits holds the number of bits to use for Step
	// Remember, you have a total 22 bits to share between Node/Step
	StepBits uint8 = 12
)
View Source
var ErrInvalidBase32 = errors.New("invalid base32")

ErrInvalidBase32 is returned by ParseBase32 when given an invalid []byte

View Source
var ErrInvalidBase58 = errors.New("invalid base58")

ErrInvalidBase58 is returned by ParseBase58 when given an invalid []byte

Functions

func CheckNotExist

func CheckNotExist(src string) bool

CheckNotExist check if the file exists

func CheckPermission

func CheckPermission(src string) bool

CheckPermission check if the file has permission

func Contains

func Contains(obj interface{}, target interface{}) (bool, error)

func Downloader

func Downloader(c *gin.Context, fileName, filePath string)

func GenerateToken

func GenerateToken(username, accessToken string, expireTime int64) (string, error)

GenerateToken generate tokens used for auth

func GetSSLCrtContent

func GetSSLCrtContent() string

func GetSSLKeyContent

func GetSSLKeyContent() string

func GetWeekendInYear

func GetWeekendInYear(timestamp int64) int

func IsNotExistMkDir

func IsNotExistMkDir(src string) error

IsNotExistMkDir create a directory if it does not exist

func Md5

func Md5(value string) string

func MustOpen

func MustOpen(fileName, filePath string) (*os.File, error)

MustOpen maximize trying to open the file

func Open

func Open(name string, flag int, perm os.FileMode) (*os.File, error)

Open a file according to a specific mode

func PageInfo

func PageInfo(c *gin.Context, total int64) (page int, limit int)

PageInfo TODO: size range check

func ParseDuration

func ParseDuration(d string) time.Duration

func ParseStrTimeCondition

func ParseStrTimeCondition(timeCondition *TimeCondition, timeField string, where map[string]interface{}) map[string]interface{}

func PasswordHash

func PasswordHash(pwd []byte) string

func PasswordVerify

func PasswordVerify(hashedPwd string, plainPwd string) bool

func RespFailed

func RespFailed(c *gin.Context, code ErrCodeStringify, data interface{})

RespFailed 失败返回

func RespSuccess

func RespSuccess(c *gin.Context, data interface{})

RespSuccess 成功返回

func RespValidateFailed

func RespValidateFailed(c *gin.Context, err error)

RespValidateFailed 失败返回

func RespWithError

func RespWithError(c *gin.Context, code ErrCodeStringify, err error, data interface{})

func SetSize

func SetSize(s int)

func SetUp

func SetUp(keyPath, certPath string, pageSize int)

func SetUpSSL

func SetUpSSL(privateKey, publicKey string)

func StrPad

func StrPad(str string, placeholder string, overallLen int) string

func TrimHandlerName

func TrimHandlerName(handler string) string

func ValidatorAddon

func ValidatorAddon()

Types

type Claims

type Claims struct {
	Username    string `json:"username"`
	AccessToken string `json:"access_token"`
	jwt.StandardClaims
}

func ParseToken

func ParseToken(token string) (*Claims, error)

ParseToken parsing token

type ErrCode

type ErrCode int
const (
	Success                        ErrCode = 200   // 成功
	InvalidParams                  ErrCode = 400   // 请求参数错误
	Error                          ErrCode = 500   // 失败
	InvalidAccess                  ErrCode = 401   // 访问失效
	NotAuthorize                   ErrCode = 402   // 接口未授权
	ErrorNotExist                  ErrCode = 600   // 所操作的资源不存在
	ErrorExist                     ErrCode = 610   // 所操作的资源已存在
	ErrorOperationAddFailure       ErrCode = 620   // 添加操作失败
	ErrorOperationUpdateFailure    ErrCode = 621   // 更新操作失败
	ErrorOperationDeleteFailure    ErrCode = 622   // 删除操作失败
	ErrorOperationQueryListFailure ErrCode = 623   // 获取列表失败
	ErrorMakeDirFailure            ErrCode = 625   // 创建目录失败
	ErrorUserNameExist             ErrCode = 10000 // 用户名已存在
	ErrorAuthCheckTokenFail        ErrCode = 20001 // Token鉴权失败
	ErrorAuthCheckTokenExpired     ErrCode = 20002 // Token已过期
	ErrorAuthToken                 ErrCode = 20003 // Token生成失败
	ErrorAuth                      ErrCode = 20004 // Token错误
	ErrorAuthCheckTokenRevoked     ErrCode = 20005 // Token已失效

)

func (ErrCode) String

func (i ErrCode) String() string

type ErrCodeStringify

type ErrCodeStringify interface {
	String() string
}

type HttpRespFailed

type HttpRespFailed struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

type ID

type ID int64

An ID is a custom type used for a snowflake ID. This is used so we can attach methods onto the ID.

func ParseBase2

func ParseBase2(id string) (ID, error)

ParseBase2 converts a Base2 string into a snowflake ID

func ParseBase32

func ParseBase32(b []byte) (ID, error)

ParseBase32 parses a base32 []byte into a snowflake ID NOTE: There are many different base32 implementations so becareful when doing any interoperation.

func ParseBase36

func ParseBase36(id string) (ID, error)

ParseBase36 converts a Base36 string into a snowflake ID

func ParseBase58

func ParseBase58(b []byte) (ID, error)

ParseBase58 parses a base58 []byte into a snowflake ID

func ParseBase64

func ParseBase64(id string) (ID, error)

ParseBase64 converts a base64 string into a snowflake ID

func ParseBytes

func ParseBytes(id []byte) (ID, error)

ParseBytes converts a byte slice into a snowflake ID

func ParseInt64

func ParseInt64(id int64) ID

ParseInt64 converts an int64 into a snowflake ID

func ParseIntBytes

func ParseIntBytes(id [8]byte) ID

ParseIntBytes converts an array of bytes encoded as big endian integer as a snowflake ID

func ParseString

func ParseString(id string) (ID, error)

ParseString converts a string into a snowflake ID

func (ID) Base2

func (f ID) Base2() string

Base2 returns a string base2 of the snowflake ID

func (ID) Base32

func (f ID) Base32() string

Base32 uses the z-base-32 character set but encodes and decodes similar to base58, allowing it to create an even smaller result string. NOTE: There are many different base32 implementations so becareful when doing any interoperation.

func (ID) Base36

func (f ID) Base36() string

Base36 returns a base36 string of the snowflake ID

func (ID) Base58

func (f ID) Base58() string

Base58 returns a base58 string of the snowflake ID

func (ID) Base64

func (f ID) Base64() string

Base64 returns a base64 string of the snowflake ID

func (ID) Bytes

func (f ID) Bytes() []byte

Bytes returns a byte slice of the snowflake ID

func (ID) Int64

func (f ID) Int64() int64

Int64 returns an int64 of the snowflake ID

func (ID) IntBytes

func (f ID) IntBytes() [8]byte

IntBytes returns an array of bytes of the snowflake ID, encoded as a big endian integer.

func (ID) MarshalJSON

func (f ID) MarshalJSON() ([]byte, error)

MarshalJSON returns a json byte array string of the snowflake ID.

func (ID) Node

func (f ID) Node() int64

Node returns an int64 of the snowflake ID node number DEPRECATED: the below function will be removed in a future release.

func (ID) Step

func (f ID) Step() int64

Step returns an int64 of the snowflake step (or sequence) number DEPRECATED: the below function will be removed in a future release.

func (ID) String

func (f ID) String() string

String returns a string of the snowflake ID

func (ID) Time

func (f ID) Time() int64

Time returns an int64 unix timestamp in milliseconds of the snowflake ID time DEPRECATED: the below function will be removed in a future release.

func (*ID) UnmarshalJSON

func (f *ID) UnmarshalJSON(b []byte) error

UnmarshalJSON converts a json byte array of a snowflake ID into an ID type.

type JSONSyntaxError

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

A JSONSyntaxError is returned from UnmarshalJSON if an invalid ID is provided.

func (JSONSyntaxError) Error

func (j JSONSyntaxError) Error() string

type Node

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

A Node struct holds the basic information needed for a snowflake generator node

func NewNode

func NewNode(node int64) (*Node, error)

NewNode returns a new snowflake node that can be used to generate snowflake IDs

func (*Node) Generate

func (n *Node) Generate() ID

Generate creates and returns a unique snowflake ID To help guarantee uniqueness - Make sure your system is keeping accurate system time - Make sure you never have multiple nodes running with the same node ID

type TimeCondition

type TimeCondition struct {
	StartTime int `json:"start_time" form:"start_time"`
	EndTime   int `json:"end_time" form:"end_time"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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