Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5b9a6c6cc5 | |||
| ebed85772f | |||
| 40afc6c0d5 | |||
| ffa4a64107 | |||
| f8f647c12a | |||
| d88178458d |
18
README.md
18
README.md
@@ -11,6 +11,16 @@ go env -w GOINSECURE=git.apinb.com/*
|
||||
go env -w GONOSUMDB=git.apinb.com/*
|
||||
```
|
||||
|
||||
### 配置环境变量
|
||||
|
||||
```bash
|
||||
export BSM_Workspace=def
|
||||
export BSM_JwtSecretKey=your_secret_key
|
||||
export BSM_RuntimeMode=dev
|
||||
export BSM_Prefix=/usr/local/bsm
|
||||
```
|
||||
|
||||
|
||||
## 核心功能模块
|
||||
|
||||
### 1. 服务管理 (service)
|
||||
@@ -321,14 +331,6 @@ go env -w GONOSUMDB=git.apinb.com/*
|
||||
- 支持许可证文件验证
|
||||
|
||||
|
||||
### 配置环境变量
|
||||
|
||||
```bash
|
||||
export BSM_Workspace=def
|
||||
export BSM_JwtSecretKey=your_secret_key
|
||||
export BSM_RuntimeMode=dev
|
||||
export BSM_Prefix=/usr/local/bsm
|
||||
```
|
||||
|
||||
### 安全建议
|
||||
|
||||
|
||||
62
cache/mapsync/map.go
vendored
Normal file
62
cache/mapsync/map.go
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
package mapsync
|
||||
|
||||
import "sync"
|
||||
|
||||
type syncMap[T any] struct {
|
||||
sync.RWMutex
|
||||
Data map[string]T
|
||||
}
|
||||
|
||||
func newSyncMap[T any]() *syncMap[T] {
|
||||
return &syncMap[T]{
|
||||
Data: make(map[string]T),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *syncMap[T]) Set(key string, val T) {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
if c.Data == nil {
|
||||
c.Data = make(map[string]T)
|
||||
}
|
||||
c.Data[key] = val
|
||||
}
|
||||
|
||||
func (c *syncMap[T]) Get(key string) T {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
return c.Data[key]
|
||||
}
|
||||
|
||||
func (c *syncMap[T]) Del(key string) {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
delete(c.Data, key)
|
||||
}
|
||||
|
||||
func (c *syncMap[T]) Keys() (keys []string) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
keys = make([]string, 0, len(c.Data))
|
||||
for k := range c.Data {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
|
||||
return keys
|
||||
}
|
||||
|
||||
func (c *syncMap[T]) All() map[string]T {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
out := make(map[string]T, len(c.Data))
|
||||
for k, v := range c.Data {
|
||||
out[k] = v
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
56
cache/mapsync/map_float.go
vendored
56
cache/mapsync/map_float.go
vendored
@@ -1,64 +1,12 @@
|
||||
package mapsync
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
// sync map
|
||||
MapFloat *mapFloat
|
||||
)
|
||||
|
||||
// lock
|
||||
type mapFloat struct {
|
||||
sync.RWMutex
|
||||
Data map[string]float64
|
||||
}
|
||||
type mapFloat = syncMap[float64]
|
||||
|
||||
func NewMapFloat() *mapFloat {
|
||||
return &mapFloat{
|
||||
Data: make(map[string]float64),
|
||||
}
|
||||
}
|
||||
func (c *mapFloat) Set(key string, val float64) {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
c.Data[key] = val
|
||||
}
|
||||
|
||||
func (c *mapFloat) Get(key string) float64 {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
vals, ok := c.Data[key]
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
|
||||
return vals
|
||||
}
|
||||
|
||||
func (c *mapFloat) Del(key string) {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
delete(c.Data, key)
|
||||
}
|
||||
|
||||
func (c *mapFloat) Keys() (keys []string) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
for k, _ := range c.Data {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (c *mapFloat) All() map[string]float64 {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
return c.Data
|
||||
return newSyncMap[float64]()
|
||||
}
|
||||
|
||||
56
cache/mapsync/map_int.go
vendored
56
cache/mapsync/map_int.go
vendored
@@ -1,64 +1,12 @@
|
||||
package mapsync
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
// sync map
|
||||
MapInt *mapInt
|
||||
)
|
||||
|
||||
// lock
|
||||
type mapInt struct {
|
||||
sync.RWMutex
|
||||
Data map[string]int
|
||||
}
|
||||
type mapInt = syncMap[int]
|
||||
|
||||
func NewMapInt() *mapInt {
|
||||
return &mapInt{
|
||||
Data: make(map[string]int),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *mapInt) Set(key string, val int) {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
c.Data[key] = val
|
||||
}
|
||||
|
||||
func (c *mapInt) Get(key string) int {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
vals, ok := c.Data[key]
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
|
||||
return vals
|
||||
}
|
||||
|
||||
func (c *mapInt) Del(key string) {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
delete(c.Data, key)
|
||||
}
|
||||
|
||||
func (c *mapInt) All() map[string]int {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
return c.Data
|
||||
}
|
||||
func (c *mapInt) Keys() (keys []string) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
for k, _ := range c.Data {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
|
||||
return
|
||||
return newSyncMap[int]()
|
||||
}
|
||||
|
||||
56
cache/mapsync/map_string.go
vendored
56
cache/mapsync/map_string.go
vendored
@@ -1,64 +1,12 @@
|
||||
package mapsync
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
// sync map
|
||||
MapString *mapString
|
||||
)
|
||||
|
||||
// lock
|
||||
type mapString struct {
|
||||
sync.RWMutex
|
||||
Data map[string]string
|
||||
}
|
||||
type mapString = syncMap[string]
|
||||
|
||||
func NewMapString() *mapString {
|
||||
return &mapString{
|
||||
Data: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *mapString) Set(key, val string) {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
c.Data[key] = val
|
||||
}
|
||||
func (c *mapString) Get(key string) string {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
vals, ok := c.Data[key]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
|
||||
return vals
|
||||
}
|
||||
|
||||
func (c *mapString) Del(key string) {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
delete(c.Data, key)
|
||||
}
|
||||
|
||||
func (c *mapString) Keys() (keys []string) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
for k, _ := range c.Data {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (c *mapString) All() map[string]string {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
return c.Data
|
||||
return newSyncMap[string]()
|
||||
}
|
||||
|
||||
14
cache/redis/cache.go
vendored
14
cache/redis/cache.go
vendored
@@ -5,6 +5,7 @@ package redis
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
@@ -15,19 +16,20 @@ import (
|
||||
// prefix: 键前缀
|
||||
// params: 键参数
|
||||
// 返回: 完整的缓存键
|
||||
func (c *RedisClient) BuildKey(prefix string, params ...interface{}) string {
|
||||
key := vars.CacheKeyPrefix + prefix
|
||||
func (c *RedisClient) BuildKey(prefix string, params ...any) string {
|
||||
var key strings.Builder
|
||||
key.WriteString(vars.CacheKeyPrefix + prefix)
|
||||
for _, param := range params {
|
||||
key += fmt.Sprintf(":%v", param)
|
||||
key.WriteString(fmt.Sprintf(":%v", param))
|
||||
}
|
||||
return key
|
||||
return key.String()
|
||||
}
|
||||
|
||||
// Get 获取缓存
|
||||
// key: 缓存键
|
||||
// result: 存储结果的指针
|
||||
// 返回: 错误信息
|
||||
func (c *RedisClient) Get(key string, result interface{}) error {
|
||||
func (c *RedisClient) Get(key string, result any) error {
|
||||
if c.Client == nil {
|
||||
return errcode.ErrRedis
|
||||
}
|
||||
@@ -45,7 +47,7 @@ func (c *RedisClient) Get(key string, result interface{}) error {
|
||||
// value: 缓存值
|
||||
// ttl: 过期时间
|
||||
// 返回: 错误信息
|
||||
func (c *RedisClient) Set(key string, value interface{}, ttl time.Duration) error {
|
||||
func (c *RedisClient) Set(key string, value any, ttl time.Duration) error {
|
||||
if c.Client == nil {
|
||||
return errcode.ErrRedis
|
||||
}
|
||||
|
||||
35
cache/redis/redis.go
vendored
35
cache/redis/redis.go
vendored
@@ -2,6 +2,7 @@ package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"net/url"
|
||||
"strconv"
|
||||
@@ -24,10 +25,22 @@ type RedisClient struct {
|
||||
}
|
||||
|
||||
func New(dsn string, hashRadix string) *RedisClient {
|
||||
arg, err := url.Parse(dsn)
|
||||
client, err := NewWithContext(context.Background(), dsn, hashRadix)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
||||
func NewWithContext(ctx context.Context, dsn string, hashRadix string) (*RedisClient, error) {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
arg, err := url.Parse(dsn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse redis dsn: %w", err)
|
||||
}
|
||||
pwd, _ := arg.User.Password()
|
||||
|
||||
//get db number,default:0
|
||||
@@ -36,7 +49,10 @@ func New(dsn string, hashRadix string) *RedisClient {
|
||||
if arg.Path == "" {
|
||||
db = Hash(hashRadix)
|
||||
} else {
|
||||
db, _ = strconv.Atoi(arg.Path)
|
||||
db, err = strconv.Atoi(arg.Path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse redis db index: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
//connect redis server
|
||||
@@ -46,21 +62,26 @@ func New(dsn string, hashRadix string) *RedisClient {
|
||||
DB: db, // use default DB
|
||||
Protocol: 3,
|
||||
})
|
||||
_, err = client.Ping(context.Background()).Result()
|
||||
_, err = client.Ping(ctx).Result()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
_ = client.Close()
|
||||
return nil, fmt.Errorf("ping redis: %w", err)
|
||||
}
|
||||
|
||||
return &RedisClient{
|
||||
DB: db,
|
||||
Client: client,
|
||||
Ctx: context.Background(),
|
||||
Ctx: ctx,
|
||||
memory: make(map[string]any),
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
func Hash(s string) int {
|
||||
if vars.RedisShardings <= 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
h := fnv.New32a()
|
||||
h.Write([]byte(strings.ToLower(s)))
|
||||
_, _ = h.Write([]byte(strings.ToLower(s)))
|
||||
return int(h.Sum32()) % vars.RedisShardings
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package conf
|
||||
|
||||
import "git.apinb.com/bsm-sdk/core/vars"
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
type Base struct {
|
||||
Service string `yaml:"Service"` // 服务名称
|
||||
@@ -93,3 +95,8 @@ type LogConf struct {
|
||||
File bool `yaml:"File"`
|
||||
Remote bool `yaml:"Remote"`
|
||||
}
|
||||
|
||||
type MemoryCacheConf struct {
|
||||
DefaultExpiration int `yaml:"DefaultExpiration"`
|
||||
CleanupInterval int `yaml:"CleanupInterval"`
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ func (t *tokenJwt) GenerateJwt(id uint, identity, client, role string, owner any
|
||||
|
||||
// 解析JWT
|
||||
func (t *tokenJwt) ParseJwt(tokenstring string) (*Claims, error) {
|
||||
token, err := jwt.ParseWithClaims(tokenstring, &Claims{}, func(token *jwt.Token) (interface{}, error) {
|
||||
token, err := jwt.ParseWithClaims(tokenstring, &Claims{}, func(token *jwt.Token) (any, error) {
|
||||
return []byte(t.SecretKey), nil
|
||||
})
|
||||
if claims, ok := token.Claims.(*Claims); ok && token.Valid {
|
||||
|
||||
@@ -44,7 +44,7 @@ func NewElastic(endpoints []string, username, password string) (*ES, error) {
|
||||
// "time": time.Now().Unix(),
|
||||
// "date": time.Now(),
|
||||
// }
|
||||
func (es *ES) CreateDocument(index string, id string, doc *interface{}) {
|
||||
func (es *ES) CreateDocument(index string, id string, doc *any) {
|
||||
var buf bytes.Buffer
|
||||
if err := json.NewEncoder(&buf).Encode(doc); err != nil {
|
||||
log.Println("Elastic NewEncoder:", err)
|
||||
@@ -75,7 +75,7 @@ func (es *ES) CreateDocument(index string, id string, doc *interface{}) {
|
||||
// index 如果文档不存在就创建,如果文档存在就更新
|
||||
// update 更新一个文档,如果文档不存在就返回错误
|
||||
// delete 删除一个文档,如果要删除的文档id不存在,就返回错误
|
||||
func (es *ES) Batch(index string, documens []map[string]interface{}, action string) {
|
||||
func (es *ES) Batch(index string, documens []map[string]any, action string) {
|
||||
log.SetFlags(0)
|
||||
|
||||
var (
|
||||
@@ -162,7 +162,7 @@ func (es *ES) Batch(index string, documens []map[string]interface{}, action stri
|
||||
}
|
||||
}
|
||||
|
||||
func (es *ES) Search(index string, query map[string]interface{}) (res *esapi.Response, err error) {
|
||||
func (es *ES) Search(index string, query map[string]any) (res *esapi.Response, err error) {
|
||||
var buf bytes.Buffer
|
||||
if err = json.NewEncoder(&buf).Encode(query); err != nil {
|
||||
return
|
||||
@@ -201,7 +201,7 @@ func (es *ES) Delete(index, idx string) (res *esapi.Response, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (es *ES) DeleteByQuery(index []string, query map[string]interface{}) (res *esapi.Response, err error) {
|
||||
func (es *ES) DeleteByQuery(index []string, query map[string]any) (res *esapi.Response, err error) {
|
||||
var buf bytes.Buffer
|
||||
if err = json.NewEncoder(&buf).Encode(query); err != nil {
|
||||
return
|
||||
|
||||
@@ -6,9 +6,8 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/database/sql"
|
||||
dbsql "git.apinb.com/bsm-sdk/core/database/sql"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -40,7 +39,7 @@ func NewDatabase(driver string, dsn []string, options *types.SqlOptions) (db *go
|
||||
}
|
||||
|
||||
// 自动迁移表结构
|
||||
if len(MigrateTables) > 0 {
|
||||
if len(MigrateTables) > 0 && options.IsAutoMigrate {
|
||||
err = db.AutoMigrate(MigrateTables...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -50,47 +49,50 @@ func NewDatabase(driver string, dsn []string, options *types.SqlOptions) (db *go
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func firstDSN(dsn []string) (string, error) {
|
||||
if len(dsn) == 0 || strings.TrimSpace(dsn[0]) == "" {
|
||||
return "", fmt.Errorf("database dsn is empty")
|
||||
}
|
||||
return dsn[0], nil
|
||||
}
|
||||
|
||||
func applySqlOptions(gormDb *gorm.DB, options *types.SqlOptions) (*gorm.DB, error) {
|
||||
if options.Debug {
|
||||
gormDb = gormDb.Debug()
|
||||
}
|
||||
|
||||
sqlDB, err := gormDb.DB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sqlDB.SetMaxIdleConns(options.MaxIdleConns)
|
||||
sqlDB.SetMaxOpenConns(options.MaxOpenConns)
|
||||
sqlDB.SetConnMaxLifetime(options.ConnMaxLifetime)
|
||||
|
||||
return gormDb, nil
|
||||
}
|
||||
|
||||
// NewMysql 创建MySQL数据库服务
|
||||
// dsn: 数据源名称数组
|
||||
// options: 数据库连接选项
|
||||
// 返回: GORM数据库实例
|
||||
func NewMysql(dsn []string, options *types.SqlOptions) (gormDb *gorm.DB, err error) {
|
||||
// 设置连接默认值
|
||||
if options == nil {
|
||||
options = &types.SqlOptions{
|
||||
MaxIdleConns: vars.SqlOptionMaxIdleConns,
|
||||
MaxOpenConns: vars.SqlOptionMaxOpenConns,
|
||||
ConnMaxLifetime: vars.SqlOptionConnMaxLifetime,
|
||||
LogStdout: false,
|
||||
Debug: true,
|
||||
}
|
||||
options = dbsql.SetOptions(options)
|
||||
|
||||
dsn0, err := firstDSN(dsn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
gormDb, err = gorm.Open(mysql.Open(dsn[0]), &gorm.Config{
|
||||
gormDb, err = gorm.Open(mysql.Open(dsn0), &gorm.Config{
|
||||
SkipDefaultTransaction: true,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if options.Debug {
|
||||
gormDb = gormDb.Debug()
|
||||
}
|
||||
|
||||
// 获取通用数据库对象 sql.DB,然后使用其提供的功能
|
||||
sqlDB, err := gormDb.DB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// SetMaxIdleConns 用于设置连接池中空闲连接的最大数量
|
||||
sqlDB.SetMaxIdleConns(options.MaxIdleConns)
|
||||
// SetMaxOpenConns 设置打开数据库连接的最大数量
|
||||
sqlDB.SetMaxOpenConns(options.MaxOpenConns)
|
||||
// SetConnMaxLifetime 设置了连接可复用的最大时间
|
||||
sqlDB.SetConnMaxLifetime(options.ConnMaxLifetime)
|
||||
|
||||
return gormDb, nil
|
||||
return applySqlOptions(gormDb, options)
|
||||
}
|
||||
|
||||
// NewPostgres 创建PostgreSQL数据库服务
|
||||
@@ -98,40 +100,12 @@ func NewMysql(dsn []string, options *types.SqlOptions) (gormDb *gorm.DB, err err
|
||||
// options: 数据库连接选项
|
||||
// 返回: GORM数据库实例
|
||||
func NewPostgres(dsn []string, options *types.SqlOptions) (gormDb *gorm.DB, err error) {
|
||||
// 设置连接默认值
|
||||
if options == nil {
|
||||
options = &types.SqlOptions{
|
||||
MaxIdleConns: vars.SqlOptionMaxIdleConns,
|
||||
MaxOpenConns: vars.SqlOptionMaxOpenConns,
|
||||
ConnMaxLifetime: vars.SqlOptionConnMaxLifetime,
|
||||
LogStdout: false,
|
||||
Debug: true,
|
||||
}
|
||||
}
|
||||
|
||||
gormDb, err = sql.NewPostgreSql(dsn[0], options)
|
||||
dsn0, err := firstDSN(dsn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if options.Debug {
|
||||
gormDb = gormDb.Debug()
|
||||
}
|
||||
|
||||
// 获取通用数据库对象 sql.DB,然后使用其提供的功能
|
||||
sqlDB, err := gormDb.DB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// SetMaxIdleConns 用于设置连接池中空闲连接的最大数量
|
||||
sqlDB.SetMaxIdleConns(options.MaxIdleConns)
|
||||
// SetMaxOpenConns 设置打开数据库连接的最大数量
|
||||
sqlDB.SetMaxOpenConns(options.MaxOpenConns)
|
||||
// SetConnMaxLifetime 设置了连接可复用的最大时间
|
||||
sqlDB.SetConnMaxLifetime(options.ConnMaxLifetime)
|
||||
|
||||
return gormDb, nil
|
||||
return dbsql.NewPostgreSql(dsn0, options)
|
||||
}
|
||||
|
||||
// AppendMigrate 调用此函数后,会在数据库初始化时自动迁移表结构
|
||||
|
||||
@@ -14,8 +14,9 @@ func SetOptions(options *types.SqlOptions) *types.SqlOptions {
|
||||
MaxIdleConns: vars.SqlOptionMaxIdleConns,
|
||||
MaxOpenConns: vars.SqlOptionMaxOpenConns,
|
||||
ConnMaxLifetime: vars.SqlOptionConnMaxLifetime,
|
||||
IsAutoMigrate: false,
|
||||
LogStdout: false,
|
||||
Debug: false,
|
||||
Debug: true,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,16 +27,7 @@ func SetOptions(options *types.SqlOptions) *types.SqlOptions {
|
||||
func NewPostgreSql(dsn string, options *types.SqlOptions) (*gorm.DB, error) {
|
||||
var err error
|
||||
|
||||
//set connection default val.
|
||||
if options == nil {
|
||||
options = &types.SqlOptions{
|
||||
MaxIdleConns: vars.SqlOptionMaxIdleConns,
|
||||
MaxOpenConns: vars.SqlOptionMaxOpenConns,
|
||||
ConnMaxLifetime: vars.SqlOptionConnMaxLifetime,
|
||||
LogStdout: false,
|
||||
Debug: true,
|
||||
}
|
||||
}
|
||||
options = SetOptions(options)
|
||||
|
||||
gormDb, err := gorm.Open(postgres.New(postgres.Config{
|
||||
DSN: dsn,
|
||||
|
||||
89
go.mod
89
go.mod
@@ -1,3 +1,90 @@
|
||||
module git.apinb.com/bsm-sdk/core
|
||||
|
||||
go 1.25.1
|
||||
go 1.26.5
|
||||
|
||||
require (
|
||||
github.com/elastic/go-elasticsearch/v9 v9.5.0
|
||||
github.com/gin-contrib/cors v1.7.7
|
||||
github.com/gin-gonic/gin v1.12.0
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.30.0
|
||||
github.com/nats-io/nats.go v1.52.0
|
||||
github.com/oklog/ulid/v2 v2.1.2
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||
github.com/redis/go-redis/v9 v9.22.0
|
||||
github.com/shirou/gopsutil v3.21.11+incompatible
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.7.1
|
||||
go.etcd.io/etcd/client/v3 v3.7.1
|
||||
google.golang.org/grpc v1.83.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
gorm.io/driver/mysql v1.6.0
|
||||
gorm.io/driver/postgres v1.6.2
|
||||
gorm.io/gorm v1.31.2
|
||||
)
|
||||
|
||||
require (
|
||||
filippo.io/edwards25519 v1.2.0 // indirect
|
||||
github.com/bytedance/gopkg v0.1.4 // indirect
|
||||
github.com/bytedance/sonic v1.15.2 // indirect
|
||||
github.com/bytedance/sonic/loader v0.5.2 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/cloudwego/base64x v0.1.7 // indirect
|
||||
github.com/coreos/go-semver v0.3.1 // indirect
|
||||
github.com/coreos/go-systemd/v22 v22.7.0 // indirect
|
||||
github.com/elastic/elastic-transport-go/v8 v8.11.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.15 // indirect
|
||||
github.com/gin-contrib/sse v1.1.1 // indirect
|
||||
github.com/go-logr/logr v1.4.4 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/go-ole/go-ole v1.3.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.30.3 // indirect
|
||||
github.com/go-sql-driver/mysql v1.10.0 // indirect
|
||||
github.com/goccy/go-json v0.10.6 // indirect
|
||||
github.com/goccy/go-yaml v1.19.2 // indirect
|
||||
github.com/golang/protobuf v1.5.4 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/pgx/v5 v5.10.0 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/compress v1.19.2 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.4.0 // indirect
|
||||
github.com/leodido/go-urn v1.5.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.24 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/nats-io/nkeys v0.4.16 // indirect
|
||||
github.com/nats-io/nuid v1.0.1 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.4.3 // indirect
|
||||
github.com/quic-go/qpack v0.6.0 // indirect
|
||||
github.com/quic-go/quic-go v0.61.0 // indirect
|
||||
github.com/tklauser/go-sysconf v0.4.0 // indirect
|
||||
github.com/tklauser/numcpus v0.12.0 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.3.2 // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.4 // indirect
|
||||
go.etcd.io/etcd/api/v3 v3.7.1 // indirect
|
||||
go.mongodb.org/mongo-driver/v2 v2.8.0 // indirect
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
|
||||
go.opentelemetry.io/otel v1.45.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.45.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.45.0 // indirect
|
||||
go.uber.org/atomic v1.11.0 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
go.uber.org/zap v1.28.0 // indirect
|
||||
golang.org/x/arch v0.30.0 // indirect
|
||||
golang.org/x/crypto v0.54.0 // indirect
|
||||
golang.org/x/net v0.57.0 // indirect
|
||||
golang.org/x/sync v0.22.0 // indirect
|
||||
golang.org/x/sys v0.47.0 // indirect
|
||||
golang.org/x/text v0.40.0 // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260807164820-c8921c73eeea // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260807164820-c8921c73eeea // indirect
|
||||
google.golang.org/protobuf v1.36.11 // indirect
|
||||
)
|
||||
|
||||
226
go.sum
226
go.sum
@@ -0,0 +1,226 @@
|
||||
filippo.io/edwards25519 v1.2.0 h1:crnVqOiS4jqYleHd9vaKZ+HKtHfllngJIiOpNpoJsjo=
|
||||
filippo.io/edwards25519 v1.2.0/go.mod h1:xzAOLCNug/yB62zG1bQ8uziwrIqIuxhctzJT18Q77mc=
|
||||
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/gopkg v0.1.4 h1:oZnQwnX82KAIWb7033bEwtxvTqXcYMxDBaQxo5JJHWM=
|
||||
github.com/bytedance/gopkg v0.1.4/go.mod h1:v1zWfPm21Fb+OsyXN2VAHdL6TBb2L88anLQgdyje6R4=
|
||||
github.com/bytedance/sonic v1.15.2 h1:90H+rcF/FwLXwfB1cudOLq/je83n683Utf4Cbp0xHCo=
|
||||
github.com/bytedance/sonic v1.15.2/go.mod h1:mT2NbXunuaEbnZ+mRIX/vYqKISmgEuHFDI4UzmKx2SA=
|
||||
github.com/bytedance/sonic/loader v0.5.2 h1:0QtP1gevc1OZ6/H8Lb9BRZiCXd1Ftjd3OKuj1T1lBIo=
|
||||
github.com/bytedance/sonic/loader v0.5.2/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/cloudwego/base64x v0.1.7 h1:NppS+Fgzg5ovhn4NkUXaDT3x9jldgH5ToMCqzBSi2zI=
|
||||
github.com/cloudwego/base64x v0.1.7/go.mod h1:Cu1PV9zfrSf7ET2tIbWbbEy7jO7HHJ13q4X2SQ8aWYg=
|
||||
github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4=
|
||||
github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec=
|
||||
github.com/coreos/go-systemd/v22 v22.7.0 h1:LAEzFkke61DFROc7zNLX/WA2i5J8gYqe0rSj9KI28KA=
|
||||
github.com/coreos/go-systemd/v22 v22.7.0/go.mod h1:xNUYtjHu2EDXbsxz1i41wouACIwT7Ybq9o0BQhMwD0w=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/elastic/elastic-transport-go/v8 v8.11.0 h1:taYmqC2M6+fZt/+W+ENYh/W5L9+KrlJGOSbEJs8egWc=
|
||||
github.com/elastic/elastic-transport-go/v8 v8.11.0/go.mod h1:DZQ0szCNywc9F+C9l/Kkd4n69SvJVj0I3yK1Of7s3l8=
|
||||
github.com/elastic/go-elasticsearch/v9 v9.5.0 h1:ye9pnajcrSE2Fra0Q++nJsi09p2dJTaN6YXKz7LL7WU=
|
||||
github.com/elastic/go-elasticsearch/v9 v9.5.0/go.mod h1:EbZnYlTlbhNqRt98YW+QSM2SEKplN5o8J+G6d9STOVo=
|
||||
github.com/gabriel-vasile/mimetype v1.4.15 h1:05iP/CYtZ/w455R/KZM6rZ5ieAdh99UPtd+d3YzLmaI=
|
||||
github.com/gabriel-vasile/mimetype v1.4.15/go.mod h1:azpTcoLcDZRNgFou5j+APrqQx9HqVPWa6ijYQIIVswQ=
|
||||
github.com/gin-contrib/cors v1.7.7 h1:Oh9joP463x7Mw72vhvJ61YQm8ODh9b04YR7vsOErD0Q=
|
||||
github.com/gin-contrib/cors v1.7.7/go.mod h1:K5tW0RkzJtWSiOdikXloy8VEZlgdVNpHNw8FpjUPNrE=
|
||||
github.com/gin-contrib/sse v1.1.1 h1:uGYpNwTacv5R68bSGMapo62iLTRa9l5zxGCps4hK6ko=
|
||||
github.com/gin-contrib/sse v1.1.1/go.mod h1:QXzuVkA0YO7o/gun03UI1Q+FTI8ZV/n5t03kIQAI89s=
|
||||
github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8=
|
||||
github.com/gin-gonic/gin v1.12.0/go.mod h1:VxccKfsSllpKshkBWgVgRniFFAzFb9csfngsqANjnLc=
|
||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/logr v1.4.4 h1:tG4xh9yMsRCAiodLVTxyrkzSZ9+o0L1Kg/+cPVcbP/8=
|
||||
github.com/go-logr/logr v1.4.4/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
|
||||
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
|
||||
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.30.3 h1:4MU6YkEwx7GbcPJOZxrtbu+QfF3pJLJuaYTeAH0DYy8=
|
||||
github.com/go-playground/validator/v10 v10.30.3/go.mod h1:4Axh7oCNGcoGkqLoE4YWt6n20mcEIsPRlB7vPk3lpyc=
|
||||
github.com/go-sql-driver/mysql v1.10.0 h1:Q+1LV8DkHJvSYAdR83XzuhDaTykuDx0l6fkXxoWCWfw=
|
||||
github.com/go-sql-driver/mysql v1.10.0/go.mod h1:M+cqaI7+xxXGG9swrdeUIoPG3Y3KCkF0pZej+SK+nWk=
|
||||
github.com/goccy/go-json v0.10.6 h1:p8HrPJzOakx/mn/bQtjgNjdTcN+/S6FcG2CTtQOrHVU=
|
||||
github.com/goccy/go-json v0.10.6/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM=
|
||||
github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
|
||||
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
||||
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.30.0 h1:/Tnpcb2E0Pz/tN9s3bfEY2Q8ePCEX9iuS+cneUwncnw=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.30.0/go.mod h1:zOBXOsUaBSjKgmH4OGzV1esUpR3oUSCPYVd2cUBjKYY=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.10.0 h1:VhSvgU2jSli8o3AqIEOTJr7rZwAEUVo4E4XhR94Zfr0=
|
||||
github.com/jackc/pgx/v5 v5.10.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
|
||||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/klauspost/compress v1.19.2 h1:hMRETovs/pu/dVWN7zIT1PGG8t509MwT6bO7XSi26R8=
|
||||
github.com/klauspost/compress v1.19.2/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
|
||||
github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw=
|
||||
github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/leodido/go-urn v1.5.0 h1:pLqT2kq1zpHW/1D18QMjMpdtX7cekxqtJJjg5ANyWw0=
|
||||
github.com/leodido/go-urn v1.5.0/go.mod h1:9BORnCDhdPBJNDEX+w1bJisa8yOKYi116VeO96s4ifE=
|
||||
github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI=
|
||||
github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A=
|
||||
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/nats-io/nats.go v1.52.0 h1:n3avV4VBsCgsdwh71TppsTwtv+QdPs7ntSKM8qJLGsc=
|
||||
github.com/nats-io/nats.go v1.52.0/go.mod h1:26HypzazeOkyO3/mqd1zZd53STJN0EjCYF9Uy2ZOBno=
|
||||
github.com/nats-io/nkeys v0.4.16 h1:rd5oAuLOb8mnAycB0xleuEBNS1pVVnN0fv/FF34Eypg=
|
||||
github.com/nats-io/nkeys v0.4.16/go.mod h1:llLgWoI0o4z/Q57q2R1kHfmocyhGV6VG/U18Glg1Afs=
|
||||
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
|
||||
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
|
||||
github.com/oklog/ulid/v2 v2.1.2 h1:IEclFb9JNvzYA6MW2SCxbLzcHTVsfqm3PrqGQJH5zec=
|
||||
github.com/oklog/ulid/v2 v2.1.2/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
|
||||
github.com/pelletier/go-toml/v2 v2.4.3 h1:GTRvJQutkOSftxIFD5xw9aepkYNuPWmVJpffdDPYVpY=
|
||||
github.com/pelletier/go-toml/v2 v2.4.3/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/quic-go/go-ossfuzz-seeds v0.1.0 h1:APacT+iIaNF6fd8AGEiN3bT/Jtkd2jz4v4TzM7MFjy0=
|
||||
github.com/quic-go/go-ossfuzz-seeds v0.1.0/go.mod h1:3IOHRbJIc+L6YKMwfDtJAM9Vj9k0YY4muhuyUYk5tbk=
|
||||
github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8=
|
||||
github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII=
|
||||
github.com/quic-go/quic-go v0.61.0 h1:ui88A53s8MSVYLC56en0KQ17HARk+9986Dn0SBfKNvA=
|
||||
github.com/quic-go/quic-go v0.61.0/go.mod h1:9So2anK4Tp22URSQq00k+Vo2PNkle96ycDPDHL4s9vs=
|
||||
github.com/redis/go-redis/v9 v9.22.0 h1:laDvpYXTJtZLloinw1fA5Kqd6HAEH2XKxOkG/PDq2F0=
|
||||
github.com/redis/go-redis/v9 v9.22.0/go.mod h1:y2g0Wj8rQvuK0ELM+oxSudcLtC09JScs98I/X9gRWY4=
|
||||
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
|
||||
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
|
||||
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
|
||||
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/tklauser/go-sysconf v0.4.0 h1:7H0uAN+7RkwWRaxhYXDLqa5V3LPrJeV8wmD9dRUgPQU=
|
||||
github.com/tklauser/go-sysconf v0.4.0/go.mod h1:8mTNWyog7H+MpKijp4VmKJAd2bbYQ2zuUwkYRbUArPI=
|
||||
github.com/tklauser/numcpus v0.12.0 h1:NR85qdvHA9pFse3x3weVZ0r0ST8R6l5RHbZrlRaqob4=
|
||||
github.com/tklauser/numcpus v0.12.0/go.mod h1:ABHeXzJnr/qqwguhClkZKT1/8VABcYrsyUiUGobwWJg=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.3.2 h1:zkEASHHyEClGeURfgNT9PJZVfAbs9oEX9QXggwWNJbc=
|
||||
github.com/ugorji/go/codec v1.3.2/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
|
||||
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
|
||||
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
|
||||
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
|
||||
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
|
||||
go.etcd.io/etcd/api/v3 v3.7.1 h1:KJG0/DcWGfe3Y1otDf/fsBf0TSSgpxZ5RO/L8SFt73E=
|
||||
go.etcd.io/etcd/api/v3 v3.7.1/go.mod h1:8bXIpCMeV7E3/XL0Ix123ATn3dB+0V7d9zklHbB0m78=
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.7.1 h1:rKYsj3pRkR0eK3yjT3XOgrhqfmIfj9pzNgxjh7mfFv4=
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.7.1/go.mod h1:cnzZGIUzSfjEwLC6UBVsSXlEK1eepS/JUD7wE6PLRT0=
|
||||
go.etcd.io/etcd/client/v3 v3.7.1 h1:0PEMMC0KuZmVIN+RAbdqfkZ45pYTgKVtmBEbRCvZFUg=
|
||||
go.etcd.io/etcd/client/v3 v3.7.1/go.mod h1:ffNqALa8tRCYhYo1F9oR489y23K39Gz+BSR3ApAGYq0=
|
||||
go.mongodb.org/mongo-driver/v2 v2.8.0 h1:CxWDGQYY8QQwNjAl/aq2sfWakdnWZynnqJ9F4DhHbP8=
|
||||
go.mongodb.org/mongo-driver/v2 v2.8.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
|
||||
go.opentelemetry.io/otel v1.45.0 h1:pdrWmLHofpubmArBv1LgFSv1Z0Ie/ppdZzu+kUN5EeU=
|
||||
go.opentelemetry.io/otel v1.45.0/go.mod h1:XZxIqPapzEYnhNSScF5DIqXhm/rYi0FzCe2XddAwZfQ=
|
||||
go.opentelemetry.io/otel/metric v1.45.0 h1:7Eg1uH7CJ5cXv9is6tnBe1FI6rj1nwUdbFypRm3br/M=
|
||||
go.opentelemetry.io/otel/metric v1.45.0/go.mod h1:HAPbm1nd3p1PmFH7v2dR+6BjXxw+Lq4a2+pndMAm08s=
|
||||
go.opentelemetry.io/otel/sdk v1.45.0 h1:4VVSMgQ83dUgW2aoX5f6JgLvHwIvzcuLnF9lUdCSpCw=
|
||||
go.opentelemetry.io/otel/sdk v1.45.0/go.mod h1:Sr40LgXV7DsKMMJMKOhUWOgMWTfAaqvm2kF0g7ilwuA=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.44.0 h1:3LlKgI+VjbVsjNRFZJZAJ30WjXC5VkNRks6si09iEfI=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.44.0/go.mod h1:5B5pMARnXxKhltooO4xUuCBorl65a4EpnTalObqOigA=
|
||||
go.opentelemetry.io/otel/trace v1.45.0 h1:l/mP6Uv7oNO7/TblbhpbgMidxhq1uO/rPsikOyVhxag=
|
||||
go.opentelemetry.io/otel/trace v1.45.0/go.mod h1:qoJJA2xNMnxRrdISU/kLtfUH2wNeQbiv+jhs/CxI8bc=
|
||||
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
|
||||
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
|
||||
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
|
||||
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
go.uber.org/zap v1.28.0 h1:IZzaP1Fv73/T/pBMLk4VutPl36uNC+OSUh3JLG3FIjo=
|
||||
go.uber.org/zap v1.28.0/go.mod h1:rDLpOi171uODNm/mxFcuYWxDsqWSAVkFdX4XojSKg/Q=
|
||||
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
|
||||
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
|
||||
golang.org/x/arch v0.30.0 h1:sB9h+1gRGa2+LauFSV0tm8bK1J2yo1bx6/Uyi/P6DTU=
|
||||
golang.org/x/arch v0.30.0/go.mod h1:0X+GdSIP+kL5wPmpK7sdkEVTt2XoYP0cSjQSbZBwOi8=
|
||||
golang.org/x/crypto v0.54.0 h1:YLIA59K4fiNzHzjnZt2tUJQjQtUWfWbeHBqKtk3eScw=
|
||||
golang.org/x/crypto v0.54.0/go.mod h1:KWL8ny2AZdGR2cWmzeHrp2azQPGogOv+HeQaVEXC2dk=
|
||||
golang.org/x/net v0.57.0 h1:K5+3DljvIuDG9/Jv9rvyMywYNFCQ9RSUY6OOTTkT+tE=
|
||||
golang.org/x/net v0.57.0/go.mod h1:KpXc8iv+r3XplLAG/f7Jsf9RPszJzdR0f58q9vGOuEU=
|
||||
golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek=
|
||||
golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
|
||||
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/text v0.40.0 h1:Ub2Z6/xjgF1WrYQz2nuITOEegKFtiIy+rieRJ5lHZKs=
|
||||
golang.org/x/text v0.40.0/go.mod h1:hpnzDAfGV753zIKo+wk3u1bVKCGPbrnF7+7LBF/UHVY=
|
||||
gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4=
|
||||
gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260807164820-c8921c73eeea h1:Jifw/kjs/r3B0uszvls/m3c3tmZs2YHGM9C+rvxP9gY=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260807164820-c8921c73eeea/go.mod h1:K/+WGbmBY7aNW1HDw1fJnKYo10i0DkAX6pows00dLig=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260807164820-c8921c73eeea h1:kVhQEPTpKQahD5+JSBTfBB19wcgQTTjAIn45MBqnyHk=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260807164820-c8921c73eeea/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
|
||||
google.golang.org/grpc v1.83.0 h1:JeNZEKJFbQxArAMl+hiytHauacDNqJUllNfmIMmpqnQ=
|
||||
google.golang.org/grpc v1.83.0/go.mod h1:kDyl6SKsiHKt0uylY5gtn5cEjkrIOhQOGDgIc4JGwzQ=
|
||||
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
|
||||
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gorm.io/driver/mysql v1.6.0 h1:eNbLmNTpPpTOVZi8MMxCi2aaIm0ZpInbORNXDwyLGvg=
|
||||
gorm.io/driver/mysql v1.6.0/go.mod h1:D/oCC2GWK3M/dqoLxnOlaNKmXz8WNTfcS9y5ovaSqKo=
|
||||
gorm.io/driver/postgres v1.6.2 h1:BvXQ/cNUg63q5TFNg672DmDcowZSFrNLkkA3Xe6GXq4=
|
||||
gorm.io/driver/postgres v1.6.2/go.mod h1:0c4fQA44XhOklXDkgtuKqysHCycTa5i9e3EIpDGCwXk=
|
||||
gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ=
|
||||
gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8=
|
||||
gorm.io/gorm v1.31.2 h1:3o8FXNo9v9S858gil+3LlZA1LkCOzgb4g5BL64FgaCo=
|
||||
gorm.io/gorm v1.31.2/go.mod h1:XyQVbO2k6YkOis7C2437jSit3SsDK72s7n7rsSHd+Gs=
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -135,13 +136,7 @@ func (l *Licence) VerifyLicence(licName string) bool {
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// 检查机器码是否存在授权列表中
|
||||
func (l *Licence) ValidMachineCode(code string) bool {
|
||||
result := false
|
||||
for _, c := range l.MachineCodes {
|
||||
if c == code {
|
||||
result = true
|
||||
break
|
||||
}
|
||||
}
|
||||
result := slices.Contains(l.MachineCodes, code)
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -265,7 +260,7 @@ func getMacAddrs() []string {
|
||||
if err != nil {
|
||||
return macs
|
||||
}
|
||||
for i := 0; i < len(netfaces); i++ {
|
||||
for i := range netfaces {
|
||||
if (netfaces[i].Flags&net.FlagUp) != 0 && (netfaces[i].Flags&net.FlagLoopback) == 0 {
|
||||
addrs, _ := netfaces[i].Addrs()
|
||||
for _, address := range addrs {
|
||||
|
||||
247
logger/logger.go
247
logger/logger.go
@@ -159,7 +159,7 @@ func (l *Logger) sendToRemote(level, name, out string) {
|
||||
if l.endpoint == "" {
|
||||
return
|
||||
}
|
||||
data := map[string]interface{}{
|
||||
data := map[string]any{
|
||||
"level": level,
|
||||
"name": name,
|
||||
"out": out,
|
||||
@@ -168,136 +168,108 @@ func (l *Logger) sendToRemote(level, name, out string) {
|
||||
utils.HttpPost(l.endpoint, nil, jsonBytes)
|
||||
}
|
||||
|
||||
// Debug 输出调试信息
|
||||
func (l *Logger) Debug(v ...interface{}) {
|
||||
if l.level <= vars.DEBUG {
|
||||
l.checkAndRotateLog()
|
||||
out := fmt.Sprint(v...)
|
||||
func (l *Logger) loggerFor(level vars.LogLevel) (*log.Logger, string) {
|
||||
switch level {
|
||||
case vars.DEBUG:
|
||||
return l.debugLogger, "DEBUG"
|
||||
case vars.INFO:
|
||||
return l.infoLogger, "INFO"
|
||||
case vars.WARN:
|
||||
return l.warnLogger, "WARN"
|
||||
case vars.ERROR:
|
||||
return l.errorLogger, "ERROR"
|
||||
default:
|
||||
return l.fatalLogger, "FATAL"
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) output(level vars.LogLevel, out string, fatal bool) {
|
||||
if !fatal && l.level > level {
|
||||
return
|
||||
}
|
||||
|
||||
_ = l.checkAndRotateLog()
|
||||
logger, levelName := l.loggerFor(level)
|
||||
if fatal {
|
||||
logger = l.fatalLogger
|
||||
levelName = "FATAL"
|
||||
}
|
||||
if l.onRemote {
|
||||
go l.sendToRemote("DEBUG", l.name, out)
|
||||
go l.sendToRemote(levelName, l.name, out)
|
||||
}
|
||||
l.debugLogger.Output(2, out)
|
||||
_ = logger.Output(3, out)
|
||||
|
||||
if fatal {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) outputf(level vars.LogLevel, format string, v ...any) {
|
||||
l.output(level, fmt.Sprintf(format, v...), false)
|
||||
}
|
||||
|
||||
// Debug 输出调试信息
|
||||
func (l *Logger) Debug(v ...any) {
|
||||
l.output(vars.DEBUG, fmt.Sprint(v...), false)
|
||||
}
|
||||
|
||||
// Debugf 格式化输出调试信息
|
||||
func (l *Logger) Debugf(format string, v ...interface{}) {
|
||||
if l.level <= vars.DEBUG {
|
||||
l.checkAndRotateLog()
|
||||
out := fmt.Sprintf(format, v...)
|
||||
if l.onRemote {
|
||||
go l.sendToRemote("DEBUG", l.name, out)
|
||||
}
|
||||
l.debugLogger.Output(2, out)
|
||||
}
|
||||
func (l *Logger) Debugf(format string, v ...any) {
|
||||
l.outputf(vars.DEBUG, format, v...)
|
||||
}
|
||||
|
||||
// Info 输出信息
|
||||
func (l *Logger) Info(v ...interface{}) {
|
||||
if l.level <= vars.INFO {
|
||||
l.checkAndRotateLog()
|
||||
out := fmt.Sprint(v...)
|
||||
if l.onRemote {
|
||||
go l.sendToRemote("INFO", l.name, out)
|
||||
}
|
||||
l.infoLogger.Output(2, out)
|
||||
}
|
||||
func (l *Logger) Info(v ...any) {
|
||||
l.output(vars.INFO, fmt.Sprint(v...), false)
|
||||
}
|
||||
|
||||
// Infof 格式化输出信息
|
||||
func (l *Logger) Infof(format string, v ...interface{}) {
|
||||
if l.level <= vars.INFO {
|
||||
l.checkAndRotateLog()
|
||||
out := fmt.Sprintf(format, v...)
|
||||
if l.onRemote {
|
||||
go l.sendToRemote("INFO", l.name, out)
|
||||
}
|
||||
l.infoLogger.Output(2, out)
|
||||
}
|
||||
func (l *Logger) Infof(format string, v ...any) {
|
||||
l.outputf(vars.INFO, format, v...)
|
||||
}
|
||||
|
||||
// Warn 输出警告
|
||||
func (l *Logger) Warn(v ...interface{}) {
|
||||
if l.level <= vars.WARN {
|
||||
l.checkAndRotateLog()
|
||||
out := fmt.Sprint(v...)
|
||||
if l.onRemote {
|
||||
go l.sendToRemote("WARN", l.name, out)
|
||||
}
|
||||
l.warnLogger.Output(2, out)
|
||||
}
|
||||
func (l *Logger) Warn(v ...any) {
|
||||
l.output(vars.WARN, fmt.Sprint(v...), false)
|
||||
}
|
||||
|
||||
// Warnf 格式化输出警告
|
||||
func (l *Logger) Warnf(format string, v ...interface{}) {
|
||||
if l.level <= vars.WARN {
|
||||
l.checkAndRotateLog()
|
||||
out := fmt.Sprintf(format, v...)
|
||||
if l.onRemote {
|
||||
go l.sendToRemote("WARN", l.name, out)
|
||||
}
|
||||
l.warnLogger.Output(2, out)
|
||||
}
|
||||
func (l *Logger) Warnf(format string, v ...any) {
|
||||
l.outputf(vars.WARN, format, v...)
|
||||
}
|
||||
|
||||
// Error 输出错误
|
||||
func (l *Logger) Error(v ...interface{}) {
|
||||
if l.level <= vars.ERROR {
|
||||
l.checkAndRotateLog()
|
||||
out := fmt.Sprint(v...)
|
||||
if l.onRemote {
|
||||
go l.sendToRemote("ERROR", l.name, out)
|
||||
}
|
||||
l.errorLogger.Output(2, out)
|
||||
}
|
||||
func (l *Logger) Error(v ...any) {
|
||||
l.output(vars.ERROR, fmt.Sprint(v...), false)
|
||||
}
|
||||
|
||||
// Errorf 格式化输出错误
|
||||
func (l *Logger) Errorf(format string, v ...interface{}) {
|
||||
if l.level <= vars.ERROR {
|
||||
l.checkAndRotateLog()
|
||||
out := fmt.Sprintf(format, v...)
|
||||
if l.onRemote {
|
||||
go l.sendToRemote("ERROR", l.name, out)
|
||||
}
|
||||
l.errorLogger.Output(2, out)
|
||||
}
|
||||
func (l *Logger) Errorf(format string, v ...any) {
|
||||
l.outputf(vars.ERROR, format, v...)
|
||||
}
|
||||
|
||||
// Fatal 输出致命错误并退出程序
|
||||
func (l *Logger) Fatal(v ...interface{}) {
|
||||
l.checkAndRotateLog()
|
||||
out := fmt.Sprint(v...)
|
||||
if l.onRemote {
|
||||
go l.sendToRemote("FATAL", l.name, out)
|
||||
}
|
||||
l.fatalLogger.Output(2, out)
|
||||
os.Exit(1)
|
||||
func (l *Logger) Fatal(v ...any) {
|
||||
l.output(vars.ERROR, fmt.Sprint(v...), true)
|
||||
}
|
||||
|
||||
// Fatalf 格式化输出致命错误并退出程序
|
||||
func (l *Logger) Fatalf(format string, v ...interface{}) {
|
||||
l.checkAndRotateLog()
|
||||
out := fmt.Sprintf(format, v...)
|
||||
if l.onRemote {
|
||||
go l.sendToRemote("FATAL", l.name, out)
|
||||
}
|
||||
l.fatalLogger.Output(2, out)
|
||||
os.Exit(1)
|
||||
func (l *Logger) Fatalf(format string, v ...any) {
|
||||
l.output(vars.ERROR, fmt.Sprintf(format, v...), true)
|
||||
}
|
||||
|
||||
// Print 输出信息(兼容标准log包)
|
||||
func (l *Logger) Print(v ...interface{}) {
|
||||
func (l *Logger) Print(v ...any) {
|
||||
l.Info(v...)
|
||||
}
|
||||
|
||||
// Printf 格式化输出信息(兼容标准log包)
|
||||
func (l *Logger) Printf(format string, v ...interface{}) {
|
||||
func (l *Logger) Printf(format string, v ...any) {
|
||||
l.Infof(format, v...)
|
||||
}
|
||||
|
||||
// Println 输出信息并换行(兼容标准log包)
|
||||
func (l *Logger) Println(v ...interface{}) {
|
||||
func (l *Logger) Println(v ...any) {
|
||||
l.Info(v...)
|
||||
}
|
||||
|
||||
@@ -325,95 +297,64 @@ func (l *Logger) Close() error {
|
||||
|
||||
// 全局日志函数(兼容标准log包)
|
||||
|
||||
// Debug 全局调试日志
|
||||
func Debug(v ...interface{}) {
|
||||
// Global logger functions.
|
||||
|
||||
func withGlobalLogger(fn func(*Logger)) {
|
||||
if globalLogger != nil {
|
||||
globalLogger.Debug(v...)
|
||||
fn(globalLogger)
|
||||
}
|
||||
}
|
||||
|
||||
// Debugf 全局调试日志
|
||||
func Debugf(format string, v ...interface{}) {
|
||||
if globalLogger != nil {
|
||||
globalLogger.Debugf(format, v...)
|
||||
}
|
||||
func Debug(v ...any) {
|
||||
withGlobalLogger(func(l *Logger) { l.Debug(v...) })
|
||||
}
|
||||
|
||||
// Info 全局信息日志
|
||||
func Info(v ...interface{}) {
|
||||
if globalLogger != nil {
|
||||
globalLogger.Info(v...)
|
||||
}
|
||||
func Debugf(format string, v ...any) {
|
||||
withGlobalLogger(func(l *Logger) { l.Debugf(format, v...) })
|
||||
}
|
||||
|
||||
// Infof 全局信息日志
|
||||
func Infof(format string, v ...interface{}) {
|
||||
if globalLogger != nil {
|
||||
globalLogger.Infof(format, v...)
|
||||
}
|
||||
func Info(v ...any) {
|
||||
withGlobalLogger(func(l *Logger) { l.Info(v...) })
|
||||
}
|
||||
|
||||
// Warn 全局警告日志
|
||||
func Warn(v ...interface{}) {
|
||||
if globalLogger != nil {
|
||||
globalLogger.Warn(v...)
|
||||
}
|
||||
func Infof(format string, v ...any) {
|
||||
withGlobalLogger(func(l *Logger) { l.Infof(format, v...) })
|
||||
}
|
||||
|
||||
// Warnf 全局警告日志
|
||||
func Warnf(format string, v ...interface{}) {
|
||||
if globalLogger != nil {
|
||||
globalLogger.Warnf(format, v...)
|
||||
}
|
||||
func Warn(v ...any) {
|
||||
withGlobalLogger(func(l *Logger) { l.Warn(v...) })
|
||||
}
|
||||
|
||||
// Error 全局错误日志
|
||||
func Error(v ...interface{}) {
|
||||
if globalLogger != nil {
|
||||
globalLogger.Error(v...)
|
||||
}
|
||||
func Warnf(format string, v ...any) {
|
||||
withGlobalLogger(func(l *Logger) { l.Warnf(format, v...) })
|
||||
}
|
||||
|
||||
// Errorf 全局错误日志
|
||||
func Errorf(format string, v ...interface{}) {
|
||||
if globalLogger != nil {
|
||||
globalLogger.Errorf(format, v...)
|
||||
}
|
||||
func Error(v ...any) {
|
||||
withGlobalLogger(func(l *Logger) { l.Error(v...) })
|
||||
}
|
||||
|
||||
// Fatal 全局致命错误日志
|
||||
func Fatal(v ...interface{}) {
|
||||
if globalLogger != nil {
|
||||
globalLogger.Fatal(v...)
|
||||
}
|
||||
func Errorf(format string, v ...any) {
|
||||
withGlobalLogger(func(l *Logger) { l.Errorf(format, v...) })
|
||||
}
|
||||
|
||||
// Fatalf 全局致命错误日志
|
||||
func Fatalf(format string, v ...interface{}) {
|
||||
if globalLogger != nil {
|
||||
globalLogger.Fatalf(format, v...)
|
||||
}
|
||||
func Fatal(v ...any) {
|
||||
withGlobalLogger(func(l *Logger) { l.Fatal(v...) })
|
||||
}
|
||||
|
||||
// Print 全局打印日志(兼容标准log包)
|
||||
func Print(v ...interface{}) {
|
||||
if globalLogger != nil {
|
||||
globalLogger.Print(v...)
|
||||
}
|
||||
func Fatalf(format string, v ...any) {
|
||||
withGlobalLogger(func(l *Logger) { l.Fatalf(format, v...) })
|
||||
}
|
||||
|
||||
// Printf 全局打印日志(兼容标准log包)
|
||||
func Printf(format string, v ...interface{}) {
|
||||
if globalLogger != nil {
|
||||
globalLogger.Printf(format, v...)
|
||||
}
|
||||
func Print(v ...any) {
|
||||
withGlobalLogger(func(l *Logger) { l.Print(v...) })
|
||||
}
|
||||
|
||||
// Println 全局打印日志(兼容标准log包)
|
||||
func Println(v ...interface{}) {
|
||||
if globalLogger != nil {
|
||||
globalLogger.Println(v...)
|
||||
}
|
||||
func Printf(format string, v ...any) {
|
||||
withGlobalLogger(func(l *Logger) { l.Printf(format, v...) })
|
||||
}
|
||||
|
||||
func Println(v ...any) {
|
||||
withGlobalLogger(func(l *Logger) { l.Println(v...) })
|
||||
}
|
||||
|
||||
// GetLogger 获取全局日志器实例
|
||||
|
||||
@@ -16,25 +16,25 @@ func init() {
|
||||
}
|
||||
|
||||
// record INFO message. Color White
|
||||
func Info(format string, a ...interface{}) {
|
||||
func Info(format string, a ...any) {
|
||||
message := fmt.Sprintf("\033[37m[Info] "+format+"\033[0m\n", a...)
|
||||
logger.Print(message)
|
||||
}
|
||||
|
||||
// record Warn message. Color Orange
|
||||
func Warn(format string, a ...interface{}) {
|
||||
func Warn(format string, a ...any) {
|
||||
message := fmt.Sprintf("\033[33m[Warn] "+format+"\033[0m\n", a...)
|
||||
logger.Print(message)
|
||||
}
|
||||
|
||||
// record Success message. Color Green
|
||||
func Success(format string, a ...interface{}) {
|
||||
func Success(format string, a ...any) {
|
||||
message := fmt.Sprintf("\033[32m[Succ] "+format+"\033[0m\n", a...)
|
||||
logger.Print(message)
|
||||
}
|
||||
|
||||
// record ERROR message. Color Red
|
||||
func Error(format string, a ...interface{}) {
|
||||
func Error(format string, a ...any) {
|
||||
message := fmt.Sprintf("\033[31m[Error] "+format+"\033[0m\n", a...)
|
||||
logger.Print(message)
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ func WeChat_Pkcs7Unpad(data []byte, blockSize int) ([]byte, error) {
|
||||
if n == 0 || n > len(data) {
|
||||
return nil, ErrInvalidPKCS7Padding
|
||||
}
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
if data[len(data)-n+i] != c {
|
||||
return nil, ErrInvalidPKCS7Padding
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ type (
|
||||
MaxIdleConns int `gorm:"column:max_idle_conns;" json:"max_idle_conns"`
|
||||
MaxOpenConns int `gorm:"column:max_open_conns;" json:"max_open_conns"`
|
||||
ConnMaxLifetime time.Duration
|
||||
|
||||
IsAutoMigrate bool `gorm:"column:is_auto_migrate;" json:"is_auto_migrate"`
|
||||
LogStdout bool `gorm:"column:log_stdout;" json:"log_stdout"`
|
||||
Debug bool `gorm:"column:debug;" json:"debug"`
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package utils
|
||||
|
||||
import "slices"
|
||||
|
||||
import "strings"
|
||||
|
||||
// ArrayInString 判断字符串是否存在于字符串切片中
|
||||
@@ -7,24 +9,14 @@ import "strings"
|
||||
// array: 需要查找的字符串切片
|
||||
func ArrayInString(target string, array []string) bool {
|
||||
target = strings.TrimSpace(target)
|
||||
for _, v := range array {
|
||||
if strings.TrimSpace(v) == target {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
return slices.Contains(array, target)
|
||||
}
|
||||
|
||||
// ArrayInInt 判断整数是否存在于整型切片中
|
||||
// target: 待匹配的目标整数
|
||||
// array: 需要查找的整型切片
|
||||
func ArrayInInt(target int, array []int) bool {
|
||||
for _, v := range array {
|
||||
if v == target {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
return slices.Contains(array, target)
|
||||
}
|
||||
|
||||
// ArrayRemoveRepeatString 去除字符串切片中的重复元素(保持原有顺序)
|
||||
|
||||
131
utils/convert.go
131
utils/convert.go
@@ -3,7 +3,10 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -103,7 +106,7 @@ func BinaryToDecimal(bit string) (num int) {
|
||||
fields := strings.Split(bit, "")
|
||||
lens := len(fields)
|
||||
var tempF float64 = 0
|
||||
for i := 0; i < lens; i++ {
|
||||
for i := range lens {
|
||||
floatNum := String2Float64(fields[i])
|
||||
tempF += floatNum * math.Pow(2, float64(lens-i-1))
|
||||
}
|
||||
@@ -111,13 +114,129 @@ func BinaryToDecimal(bit string) (num int) {
|
||||
return
|
||||
}
|
||||
|
||||
// AnyToString 任意类型转字符串
|
||||
// in: 输入值
|
||||
// 返回: 转换后的字符串
|
||||
func AnyToString(in any) (s string) {
|
||||
// AnyToString 将任意值转为可读的字符串:nil 为空串;标量与 []byte 用 strconv;指针会解引用;其余走 fmt.Sprint。
|
||||
func AnyToString(in any) string {
|
||||
for in != nil {
|
||||
rv := reflect.ValueOf(in)
|
||||
if rv.Kind() != reflect.Ptr {
|
||||
break
|
||||
}
|
||||
if rv.IsNil() {
|
||||
return ""
|
||||
}
|
||||
in = rv.Elem().Interface()
|
||||
}
|
||||
if in == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return in.(string)
|
||||
switch v := in.(type) {
|
||||
case string:
|
||||
return v
|
||||
case []byte:
|
||||
return string(v)
|
||||
case bool:
|
||||
return strconv.FormatBool(v)
|
||||
case int:
|
||||
return strconv.Itoa(v)
|
||||
case int8:
|
||||
return strconv.FormatInt(int64(v), 10)
|
||||
case int16:
|
||||
return strconv.FormatInt(int64(v), 10)
|
||||
case int32:
|
||||
return strconv.FormatInt(int64(v), 10)
|
||||
case int64:
|
||||
return strconv.FormatInt(v, 10)
|
||||
case uint:
|
||||
return strconv.FormatUint(uint64(v), 10)
|
||||
case uint8:
|
||||
return strconv.FormatUint(uint64(v), 10)
|
||||
case uint16:
|
||||
return strconv.FormatUint(uint64(v), 10)
|
||||
case uint32:
|
||||
return strconv.FormatUint(uint64(v), 10)
|
||||
case uint64:
|
||||
return strconv.FormatUint(v, 10)
|
||||
case float32:
|
||||
return strconv.FormatFloat(float64(v), 'f', -1, 32)
|
||||
case float64:
|
||||
return strconv.FormatFloat(v, 'f', -1, 64)
|
||||
case json.Number:
|
||||
return string(v)
|
||||
default:
|
||||
if s, ok := in.(fmt.Stringer); ok {
|
||||
return s.String()
|
||||
}
|
||||
return fmt.Sprint(in)
|
||||
}
|
||||
}
|
||||
|
||||
// AnyToInt 将动态类型转为 int(两仓库 internal 中逻辑一致,此处合并分支)。
|
||||
func AnyToInt(v any) int {
|
||||
switch val := v.(type) {
|
||||
case int:
|
||||
return val
|
||||
case int8:
|
||||
return int(val)
|
||||
case int16:
|
||||
return int(val)
|
||||
case int32:
|
||||
return int(val)
|
||||
case int64:
|
||||
return int(val)
|
||||
case uint, uint8, uint16, uint32, uint64:
|
||||
return int(reflect.ValueOf(val).Uint())
|
||||
case float32:
|
||||
return int(val)
|
||||
case float64:
|
||||
return int(val)
|
||||
case string:
|
||||
i, err := strconv.Atoi(val)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return i
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// AnyToFloat64 将动态类型转为 float64(合并 stock 对 int/uint 等分支与 gostock 的 string 分支)。
|
||||
func AnyToFloat64(v any) float64 {
|
||||
switch val := v.(type) {
|
||||
case float64:
|
||||
return val
|
||||
case float32:
|
||||
return float64(val)
|
||||
case int:
|
||||
return float64(val)
|
||||
case int8:
|
||||
return float64(val)
|
||||
case int16:
|
||||
return float64(val)
|
||||
case int32:
|
||||
return float64(val)
|
||||
case int64:
|
||||
return float64(val)
|
||||
case uint:
|
||||
return float64(val)
|
||||
case uint8:
|
||||
return float64(val)
|
||||
case uint16:
|
||||
return float64(val)
|
||||
case uint32:
|
||||
return float64(val)
|
||||
case uint64:
|
||||
return float64(val)
|
||||
case string:
|
||||
return String2Float64(val)
|
||||
case json.Number:
|
||||
f, err := val.Float64()
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return f
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func If(condition bool, trueValue, falseValue interface{}) interface{} {
|
||||
func If(condition bool, trueValue, falseValue any) any {
|
||||
if condition {
|
||||
return trueValue
|
||||
}
|
||||
@@ -21,8 +21,8 @@ func FirstToUpper(str string) string {
|
||||
return strings.ToUpper(str[:1]) + strings.ToLower(str[1:])
|
||||
}
|
||||
|
||||
func ParseParams(in map[string]string) map[string]interface{} {
|
||||
out := make(map[string]interface{})
|
||||
func ParseParams(in map[string]string) map[string]any {
|
||||
out := make(map[string]any)
|
||||
for k, v := range in {
|
||||
fv, err := strconv.ParseFloat(v, 64)
|
||||
if err != nil {
|
||||
|
||||
61
utils/net.go
61
utils/net.go
@@ -56,6 +56,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -202,6 +203,30 @@ func createHTTPClient(timeout time.Duration) *http.Client {
|
||||
}
|
||||
}
|
||||
|
||||
func readHTTPResponse(resp *http.Response) ([]byte, error) {
|
||||
respBytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := checkHTTPStatus(resp, respBytes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return respBytes, nil
|
||||
}
|
||||
|
||||
func checkHTTPStatus(resp *http.Response, body []byte) error {
|
||||
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
|
||||
respBytes := body
|
||||
if respBytes == nil && resp.Body != nil {
|
||||
respBytes, _ = io.ReadAll(resp.Body)
|
||||
}
|
||||
return fmt.Errorf("http status %d: %s", resp.StatusCode, string(respBytes))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// HttpGet 发送HTTP GET请求
|
||||
// url: 请求地址
|
||||
// timeout: 超时时间(可选,默认30秒),可以传入多个,只使用第一个
|
||||
@@ -224,7 +249,7 @@ func HttpGet(url string, timeout ...time.Duration) ([]byte, error) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return io.ReadAll(resp.Body)
|
||||
return readHTTPResponse(resp)
|
||||
}
|
||||
|
||||
// HttpPostJSON 发送HTTP POST JSON请求
|
||||
@@ -232,13 +257,13 @@ func HttpGet(url string, timeout ...time.Duration) ([]byte, error) {
|
||||
// header: 请求头
|
||||
// data: 请求数据(将被序列化为JSON)
|
||||
// 返回: 响应体和错误信息
|
||||
func HttpPostJSON(url string, header map[string]string, data map[string]any) ([]byte, error) {
|
||||
func HttpPostJSON(url string, header map[string]string, data map[string]any, timeout ...time.Duration) ([]byte, error) {
|
||||
jsonBytes, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal json failed: %w", err)
|
||||
}
|
||||
|
||||
return HttpPost(url, header, jsonBytes)
|
||||
return HttpPost(url, header, jsonBytes, timeout...)
|
||||
}
|
||||
|
||||
// HttpPost 发送HTTP POST请求
|
||||
@@ -274,16 +299,7 @@ func HttpPost(url string, header map[string]string, data []byte, timeout ...time
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("http status %d: %s", resp.StatusCode, string(respBytes))
|
||||
}
|
||||
|
||||
return respBytes, nil
|
||||
return readHTTPResponse(resp)
|
||||
}
|
||||
|
||||
// HttpRequest 执行HTTP请求
|
||||
@@ -291,11 +307,14 @@ func HttpPost(url string, header map[string]string, data []byte, timeout ...time
|
||||
// timeout: 超时时间(可选,默认30秒),可以传入多个,只使用第一个
|
||||
// 返回: 响应体和错误信息
|
||||
func HttpRequest(r *http.Request, timeout ...time.Duration) ([]byte, error) {
|
||||
if r == nil {
|
||||
return nil, fmt.Errorf("http request is nil")
|
||||
}
|
||||
|
||||
timeoutDuration := getTimeoutDuration(timeout, DefaultHTTPTimeout)
|
||||
|
||||
// 如果请求还没有设置context,添加一个带超时的context
|
||||
if r.Context() == context.Background() || r.Context() == nil {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeoutDuration)
|
||||
if _, ok := r.Context().Deadline(); !ok {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), timeoutDuration)
|
||||
defer cancel()
|
||||
r = r.WithContext(ctx)
|
||||
}
|
||||
@@ -307,7 +326,7 @@ func HttpRequest(r *http.Request, timeout ...time.Duration) ([]byte, error) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return io.ReadAll(resp.Body)
|
||||
return readHTTPResponse(resp)
|
||||
}
|
||||
|
||||
// DownloadFile 下载文件
|
||||
@@ -333,6 +352,10 @@ func DownloadFile(url, saveTo string, fb func(length, downLen int64), timeout ..
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if err := checkHTTPStatus(resp, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if resp.Body == nil {
|
||||
return fmt.Errorf("response body is nil for %s", url)
|
||||
}
|
||||
@@ -344,6 +367,10 @@ func DownloadFile(url, saveTo string, fb func(length, downLen int64), timeout ..
|
||||
fsize = -1
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(saveTo), 0755); err != nil {
|
||||
return fmt.Errorf("create dir for %s error: %w", saveTo, err)
|
||||
}
|
||||
|
||||
// 创建文件
|
||||
file, err := os.Create(saveTo)
|
||||
if err != nil {
|
||||
|
||||
@@ -341,8 +341,8 @@ func GenerateQRCodeWithLogo(content, logoPath string, size ...int) ([]byte, erro
|
||||
|
||||
// 绘制Logo
|
||||
logoOriginalBounds := logoImage.Bounds()
|
||||
for y := 0; y < logoSize; y++ {
|
||||
for x := 0; x < logoSize; x++ {
|
||||
for y := range logoSize {
|
||||
for x := range logoSize {
|
||||
// 计算原始Logo的对应像素
|
||||
origX := x * logoOriginalBounds.Dx() / logoSize
|
||||
origY := y * logoOriginalBounds.Dy() / logoSize
|
||||
|
||||
@@ -9,7 +9,7 @@ func RandomString(l int) string {
|
||||
str := "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
|
||||
bytes := []byte(str)
|
||||
var result []byte = make([]byte, 0, l)
|
||||
for i := 0; i < l; i++ {
|
||||
for range l {
|
||||
result = append(result, bytes[rand.IntN(len(bytes))])
|
||||
}
|
||||
return string(result)
|
||||
@@ -20,7 +20,7 @@ func RandomPureString(l int) string {
|
||||
str := "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
|
||||
bytes := []byte(str)
|
||||
var result []byte = make([]byte, 0, l)
|
||||
for i := 0; i < l; i++ {
|
||||
for range l {
|
||||
result = append(result, bytes[rand.IntN(len(bytes))])
|
||||
}
|
||||
return string(result)
|
||||
@@ -31,7 +31,7 @@ func RandomPureUpString(l int) string {
|
||||
str := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
bytes := []byte(str)
|
||||
var result []byte = make([]byte, 0, l)
|
||||
for i := 0; i < l; i++ {
|
||||
for range l {
|
||||
result = append(result, bytes[rand.IntN(len(bytes))])
|
||||
}
|
||||
return string(result)
|
||||
@@ -42,7 +42,7 @@ func RandomNumber(l int) string {
|
||||
str := "0123456789"
|
||||
bytes := []byte(str)
|
||||
var result []byte
|
||||
for i := 0; i < l; i++ {
|
||||
for range l {
|
||||
result = append(result, bytes[rand.IntN(len(bytes))])
|
||||
}
|
||||
return string(result)
|
||||
|
||||
@@ -1,36 +1,24 @@
|
||||
package with
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/conf"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"github.com/allegro/bigcache/v3"
|
||||
cache "github.com/patrickmn/go-cache"
|
||||
)
|
||||
|
||||
func Memory(opts *bigcache.Config) (cli *bigcache.BigCache) {
|
||||
func Memory(opts *conf.MemoryCacheConf) *cache.Cache {
|
||||
if opts == nil {
|
||||
opts = &bigcache.Config{
|
||||
Shards: 1024,
|
||||
LifeWindow: 10 * time.Minute,
|
||||
CleanWindow: 5 * time.Minute,
|
||||
MaxEntriesInWindow: 1000 * 10 * 60,
|
||||
MaxEntrySize: 500,
|
||||
Verbose: true,
|
||||
HardMaxCacheSize: 8192,
|
||||
OnRemove: nil,
|
||||
OnRemoveWithReason: nil,
|
||||
opts = &conf.MemoryCacheConf{
|
||||
DefaultExpiration: 60 * 60, // 1 hour
|
||||
CleanupInterval: 24 * 60 * 60, // 1 day
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
cli, err = bigcache.New(context.Background(), *opts)
|
||||
if err != nil {
|
||||
printer.Error("Memory Cache Fatal Error")
|
||||
panic(err)
|
||||
}
|
||||
printer.Success("[BSM - %s] Memory Cache: DefaultExpiration=%d, CleanupInterval=%d", vars.ServiceKey, opts.DefaultExpiration, opts.CleanupInterval)
|
||||
|
||||
return cache.New(time.Duration(opts.DefaultExpiration)*time.Second, time.Duration(opts.CleanupInterval)*time.Second)
|
||||
|
||||
printer.Success("[BSM - %s] Memory Cache: Shards=%d, MaxEntrySize=%d", vars.ServiceKey, opts.Shards, opts.MaxEntrySize)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user