- update storage

- update string
- update trace_id
- update uuid
master
李光春 1 year ago
parent 16cf68e8af
commit 221a820e72

@ -0,0 +1,25 @@
package gostorage
import (
"context"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
type Aws struct {
client *s3.Client
}
func NewAws() *Aws {
ac := &Aws{}
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
panic("configuration error, " + err.Error())
}
ac.client = s3.NewFromConfig(cfg)
return ac
}

@ -0,0 +1,15 @@
package gostring
func GetDefault(key, defVal any) any {
if key != nil {
return key
}
return defVal
}
func GetStringDefault(key, defVal string) string {
if key != "" {
return key
}
return defVal
}

@ -0,0 +1,71 @@
package gostring
import (
"math/rand"
"time"
)
// GenerateRandom 生成count个长度length不重复的随机数
func GenerateRandom(length, count int) []int {
return GenerateRandomFunc(length, count, func(num int) bool {
return false
})
}
// GenerateRandomFunc 生成count个长度length不重复的随机数支持外部查询
func GenerateRandomFunc(length, count int, dFun func(num int) bool) []int {
var fI int = 0
startStr := "1"
endStr := "9"
for {
if fI+2 > length {
break
}
startStr += "0"
endStr += "9"
fI = fI + 1
}
return GenerateRandomNumber(ToInt(startStr), ToInt(endStr), count, dFun)
}
// GenerateRandomNumber 生成count个[start,end)结束的不重复的随机数
func GenerateRandomNumber(start, end, count int, dFun func(num int) bool) []int {
// 范围检查
if end < start || (end-start) < count {
return nil
}
// 存放结果的slice
nums := make([]int, 0)
// 随机数生成器,加入时间戳保证每次生成的随机数不一样
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for len(nums) < count {
// 生成随机数
num := r.Intn(end-start) + start
// 查重
exist := false
for _, v := range nums {
if v == num {
exist = true
break
}
}
if !exist {
isExist := dFun(num)
if isExist == false {
nums = append(nums, num)
}
}
}
return nums
}

@ -0,0 +1,3 @@
package gotrace_id
const Nil = "%!s(<nil>)"

@ -8,18 +8,26 @@ import (
)
// CustomTraceIdContext 自定义设置跟踪编号上下文
func CustomTraceIdContext() context.Context {
return context.WithValue(context.Background(), "trace_id", gostring.GetUuId())
func CustomTraceIdContext(ctx context.Context) context.Context {
return context.WithValue(ctx, "trace_id", gostring.GetUuId())
}
// SetCustomTraceId 自定义设置跟踪编号上下文
func SetCustomTraceId(ctx context.Context, traceId string) context.Context {
return context.WithValue(ctx, "trace_id", traceId)
}
// SetGinTraceIdContext 设置跟踪编号上下文
func SetGinTraceIdContext(c *gin.Context) context.Context {
return context.WithValue(context.Background(), "trace_id", GetGinTraceId(c))
func SetGinTraceIdContext(ctx context.Context, c *gin.Context) context.Context {
return context.WithValue(ctx, "trace_id", GetGinTraceId(c))
}
// GetTraceIdContext 通过上下文获取跟踪编号
func GetTraceIdContext(ctx context.Context) string {
traceId := fmt.Sprintf("%v", ctx.Value("trace_id"))
traceId := fmt.Sprintf("%s", ctx.Value("trace_id"))
if traceId == Nil {
return ""
}
if len(traceId) <= 0 {
return ""
}

@ -21,7 +21,10 @@ func SetGinTraceId() gin.HandlerFunc {
// GetGinTraceId 通过gin中间件获取跟踪编号
func GetGinTraceId(c *gin.Context) string {
traceId := fmt.Sprintf("%v", c.MustGet("trace_id"))
traceId := fmt.Sprintf("%s", c.MustGet("trace_id"))
if traceId == Nil {
return ""
}
if len(traceId) <= 0 {
return ""
}

@ -0,0 +1,18 @@
package gouuid
import (
"crypto/rand"
"fmt"
"time"
)
// GetUuId 获取唯一ID
func GetUuId() string {
unix32bits := uint32(time.Now().UTC().Unix())
buff := make([]byte, 12)
numRead, err := rand.Read(buff)
if numRead != len(buff) || err != nil {
return ""
}
return fmt.Sprintf("%x-%x-%x-%x-%x-%x", unix32bits, buff[0:2], buff[2:4], buff[4:6], buff[6:8], buff[8:])
}
Loading…
Cancel
Save