utils

package
v0.0.6-alpha Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2023 License: BSD-2-Clause Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ERRCODE_CUSTOM              = 40001
	ERRCODE_SERVER_ERROR        = 40002
	ERRCODE_DATABASE_ERROR      = 40003
	ERRCODE_REQUEST_PARAM_ERROR = 40004
	ERRCODE_NOT_LOGINED         = 40005
	ERRCODE_USER_NOT_EXIST      = 40006
	ERRCODE_INVALID_SESSION     = 40101
	ERRCODE_INVALID_Id          = 40102
	ERRCODE_INVALID_TOKEN       = 40103
	SUCCESS_CODE_CUSTOM         = 2000
)
View Source
const (
	ERRMSG_CUSTOM              = "custom error"
	ERRMSG_SERVER_ERROR        = "server error"
	ERRMSG_DATABASE_ERROR      = "database error"
	ERRMSG_PARAM_ERROR         = "param error"
	ERRMSG_REQUEST_PARAM_ERROR = "request param error"
	ERRMSG_NOT_LOGINED         = "not logined"
	ERRMSG_USER_NOT_EXIST      = "user not exist"
	ERRMSG_INVALID_SESSION     = "invalid session"
	ERRMSG_INVALID_Id          = "invalid id"
	ERRMSG_INVALID_TOKEN       = "invalid token"
	SUCCESS_MSG_CUSTOM         = "success"
)
View Source
const (
	TOKEN_IN_HEADER         = "X-Auth-Token"
	USER_TOKEN_REDIS_PREFIX = "USER:TOKEN:"
	USER_SID_REDIS_PREFIX   = "USER:SID:"
	TOKEN_EXPIRE            = 1 * time.Hour
	REFRESH_EXPIRE          = 24 * TOKEN_EXPIRE
)
View Source
const (
	EXTRA_INFO = "extra-info"
	LOGINED    = "logined"
)
View Source
const (
	SESSION_COOKIE_NAME = "SID"
	CLIENT_KEY          = "client"
	UID_KEY             = "UID"
)
View Source
const (
	TOKEN_TYPE_ACCESS  = "access_token"
	TOKEN_TYPE_REFRESH = "refresh_token"
)
View Source
const (
	DEFAULT_SIZE = 10
)

Variables

This section is empty.

Functions

func AddExtraInfoToContext

func AddExtraInfoToContext(c *gin.Context, key string, value interface{})

func Atoi

func Atoi(s string, default_value int) int

func ErrorHandler

func ErrorHandler() gin.HandlerFunc

func GenerateAad

func GenerateAad() []byte

GenerateAad AAD

func GenerateID

func GenerateID() string

GenerateID GenerateID

func GenerateIv

func GenerateIv() []byte

GenerateIv IV

func GenerateMsgID

func GenerateMsgID(address string, length ...int) string

GenerateMsgID Create Message ID

func GenerateSalt

func GenerateSalt() []byte

GenerateSalt Salt

func GenerateToken

func GenerateToken(val string) string

GenerateToken File Token

func GenerateUUID

func GenerateUUID() string

func GetUser

func GetUser(verifyKey crypto.PublicKey) gin.HandlerFunc

func HmacSha1

func HmacSha1(plainText string) string

HmacSha1 Hmac Sha1

func HmacSha1WithKey

func HmacSha1WithKey(plainText []byte, key string) string

HmacSha1WithKey Hmac sha1 with key

func HmacSha1WithKeyByte

func HmacSha1WithKeyByte(plainText []byte, key string) []byte

HmacSha1WithKey Hmac sha1 with key

func HmacSha256

func HmacSha256(pubKey, plaintex []byte) string

HmacSha256 Hmac Sha256

func HmacSha512

func HmacSha512(plaintex string) string

HmacSha512 Hmac Sha512

func HmacSha512Rand

func HmacSha512Rand(category string) string

HmacSha512Rand hmac sha512

func InitLogger

func InitLogger(logConfig *config.LogConfig)

func Int64ToString

func Int64ToString(d int64) string

Int64ToString Int64 To String

func LoadEdPrivateKeyFromDisk

func LoadEdPrivateKeyFromDisk(location string) crypto.PrivateKey

func LoadEdPublicKeyFromDisk

func LoadEdPublicKeyFromDisk(location string) crypto.PublicKey

func OptionsHeader

func OptionsHeader() gin.HandlerFunc

func OutHttpJson

func OutHttpJson(c *gin.Context, code int, message string, data interface{}, statusCode ...int)

func RandBytes

func RandBytes(len int) ([]byte, error)

RandBytes RandBytes

func RandInt64

func RandInt64(min, max int64) int64

RandInt64 获得一个随机数 min 最小值 max 最大值

func RandStr

func RandStr(strlen int) string

func ResponseErrorJson

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

func ResponseFailedJson

func ResponseFailedJson(c *gin.Context, code int, message string, data interface{}, statusCode ...int)

func ResponseSuccessJson

func ResponseSuccessJson(c *gin.Context, data interface{}, statusCode ...int)

func ResponseSuccessJsonWithPagination

func ResponseSuccessJsonWithPagination(c *gin.Context, data interface{}, p *Pagination, statusCode ...int)

func ResponseText

func ResponseText(c *gin.Context, text string, statusCode ...int)

func Sha1Digest

func Sha1Digest(plaintext string) (result string)

Sha1Digest Sha1Digest

func Sha1Hex

func Sha1Hex(plaintext string) (result string)

func Sha256

func Sha256(plaintext []byte) []byte

func Sha256Digest

func Sha256Digest(plaintext string) (result string)

Sha256Digest Sha256Digest

func Sha256Hex

func Sha256Hex(plaintext string) (result string)

func Sha256HexBytes

func Sha256HexBytes(plaintext string) []byte

func StringToHex

func StringToHex(str string) ([]byte, error)

StringToHex HexString to bytes

func TimestampString

func TimestampString() string

TimestampString timestamp

func TrailClient

func TrailClient(domain string, maxAge int) gin.HandlerFunc

func UserAuthen

func UserAuthen(verifyKey crypto.PublicKey) gin.HandlerFunc

func VerifyRefleshToken

func VerifyRefleshToken(t string, verifyKey *crypto.PublicKey) (*jwt.Token, error)

Types

type AccessClaims

type AccessClaims struct {
	UID       int64
	TokenType string
	jwt.RegisteredClaims
}

type ClientInfo

type ClientInfo struct {
	Ip        string
	Platform  string
	Os        string
	UserAgent string
	SessionId string
	UserId    int64
	RequestId string
}

func GetClientInfo

func GetClientInfo(c *gin.Context) (*ClientInfo, error)

func (*ClientInfo) LogFormatLong

func (c *ClientInfo) LogFormatLong() string

func (*ClientInfo) LogFormatShort

func (c *ClientInfo) LogFormatShort() string

func (*ClientInfo) LogFormatShortAndIp

func (c *ClientInfo) LogFormatShortAndIp() string

type Pagination

type Pagination struct {
	Size  int    `json:"size"`
	Page  int    `json:"page"`
	Count int    `json:"count"`
	Sort  string `json:"sort"`
}

func GeneratePaginationFromRequest

func GeneratePaginationFromRequest(c *gin.Context, default_size int) *Pagination

type RefreshClaims

type RefreshClaims struct {
	UID       int64
	TokenType string
	Salt      int64
	jwt.RegisteredClaims
}

type Response

type Response struct {
	Code    int
	Message string
	Data    interface{}
}

Jump to

Keyboard shortcuts

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