encapsulate Redis base function

This commit is contained in:
landaiqing
2024-08-06 13:17:03 +08:00
parent d0496b34f0
commit 86b907d4d0
15 changed files with 493 additions and 23 deletions

View File

@@ -2,13 +2,13 @@ package api
import ( import (
"schisandra-cloud-album/api/auth_api" "schisandra-cloud-album/api/auth_api"
//"schisandra-cloud-album/api/captcha_api" "schisandra-cloud-album/api/captcha_api"
) )
// Apis 统一导出的api // Apis 统一导出的api
type Apis struct { type Apis struct {
AuthApi auth_api.AuthAPI AuthApi auth_api.AuthAPI
//CaptchaAPI captcha_api.CaptchaAPI CaptchaAPI captcha_api.CaptchaAPI
} }
// Api new函数实例化实例化完成后会返回结构体地指针类型 // Api new函数实例化实例化完成后会返回结构体地指针类型

View File

@@ -1,3 +1,3 @@
package main package captcha_api
type CaptchaAPI struct{} type CaptchaAPI struct{}

View File

@@ -1,22 +1,16 @@
package main package captcha_api
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"github.com/wenlng/go-captcha/v2/base/option" "github.com/wenlng/go-captcha/v2/base/option"
"github.com/wenlng/go-captcha/v2/click" "log"
"schisandra-cloud-album/global"
) )
var textCapt click.Captcha // GenerateTextCaptcha 生成文本验证码
func GenerateTextCaptcha() {
func init() { captData, err := global.TextCaptcha.Generate()
}
func main() {
captData, err := textCapt.Generate()
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }

391
common/redis/redis.go Normal file
View File

@@ -0,0 +1,391 @@
package redis
import (
"context"
"github.com/redis/go-redis/v9"
"schisandra-cloud-album/global"
"time"
)
var (
ctx = context.Background()
)
// Set 给数据库中名称为key的string赋予值value,并设置失效时间0为永久有效
func Set(key string, value interface{}, expiration time.Duration) *redis.StatusCmd {
return global.REDIS.Set(ctx, key, value, expiration)
}
// Get 查询数据库中名称为key的value值
func Get(key string) *redis.StringCmd {
return global.REDIS.Get(ctx, key)
}
// GetSet 设置一个key的值并返回这个key的旧值
func GetSet(key string, value interface{}) *redis.StringCmd {
return global.REDIS.GetSet(ctx, key, value)
}
// SetNX 如果key不存在则设置这个key的值,并设置key的失效时间。如果key存在则设置不生效
func SetNX(key string, value interface{}, expiration time.Duration) *redis.BoolCmd {
return global.REDIS.SetNX(ctx, key, value, expiration)
}
// MGet 批量查询key的值。比如redisDb.MGet("name1","name2","name3")
func MGet(keys ...string) *redis.SliceCmd {
return global.REDIS.MGet(ctx, keys...)
}
// MSet 批量设置key的值。redisDb.MSet("key1", "value1", "key2", "value2", "key3", "value3")
func MSet(pairs ...interface{}) *redis.StatusCmd {
return global.REDIS.MSet(ctx, pairs...)
}
// Incr Incr函数每次加一,key对应的值必须是整数或nil 否则会报错incr key1: ERR value is not an integer or out of range
func Incr(key string) *redis.IntCmd {
return global.REDIS.Incr(ctx, key)
}
// IncrBy IncrBy函数,可以指定每次递增多少,key对应的值必须是整数或nil
func IncrBy(key string, value int64) *redis.IntCmd {
return global.REDIS.IncrBy(ctx, key, value)
}
// IncrByFloat IncrByFloat函数,可以指定每次递增多少跟IncrBy的区别是累加的是浮点数
func IncrByFloat(key string, value float64) *redis.FloatCmd {
return global.REDIS.IncrByFloat(ctx, key, value)
}
// Decr Decr函数每次减一,key对应的值必须是整数或nil.否则会报错
func Decr(key string) *redis.IntCmd {
return global.REDIS.Decr(ctx, key)
}
// DecrBy 可以指定每次递减多少,key对应的值必须是整数或nil
func DecrBy(key string, decrement int64) *redis.IntCmd {
return global.REDIS.DecrBy(ctx, key, decrement)
}
// Del 删除key操作,支持批量删除 redisDb.Del("key1","key2","key3")
func Del(keys ...string) *redis.IntCmd {
return global.REDIS.Del(ctx, keys...)
}
// Expire 设置key的过期时间,单位秒
func Expire(key string, expiration time.Duration) *redis.BoolCmd {
return global.REDIS.Expire(ctx, key, expiration)
}
// Append 给数据库中名称为key的string值追加value
func Append(key, value string) *redis.IntCmd {
return global.REDIS.Append(ctx, key, value)
}
/*
* List操作
*/
// LPush 从列表左边插入数据,list不存在则新建一个继续插入数据
func LPush(key string, values ...interface{}) *redis.IntCmd {
return global.REDIS.LPush(ctx, key, values...)
}
// LPushX 跟LPush的区别是仅当列表存在的时候才插入数据
func LPushX(key string, value interface{}) *redis.IntCmd {
return global.REDIS.LPushX(ctx, key, value)
}
// LRange 返回名称为 key 的 list 中 start 至 end 之间的元素 返回从0开始到-1位置之间的数据意思就是返回全部数据
func LRange(key string, start, stop int64) *redis.StringSliceCmd {
return global.REDIS.LRange(ctx, key, start, stop)
}
// LLen 返回列表的长度大小
func LLen(key string) *redis.IntCmd {
return global.REDIS.LLen(ctx, key)
}
// LTrim 截取名称为key的list的数据list的数据为截取后的值
func LTrim(key string, start, stop int64) *redis.StatusCmd {
return global.REDIS.LTrim(ctx, key, start, stop)
}
// LIndex 根据索引坐标,查询列表中的数据
func LIndex(key string, index int64) *redis.StringCmd {
return global.REDIS.LIndex(ctx, key, index)
}
// LSet 给名称为key的list中index位置的元素赋值
func LSet(key string, index int64, value interface{}) *redis.StatusCmd {
return global.REDIS.LSet(ctx, key, index, value)
}
// LInsert 在指定位置插入数据。op为"after或者before"
func LInsert(key, op string, pivot, value interface{}) *redis.IntCmd {
return global.REDIS.LInsert(ctx, key, op, pivot, value)
}
// LInsertBefore 在指定位置前面插入数据
func LInsertBefore(key string, op string, pivot, value interface{}) *redis.IntCmd {
return global.REDIS.LInsert(ctx, key, op, pivot, value)
}
// LInsertAfter 在指定位置后面插入数据
func LInsertAfter(key string, op string, pivot, value interface{}) *redis.IntCmd {
return global.REDIS.LInsert(ctx, key, op, pivot, value)
}
// LPop 从列表左边删除第一个数据,并返回删除的数据
func LPop(key string) *redis.StringCmd {
return global.REDIS.LPop(ctx, key)
}
// LRem 删除列表中的数据。删除count个key的list中值为value 的元素。
func LRem(key string, count int64, value interface{}) *redis.IntCmd {
return global.REDIS.LRem(ctx, key, count, value)
}
/**
* 集合set操作
*/
// SAdd 向名称为key的set中添加元素member
func SAdd(key string, members ...interface{}) *redis.IntCmd {
return global.REDIS.SAdd(ctx, key, members...)
}
// SCard 获取集合set元素个数
func SCard(key string) *redis.IntCmd {
return global.REDIS.SCard(ctx, key)
}
// SIsMember 判断元素member是否在集合set中
func SIsMember(key string, member interface{}) *redis.BoolCmd {
return global.REDIS.SIsMember(ctx, key, member)
}
// SMembers 返回名称为 key 的 set 的所有元素
func SMembers(key string) *redis.StringSliceCmd {
return global.REDIS.SMembers(ctx, key)
}
// SDiff 求差集
func SDiff(keys ...string) *redis.StringSliceCmd {
return global.REDIS.SDiff(ctx, keys...)
}
// SDiffStore 求差集并将差集保存到 destination 的集合
func SDiffStore(destination string, keys ...string) *redis.IntCmd {
return global.REDIS.SDiffStore(ctx, destination, keys...)
}
// SInter 求交集
func SInter(keys ...string) *redis.StringSliceCmd {
return global.REDIS.SInter(ctx, keys...)
}
// SInterStore 求交集并将交集保存到 destination 的集合
func SInterStore(destination string, keys ...string) *redis.IntCmd {
return global.REDIS.SInterStore(ctx, destination, keys...)
}
// SUnion 求并集
func SUnion(keys ...string) *redis.StringSliceCmd {
return global.REDIS.SUnion(ctx, keys...)
}
// SUnionStore 求并集并将并集保存到 destination 的集合
func SUnionStore(destination string, keys ...string) *redis.IntCmd {
return global.REDIS.SUnionStore(ctx, destination, keys...)
}
// SPop 随机返回集合中的一个元素,并且删除这个元素
func SPop(key string) *redis.StringCmd {
return global.REDIS.SPop(ctx, key)
}
// SPopN 随机返回集合中的count个元素并且删除这些元素
func SPopN(key string, count int64) *redis.StringSliceCmd {
return global.REDIS.SPopN(ctx, key, count)
}
// SRem 删除名称为 key 的 set 中的元素 member,并返回删除的元素个数
func SRem(key string, members ...interface{}) *redis.IntCmd {
return global.REDIS.SRem(ctx, key, members...)
}
// SRandMember 随机返回名称为 key 的 set 的一个元素
func SRandMember(key string) *redis.StringCmd {
return global.REDIS.SRandMember(ctx, key)
}
// SRandMemberN 随机返回名称为 key 的 set 的count个元素
func SRandMemberN(key string, count int64) *redis.StringSliceCmd {
return global.REDIS.SRandMemberN(ctx, key, count)
}
// SMembersMap 把集合里的元素转换成map的key
func SMembersMap(key string) *redis.StringStructMapCmd {
return global.REDIS.SMembersMap(ctx, key)
}
// SMove 移动集合source中的一个member元素到集合destination中去
func SMove(source, destination string, member interface{}) *redis.BoolCmd {
return global.REDIS.SMove(ctx, source, destination, member)
}
/**
* hash操作
*/
// HDel 根据key和字段名删除hash字段支持批量删除hash字段
func HDel(key string, fields ...string) *redis.IntCmd {
return global.REDIS.HDel(ctx, key, fields...)
}
// HExists 检测hash字段名是否存在。
func HExists(key, field string) *redis.BoolCmd {
return global.REDIS.HExists(ctx, key, field)
}
// HGet 根据key和field字段查询field字段的值
func HGet(key, field string) *redis.StringCmd {
return global.REDIS.HGet(ctx, key, field)
}
// HGetAll 根据key查询所有字段和值
func HGetAll(key string) *redis.MapStringStringCmd {
return global.REDIS.HGetAll(ctx, key)
}
// HIncrBy 根据key和field字段累加数值。
func HIncrBy(key, field string, incr int64) *redis.IntCmd {
return global.REDIS.HIncrBy(ctx, key, field, incr)
}
// HIncrByFloat 根据key和field字段累加数值。
func HIncrByFloat(key, field string, incr float64) *redis.FloatCmd {
return global.REDIS.HIncrByFloat(ctx, key, field, incr)
}
// HKeys 根据key返回所有字段名
func HKeys(key string) *redis.StringSliceCmd {
return global.REDIS.HKeys(ctx, key)
}
// HLen 根据key查询hash的字段数量
func HLen(key string) *redis.IntCmd {
return global.REDIS.HLen(ctx, key)
}
// HMGet 根据key和多个字段名批量查询多个hash字段值
func HMGet(key string, fields ...string) *redis.SliceCmd {
return global.REDIS.HMGet(ctx, key, fields...)
}
// HMSet 根据key和多个字段名和字段值批量设置hash字段值
func HMSet(key string, fields map[string]interface{}) *redis.BoolCmd {
return global.REDIS.HMSet(ctx, key, fields)
}
// HSet 根据key和field字段设置field字段的值
func HSet(key, field string, value interface{}) *redis.IntCmd {
return global.REDIS.HSet(ctx, key, field, value)
}
// HSetNX 根据key和field字段查询field字段的值
func HSetNX(key, field string, value interface{}) *redis.BoolCmd {
return global.REDIS.HSetNX(ctx, key, field, value)
}
// ZAdd 添加一个或者多个元素到集合,如果元素已经存在则更新分数
func ZAdd(key string, members ...redis.Z) *redis.IntCmd {
return global.REDIS.ZAdd(ctx, key, members...)
}
func ZAddNX(key string, members ...redis.Z) *redis.IntCmd {
return global.REDIS.ZAddNX(ctx, key, members...)
}
func ZAddXX(key string, members ...redis.Z) *redis.IntCmd {
return global.REDIS.ZAddXX(ctx, key, members...)
}
func ZAddCh(key string, members ...redis.Z) *redis.IntCmd {
return global.REDIS.ZAdd(ctx, key, members...)
}
func ZAddNXCh(key string, members ...redis.Z) *redis.IntCmd {
return global.REDIS.ZAddNX(ctx, key, members...)
}
// ZAddXXCh 添加一个或者多个元素到集合,如果元素已经存在则更新分数
func ZAddXXCh(key string, members ...redis.Z) *redis.IntCmd {
return global.REDIS.ZAdd(ctx, key, members...)
}
// ZIncrBy 增加元素的分数增加的分数必须是float64类型
func ZIncrBy(key string, increment float64, member string) *redis.FloatCmd {
return global.REDIS.ZIncrBy(ctx, key, increment, member)
}
// ZInterStore 存储增加分数的元素到destination集合
func ZInterStore(destination string, store *redis.ZStore) *redis.IntCmd {
return global.REDIS.ZInterStore(ctx, destination, store)
}
// ZCard 返回集合元素个数
func ZCard(key string) *redis.IntCmd {
return global.REDIS.ZCard(ctx, key)
}
// ZCount 统计某个分数范围内的元素个数
func ZCount(key, min, max string) *redis.IntCmd {
return global.REDIS.ZCount(ctx, key, min, max)
}
// ZRange 返回集合中某个索引范围的元素,根据分数从小到大排序
func ZRange(key string, start, stop int64) *redis.StringSliceCmd {
return global.REDIS.ZRange(ctx, key, start, stop)
}
// ZRevRange ZRevRange的结果是按分数从大到小排序。
func ZRevRange(key string, start, stop int64) *redis.StringSliceCmd {
return global.REDIS.ZRevRange(ctx, key, start, stop)
}
// ZRangeByScore 根据分数范围返回集合元素,元素根据分数从小到大排序,支持分页。
func ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd {
return global.REDIS.ZRangeByScore(ctx, key, opt)
}
// ZRemRangeByScore 根据分数范围返回集合元素用法类似ZRangeByScore区别是元素根据分数从大到小排序。
func ZRemRangeByScore(key, min, max string) *redis.IntCmd {
return global.REDIS.ZRemRangeByScore(ctx, key, min, max)
}
// ZRangeWithScores 用法跟ZRangeByScore一样区别是除了返回集合元素同时也返回元素对应的分数
func ZRangeWithScores(key string, start, stop int64) *redis.ZSliceCmd {
return global.REDIS.ZRangeWithScores(ctx, key, start, stop)
}
// ZRank 根据元素名查询集合元素在集合中的排名从0开始算集合元素按分数从小到大排序
func ZRank(key, member string) *redis.IntCmd {
return global.REDIS.ZRank(ctx, key, member)
}
// ZRevRank ZRevRank的作用跟ZRank一样区别是ZRevRank是按分数从大到小排序。
func ZRevRank(key, member string) *redis.IntCmd {
return global.REDIS.ZRevRank(ctx, key, member)
}
// ZScore 查询元素对应的分数
func ZScore(key, member string) *redis.FloatCmd {
return global.REDIS.ZScore(ctx, key, member)
}
// ZRem 删除集合元素
func ZRem(key string, members ...interface{}) *redis.IntCmd {
return global.REDIS.ZRem(ctx, key, members...)
}
// ZRemRangeByRank 根据索引范围删除元素。从最低分到高分的stop-start个元素
func ZRemRangeByRank(key string, start, stop int64) *redis.IntCmd {
return global.REDIS.ZRemRangeByRank(ctx, key, start, stop)
}

19
config/conf_redis.go Normal file
View File

@@ -0,0 +1,19 @@
package config
import "time"
type Redis struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
Password string `yaml:"password"`
Db int `yaml:"db"`
MaxActive int `yaml:"max-active"`
MaxIdle int `yaml:"max-idle"`
MinIdle int `yaml:"min-idle"`
PoolSize int `yaml:"pool-size"`
PoolTimeout time.Duration `yaml:"pool-timeout"`
}
func (r *Redis) Addr() string {
return r.Host + ":" + r.Port
}

