utils

package module
v0.0.0-...-a14c249 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2023 License: Apache-2.0 Imports: 48 Imported by: 0

README

go-utils

package main

import (
	"errors"
	"fmt"

	"github.com/lsq51201314/go-utils"
	"go.uber.org/zap"
)

func main() {
	//配置文件
	utils.YamlLoad(Configs)
	fmt.Println(Configs.App.Mode)
	fmt.Println(Configs.Log.Level)
	fmt.Println(Configs.MySQL.DbName)
	fmt.Println(Configs.Redis.Prefix)
	fmt.Println(Configs.Token.Issuer)
	//获取验证
	var vc utils.VerifyCode
	id, code, _, _ := vc.GetCode()
	fmt.Println(id, code)
	//压缩解压
	var zlib utils.Zlib
	c, _ := zlib.Compress([]byte("Hello World"))
	u, _ := zlib.UnCompress(c)
	fmt.Println(string(u))
	//随机索引
	var sfid utils.Snowflake
	sfid, _ = utils.NewSnowflake("2023-12-01", 1)
	fmt.Println(sfid.GetID())
	//redis
	var redis utils.Redis
	redis, _ = utils.NewRedis(utils.RedisOptions{
		Prefix:  Configs.Redis.Prefix,
		Host:    Configs.Redis.Host,
		Port:    Configs.Redis.Port,
		Passwd:  Configs.Redis.Passwd,
		MaxOpen: Configs.Redis.MaxOpen,
		MinIdle: Configs.Redis.MinIdle,
	})
	fmt.Println(redis.Lock("localhost"))
	defer redis.UnLock("localhost")
	//mysql
	var mysql utils.Mysql
	mysql, _ = utils.NewMysql(utils.MysqlOptions{
		Host:    Configs.MySQL.Host,
		User:    Configs.MySQL.User,
		Passwd:  Configs.MySQL.Passwd,
		DbName:  Configs.MySQL.DbName,
		Port:    Configs.MySQL.Port,
		MaxOpen: Configs.Redis.MaxOpen,
		MinIdle: Configs.Redis.MinIdle,
	})
	fmt.Println(mysql.DB.Migrator().HasTable("test"))
	//zap
	utils.ZapInit(utils.ZapOptions{
		Level:      Configs.Log.Level,
		FileName:   Configs.Log.FileName,
		MaxSize:    Configs.Log.MaxSize,
		MaxAge:     Configs.Log.MaxAge,
		MaxBackups: Configs.Log.MaxBackups,
	})
	zap.L().Error("test error")
	//http
	var http utils.Http
	http.Get("http://www.baidu.com")
	http.Post("http://www.baidu.com", nil)
	//const
	var constant utils.Const
	constant.AutoBuild(&ErrorCode)
	fmt.Println(ErrorCode.LoginChangeError)
	//aliyunoss
	var aliyunoss utils.AliyunOSS
	aliyunoss, _ = utils.NewAliyunOSS(utils.AliyunOSSOptions{
		AccessKeyId:     Configs.YunOSS.AccessKeyId,
		AccessKeySecret: Configs.YunOSS.AccessKeySecret,
		OssEndpoint:     Configs.YunOSS.OssEndpoint,
		OssBucketName:   Configs.YunOSS.OssBucketName,
	})
	name, _ := aliyunoss.Upload([]byte("hello world"))
	fmt.Println(name)
	ossbuf, _ := aliyunoss.Download("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed")
	fmt.Println(string(ossbuf))
	fmt.Println(aliyunoss.Delete("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"))
	//yunfile
	var yunfile utils.YunFile
	yunfile, _ = utils.NewYunFile()
	name, _ = yunfile.Upload([]byte("hello world"))
	fmt.Println(name)
	ossbuf, _ = yunfile.Download("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed")
	fmt.Println(string(ossbuf))
	fmt.Println(yunfile.Delete("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"))
	//jwt
	var jwt utils.Jwt
	jwt, _ = utils.NewJWT(utils.JwtOptions{
		Issuer: Configs.Token.Issuer,
		Passwd: Configs.Token.Passwd,
		Expire: Configs.Token.Expire,
	})
	str, _ := jwt.GetToken("hello world")
	fmt.Println(str)
	fmt.Println(jwt.ParseToken(str))
	//logger
	var logger utils.Logger
	logger.Info("Hello world")
	logger.Error("Hello world",errors.New("Hello world"))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GinCors

func GinCors() gin.HandlerFunc

跨域设置

func YamlLoad

func YamlLoad(config interface{}, file ...string) (err error)

载入配置

func ZapInit

func ZapInit(options ...ZapOptions) (err error)

初始化配置

Types

type Aes

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

加密实例

func NewAes

func NewAes(passwd string) *Aes

新建实例

func (*Aes) Dec

func (a *Aes) Dec(src []byte) (data []byte, err error)

解密数据

func (*Aes) Enc

func (a *Aes) Enc(src []byte) (data []byte, err error)

加密数据

type Cert

证书实例

func (*Cert) NewClient

func (c *Cert) NewClient(cafile, pemfiile, keyfile string) (err error)

客户证书

func (*Cert) NewServer

func (c *Cert) NewServer(cafile, pemfiile, keyfile string) (err error)

服务证书

type Const

type Const struct{}

常量实例

func (Const) AutoBuild

func (c Const) AutoBuild(dst ...interface{})

自动赋值

func (Const) Default

func (c Const) Default(t interface{})

默认值

func (Const) GetConfig

func (c Const) GetConfig(t interface{}, name string, id int) (config string)

获取配置

func (Const) GetOption

func (c Const) GetOption(t interface{}, id int) (data ConstOptions)

获取配置

func (Const) GetOptions

func (c Const) GetOptions(t interface{}) (data []ConstOptions)

获取配置

type ConstOptions

type ConstOptions struct {
	ID   int    `json:"id,string"`
	Name string `json:"name"`
	Text string `json:"text"`
}

常量配置

type GinBind

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

参数绑定

func NewGinBind

func NewGinBind() (g GinBind, err error)

新建实例

func (*GinBind) Get

func (g *GinBind) Get(c *gin.Context, params interface{}, data interface{}) (err error)

获取参数

type Http

type Http struct{}

http实例

func (Http) Get

func (h Http) Get(url string, timeout ...int) (code int, body []byte, err error)

get请求

func (Http) Post

func (h Http) Post(url string, data interface{}, timeout ...int) (code int, body []byte, err error)

post请求

type Image

type Image struct{}

图片实例

func (Image) Zoom

func (i Image) Zoom(src []byte, width, height uint) (data []byte, err error)

缩放图片

type Jwt

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

jwt实例

func NewJWT

func NewJWT(issuer string, options ...JwtOptions) (j Jwt, err error)

新建实例

func (*Jwt) GetToken

func (j *Jwt) GetToken(custom interface{}) (token string, err error)

生成凭证

func (*Jwt) ParseToken

func (j *Jwt) ParseToken(token string) (custom interface{}, err error)

解析凭证

type JwtOptions

type JwtOptions struct {
	Issuer string //""
	Passwd string //go-utils-jwt
	Expire int64  //7776000秒(90天)
}

配置信息

type Logger

type Logger struct{}

日志显示

func (Logger) Error

func (l Logger) Error(text string, err error)

错误显示

func (Logger) Info

func (l Logger) Info(text string)

信息显示

type Mysql

type Mysql struct {
	DB *gorm.DB
}

mysql实例

func NewMysql

func NewMysql(host, passwd, dbname string, options ...MysqlOptions) (m Mysql, err error)

新建实例

type MysqlOptions

type MysqlOptions struct {
	User    string //"root"
	Port    int32  //3306
	MaxOpen int    //100
	MinIdle int    //20
}

连接配置

type Redis

type Redis struct {
	DB *redis.Client
	// contains filtered or unexported fields
}

redis实例

func NewRedis

func NewRedis(host, passwd string, options ...RedisOptions) (r Redis, err error)

新建实例

func (*Redis) Lock

func (r *Redis) Lock(name string) (bool, error)

锁定

func (*Redis) UnLock

func (r *Redis) UnLock(name string) error

解锁

type RedisOptions

type RedisOptions struct {
	Prefix   string //""
	UserName string //""
	Port     int32  //6379
	DB       int    //0
	MaxOpen  int    //100
	MinIdle  int    //20
	LockTime int    //5秒
}

连接配置

type Snowflake

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

雪花实例

func NewSnowflake

func NewSnowflake(date string, id int64) (sfid Snowflake, err error)

新建实例

func (*Snowflake) GetID

func (s *Snowflake) GetID() int64

获取ID

type VerifyCode

type VerifyCode struct{}

数字验证

func (VerifyCode) GetCode

func (v VerifyCode) GetCode() (id, code, image string, err error)

获取验证码

func (VerifyCode) Verify

func (v VerifyCode) Verify(id, code string) (ok bool)

校验验证码

type YunFile

type YunFile struct {
	Path         string
	Size         int64
	LastModified string
}

文件信息

type YunOSS

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

存储实例

func NewYunOSS

func NewYunOSS(accessKeyId, accessKeySecret, ossBucketName string, ossEndpoint ...string) (yun YunOSS, err error)

新建实例

func (*YunOSS) Delete

func (a *YunOSS) Delete(path string) (err error)

删除文件

func (*YunOSS) Download

func (a *YunOSS) Download(path string) (data []byte, err error)

下载文件

func (*YunOSS) Query

func (a *YunOSS) Query(prefix string, size int, next ...string) (files []YunFile, token string, err error)

枚举文件

func (*YunOSS) Upload

func (a *YunOSS) Upload(path string, data []byte) (err error)

上传文件

type YunSMS

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

短信实例

func NewYunSMS

func NewYunSMS(accessKeyId, accessKeySecret string, smsEndpoint ...string) (sms YunSMS, err error)

新建实例

func (*YunSMS) Send

func (a *YunSMS) Send(signname, template, phoneNumber, jsonStr string) (err error)

发送短信

type ZapOptions

type ZapOptions struct {
	Level      string //debug
	FileName   string //./log/log.txt
	MaxSize    int    //100MB
	MaxAge     int    //90天
	MaxBackups int    //100个
}

日志配置

type Zlib

type Zlib struct{}

func (Zlib) Compress

func (z Zlib) Compress(src []byte) (data []byte, err error)

压缩数据

func (Zlib) UnCompress

func (z Zlib) UnCompress(src []byte) (data []byte, err error)

解压数据

Jump to

Keyboard shortcuts

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