View File

@@ -4,4 +4,5 @@ type Config struct {
MySQL MySQL `yaml:"mysql"` MySQL MySQL `yaml:"mysql"`
Logger Logger `yaml:"logger"` Logger Logger `yaml:"logger"`
System System `yaml:"system"` System System `yaml:"system"`
Redis Redis `yaml:"redis"`
} }

20
core/redis.go Normal file
View File

@@ -0,0 +1,20 @@
package core
import (
"github.com/redis/go-redis/v9"
"schisandra-cloud-album/global"
)
func InitRedis() {
rdb := redis.NewClient(&redis.Options{
Addr: global.CONFIG.Redis.Addr(),
Password: global.CONFIG.Redis.Password,
DB: global.CONFIG.Redis.Db,
MaxActiveConns: global.CONFIG.Redis.MaxActive,
MaxIdleConns: global.CONFIG.Redis.MaxIdle,
PoolSize: global.CONFIG.Redis.PoolSize,
MinIdleConns: global.CONFIG.Redis.MinIdle,
PoolTimeout: global.CONFIG.Redis.PoolTimeout,
})
global.REDIS = rdb
}

View File

@@ -1,6 +1,7 @@
package global package global
import ( import (
"github.com/redis/go-redis/v9"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/wenlng/go-captcha/v2/click" "github.com/wenlng/go-captcha/v2/click"
"github.com/wenlng/go-captcha/v2/rotate" "github.com/wenlng/go-captcha/v2/rotate"
@@ -17,4 +18,5 @@ var (
TextCaptcha click.Captcha TextCaptcha click.Captcha
SlideCaptcha slide.Captcha SlideCaptcha slide.Captcha
RotateCaptcha rotate.Captcha RotateCaptcha rotate.Captcha
REDIS *redis.Client
) )

11
go.mod
View File

@@ -4,10 +4,14 @@ go 1.22.5
require ( require (
github.com/gin-gonic/gin v1.10.0 github.com/gin-gonic/gin v1.10.0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/redis/go-redis/v9 v9.6.1
github.com/sirupsen/logrus v1.9.3 github.com/sirupsen/logrus v1.9.3
github.com/swaggo/files v1.0.1 github.com/swaggo/files v1.0.1
github.com/swaggo/gin-swagger v1.6.0 github.com/swaggo/gin-swagger v1.6.0
github.com/swaggo/swag v1.16.3 github.com/swaggo/swag v1.16.3
github.com/wenlng/go-captcha-assets v1.0.1
github.com/wenlng/go-captcha/v2 v2.0.0
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/mysql v1.5.7 gorm.io/driver/mysql v1.5.7
gorm.io/gen v0.3.26 gorm.io/gen v0.3.26
@@ -19,11 +23,13 @@ require (
github.com/KyleBanks/depth v1.2.1 // indirect github.com/KyleBanks/depth v1.2.1 // indirect
github.com/bytedance/sonic v1.12.0 // indirect github.com/bytedance/sonic v1.12.0 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect github.com/cloudwego/iasm v0.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/gabriel-vasile/mimetype v1.4.5 // indirect github.com/gabriel-vasile/mimetype v1.4.5 // indirect
github.com/gin-contrib/cors v1.7.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/spec v0.21.0 // indirect github.com/go-openapi/spec v0.21.0 // indirect
@@ -33,7 +39,6 @@ require (
github.com/go-playground/validator/v10 v10.22.0 // indirect github.com/go-playground/validator/v10 v10.22.0 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/goccy/go-json v0.10.3 // indirect github.com/goccy/go-json v0.10.3 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect github.com/josharian/intern v1.0.0 // indirect
@@ -47,8 +52,6 @@ require (
github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect github.com/ugorji/go/codec v1.2.12 // indirect
github.com/wenlng/go-captcha-assets v1.0.1 // indirect
github.com/wenlng/go-captcha/v2 v2.0.0 // indirect
golang.org/x/arch v0.8.0 // indirect golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.25.0 // indirect golang.org/x/crypto v0.25.0 // indirect
golang.org/x/image v0.18.0 // indirect golang.org/x/image v0.18.0 // indirect

13
go.sum
View File

@@ -2,11 +2,17 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/bytedance/sonic v1.12.0 h1:YGPgxF9xzaCNvd/ZKdQ28yRovhfMFZQjuk6fKBzZ3ls= github.com/bytedance/sonic v1.12.0 h1:YGPgxF9xzaCNvd/ZKdQ28yRovhfMFZQjuk6fKBzZ3ls=
github.com/bytedance/sonic v1.12.0/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= github.com/bytedance/sonic v1.12.0/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM= github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM=
github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
@@ -14,15 +20,18 @@ github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQ
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4= github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4=
github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4= github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4=
github.com/gin-contrib/cors v1.7.2 h1:oLDHxdg8W/XDoN/8zamqk/Drgt4oVZDvaV0YmvVICQw=
github.com/gin-contrib/cors v1.7.2/go.mod h1:SUJVARKgQ40dmrzgXEVxj2m7Ig1v1qIboQkPDTQ9t2E=
github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4= github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4=
github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk= github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
github.com/go-bindata/go-bindata v3.1.2+incompatible h1:5vjJMVhowQdPzjE1LdxyFF7YFTXg5IgGVW4gBr5IbvE=
github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo= github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo=
github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ= github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ=
github.com/go-openapi/jsonpointer v0.21.0/go.mod h1:IUyH9l/+uyhIYQ/PXVA41Rexl+kOkAPDdXEYns6fzUY= github.com/go-openapi/jsonpointer v0.21.0/go.mod h1:IUyH9l/+uyhIYQ/PXVA41Rexl+kOkAPDdXEYns6fzUY=
@@ -98,6 +107,8 @@ github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=

View File

@@ -13,6 +13,7 @@ func main() {
core.InitLogger() core.InitLogger()
core.InitGorm() core.InitGorm()
core.InitCaptcha() core.InitCaptcha()
core.InitRedis()
// 命令行参数绑定 // 命令行参数绑定
option := cmd.Parse() option := cmd.Parse()
if cmd.IsStopWeb(&option) { if cmd.IsStopWeb(&option) {

26
middleware/cors.go Normal file
View File

@@ -0,0 +1,26 @@
package middleware
import (
"github.com/gin-gonic/gin"
"net/http"
)
// Cors 跨域中间件
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
method := c.Request.Method
origin := c.Request.Header.Get("Origin")
if origin != "" {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
c.Header("Access-Control-Allow-Credentials", "true")
c.Set("content-type", "application/json")
}
//放行所有OPTIONS方法
if method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
}
c.Next()
}
}

View File

@@ -1,6 +1,7 @@
package router package router
import ( import (
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"schisandra-cloud-album/global" "schisandra-cloud-album/global"
"schisandra-cloud-album/router/modules" "schisandra-cloud-album/router/modules"
@@ -14,8 +15,9 @@ func InitRouter() *gin.Engine {
global.LOG.Error(err) global.LOG.Error(err)
return nil return nil
} }
PublicGroup := router.Group("api") publicGroup := router.Group("api")
publicGroup.Use(cors.Default())
modules.SwaggerRouter(router) // 注册swagger路由 modules.SwaggerRouter(router) // 注册swagger路由
modules.AuthRouter(PublicGroup) // 注册鉴权路由 modules.AuthRouter(publicGroup) // 注册鉴权路由
return router return router
} }