Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f2d8ae26f6 | |||
| dbf68c38c1 | |||
| f70f8d94db | |||
| 257f0a6b6e | |||
| 5e25e8eccc | |||
| 4f584726d6 | |||
| b9d144353e | |||
| 7e7fa16441 | |||
| bc2cb53287 | |||
| cef8b55fba |
76
README.md
76
README.md
@@ -1,27 +1,59 @@
|
|||||||
# 私有仓库:core 设置
|
# BSM-SDK Core
|
||||||
|
|
||||||
|
BSM-SDK Core 是一个企业级后端开发工具包的核心模块,提供了加密解密、配置管理、缓存、数据库访问、中间件等基础功能。
|
||||||
|
|
||||||
|
## 私有仓库设置
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go env -w GOPRIVATE=git.apinb.com/*
|
||||||
|
go env -w GONOPROXY=git.apinb.com/*
|
||||||
|
go env -w GOINSECURE=git.apinb.com/*
|
||||||
|
go env -w GONOSUMDB=git.apinb.com/*
|
||||||
```
|
```
|
||||||
go env -w GOPRIVATE=git.apinb.com/*
|
|
||||||
go env -w GONOPROXY=git.apinb.com/*
|
## 功能模块
|
||||||
go env -w GOINSECURE=git.apinb.com/*
|
|
||||||
go env -w GONOSUMDB=git.apinb.com/*
|
### 加密与解密 (crypto)
|
||||||
|
|
||||||
|
#### GCM加密
|
||||||
|
```go
|
||||||
|
AESGCMEncrypt // GCM 加密
|
||||||
|
AESGCMDecrypt // GCM 解密
|
||||||
```
|
```
|
||||||
# crypto 加密与解密
|
|
||||||
## GCM加密
|
#### CBC加密
|
||||||
|
```go
|
||||||
|
Encrypt // CBC加密
|
||||||
|
Decrypt // CBC解密
|
||||||
```
|
```
|
||||||
AESGCMEncrypt GCM 加密
|
|
||||||
AESGCMDecrypt GCM 解密
|
#### ECB加密
|
||||||
|
```go
|
||||||
|
AesEncryptECB // ECB加密
|
||||||
|
AesDecryptECB // ECB解密
|
||||||
```
|
```
|
||||||
## CBC加密
|
|
||||||
```
|
#### 环境变量检测
|
||||||
Encrypt CBC加密
|
```go
|
||||||
Decrypt CBC解密
|
AesKeyCheck // 秘钥环境变量检测
|
||||||
```
|
|
||||||
## ECB加密
|
|
||||||
```
|
|
||||||
AesEncryptECB ECB加密
|
|
||||||
AesDecryptECB ECB解密
|
|
||||||
```
|
|
||||||
## 环境变量检测
|
|
||||||
```
|
|
||||||
AesKeyCheck 秘钥环境变量检测
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 缓存 (cache)
|
||||||
|
|
||||||
|
支持 Redis 和内存缓存。
|
||||||
|
|
||||||
|
### 数据库 (database)
|
||||||
|
|
||||||
|
支持 MySQL 和 PostgreSQL 数据库连接和管理。
|
||||||
|
|
||||||
|
### 中间件 (middleware)
|
||||||
|
|
||||||
|
提供 CORS、JWT 认证、运行模式控制等中间件功能。
|
||||||
|
|
||||||
|
### 消息队列 (queue)
|
||||||
|
|
||||||
|
集成 NATS 消息队列。
|
||||||
|
|
||||||
|
### 工具类 (utils)
|
||||||
|
|
||||||
|
提供 JSON 处理、时间处理、随机数生成等通用工具函数。
|
||||||
14
cache/mem/mem.go
vendored
14
cache/mem/mem.go
vendored
@@ -1,14 +0,0 @@
|
|||||||
package mem
|
|
||||||
|
|
||||||
import (
|
|
||||||
"git.apinb.com/bsm-sdk/core/vars"
|
|
||||||
"github.com/FishGoddess/cachego"
|
|
||||||
)
|
|
||||||
|
|
||||||
func New() cachego.Cache {
|
|
||||||
return cachego.NewCache(
|
|
||||||
cachego.WithGC(vars.MemGcDuration),
|
|
||||||
cachego.WithShardings(vars.MemShardings),
|
|
||||||
cachego.WithLFU(vars.MemLRUMaxNumber),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
11
conf/new.go
11
conf/new.go
@@ -30,6 +30,12 @@ func New(srvKey string, cfg any) {
|
|||||||
cfp := fmt.Sprintf("%s_%s.yaml", strings.ToLower(srvKey), env.Runtime.Mode)
|
cfp := fmt.Sprintf("%s_%s.yaml", strings.ToLower(srvKey), env.Runtime.Mode)
|
||||||
cfp = filepath.Join(env.Runtime.Prefix, "etc", cfp)
|
cfp = filepath.Join(env.Runtime.Prefix, "etc", cfp)
|
||||||
|
|
||||||
|
// 配置文件不存在则读取Workspace配置文件
|
||||||
|
if !utils.PathExists(cfp) {
|
||||||
|
cfp = fmt.Sprintf("workspace_%s_%s.yaml", strings.ToLower(env.Runtime.Workspace), env.Runtime.Mode)
|
||||||
|
cfp = filepath.Join(env.Runtime.Prefix, "etc", cfp)
|
||||||
|
}
|
||||||
|
|
||||||
print.Info("[BSM - %s] Config File: %s", srvKey, cfp)
|
print.Info("[BSM - %s] Config File: %s", srvKey, cfp)
|
||||||
print.Info("[BSM - %s] Check Configure ...", vars.ServiceKey)
|
print.Info("[BSM - %s] Check Configure ...", vars.ServiceKey)
|
||||||
|
|
||||||
@@ -39,8 +45,11 @@ func New(srvKey string, cfg any) {
|
|||||||
log.Fatalf("ERROR: %v", err)
|
log.Fatalf("ERROR: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 替换环境变量
|
||||||
|
yamlString := os.ExpandEnv(string(yamlFile))
|
||||||
|
|
||||||
// 检查配置文件中是否存在Service和Port字段
|
// 检查配置文件中是否存在Service和Port字段
|
||||||
if !strings.Contains(string(yamlFile), "Service:") {
|
if !strings.Contains(yamlString, "Service:") {
|
||||||
log.Fatalln("ERROR: Service Not Nil", cfp)
|
log.Fatalln("ERROR: Service Not Nil", cfp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -104,31 +104,41 @@ func AesDecryptCBC(cryted string) (b []byte, err error) {
|
|||||||
return orig, nil
|
return orig, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PKCS7Padding 使用PKCS7填充法对明文进行填充
|
||||||
func PKCS7Padding(ciphertext []byte, blocksize int) []byte {
|
func PKCS7Padding(ciphertext []byte, blocksize int) []byte {
|
||||||
padding := blocksize - len(ciphertext)%blocksize
|
padding := blocksize - len(ciphertext)%blocksize
|
||||||
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||||
return append(ciphertext, padtext...)
|
return append(ciphertext, padtext...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 去码
|
// PKCS7UnPadding 去除PKCS7填充
|
||||||
// bug:runtime error: slice bounds out of range [:-22]
|
// bug:runtime error: slice bounds out of range [:-22]
|
||||||
func PKCS7UnPadding(origData []byte, blocksize int) []byte {
|
func PKCS7UnPadding(origData []byte, blocksize int) []byte {
|
||||||
|
// 检查块大小是否有效
|
||||||
if blocksize <= 0 {
|
if blocksize <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查原始数据是否为空
|
||||||
if origData == nil || len(origData) == 0 {
|
if origData == nil || len(origData) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查数据长度是否为块大小的整数倍
|
||||||
if len(origData)%blocksize != 0 {
|
if len(origData)%blocksize != 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
length := len(origData)
|
length := len(origData)
|
||||||
|
// 获取填充字节数
|
||||||
unpadding := int(origData[length-1])
|
unpadding := int(origData[length-1])
|
||||||
|
|
||||||
|
// 检查去填充后数据长度是否有效
|
||||||
if length-unpadding <= 0 {
|
if length-unpadding <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 返回去除填充后的数据
|
||||||
return origData[:(length - unpadding)]
|
return origData[:(length - unpadding)]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
137
database/new.go
Normal file
137
database/new.go
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
// Package database provides database connection and management functionality
|
||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"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"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// MigrateTables holds the tables that need to be auto-migrated on database initialization
|
||||||
|
MigrateTables []any
|
||||||
|
// Init is an optional initialization function that can be executed after database connection is established
|
||||||
|
Init *func() = nil
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewDatabase creates a new database connection based on the provided driver type
|
||||||
|
// It supports both MySQL and PostgreSQL databases
|
||||||
|
func NewDatabase(driver string, dsn []string, options *types.SqlOptions) (db *gorm.DB, err error) {
|
||||||
|
driver = strings.ToLower(driver)
|
||||||
|
switch driver {
|
||||||
|
case "mysql":
|
||||||
|
db, err = NewMysql(dsn, options)
|
||||||
|
case "postgres":
|
||||||
|
db, err = NewPostgres(dsn, options)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unsupported database driver: %s", driver)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute the Init function if it's not nil
|
||||||
|
if Init != nil {
|
||||||
|
(*Init)()
|
||||||
|
}
|
||||||
|
|
||||||
|
return db, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMysql creates a MySQL database service
|
||||||
|
func NewMysql(dsn []string, options *types.SqlOptions) (gormDb *gorm.DB, 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gormDb, err = gorm.Open(mysql.Open(dsn[0]), &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)
|
||||||
|
|
||||||
|
if len(MigrateTables) > 0 {
|
||||||
|
err = gormDb.AutoMigrate(MigrateTables...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return gormDb, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPostgres creates a PostgreSQL database service
|
||||||
|
func NewPostgres(dsn []string, options *types.SqlOptions) (gormDb *gorm.DB, 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gormDb, err = sql.NewPostgreSql(dsn[0], options)
|
||||||
|
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)
|
||||||
|
|
||||||
|
if len(MigrateTables) > 0 {
|
||||||
|
err = gormDb.AutoMigrate(MigrateTables...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -12,7 +12,7 @@ func SetOptions(options *types.SqlOptions) *types.SqlOptions {
|
|||||||
if options == nil {
|
if options == nil {
|
||||||
options = &types.SqlOptions{
|
options = &types.SqlOptions{
|
||||||
MaxIdleConns: vars.SqlOptionMaxIdleConns,
|
MaxIdleConns: vars.SqlOptionMaxIdleConns,
|
||||||
MaxOpenConns: vars.SqlOptionMaxIdleConns,
|
MaxOpenConns: vars.SqlOptionMaxOpenConns,
|
||||||
ConnMaxLifetime: vars.SqlOptionConnMaxLifetime,
|
ConnMaxLifetime: vars.SqlOptionConnMaxLifetime,
|
||||||
LogStdout: false,
|
LogStdout: false,
|
||||||
Debug: false,
|
Debug: false,
|
||||||
@@ -30,7 +30,7 @@ func NewPostgreSql(dsn string, options *types.SqlOptions) (*gorm.DB, error) {
|
|||||||
if options == nil {
|
if options == nil {
|
||||||
options = &types.SqlOptions{
|
options = &types.SqlOptions{
|
||||||
MaxIdleConns: vars.SqlOptionMaxIdleConns,
|
MaxIdleConns: vars.SqlOptionMaxIdleConns,
|
||||||
MaxOpenConns: vars.SqlOptionMaxIdleConns,
|
MaxOpenConns: vars.SqlOptionMaxOpenConns,
|
||||||
ConnMaxLifetime: vars.SqlOptionConnMaxLifetime,
|
ConnMaxLifetime: vars.SqlOptionConnMaxLifetime,
|
||||||
LogStdout: false,
|
LogStdout: false,
|
||||||
Debug: true,
|
Debug: true,
|
||||||
@@ -56,7 +56,11 @@ func NewPostgreSql(dsn string, options *types.SqlOptions) (*gorm.DB, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 获取通用数据库对象 sql.DB ,然后使用其提供的功能
|
// 获取通用数据库对象 sql.DB ,然后使用其提供的功能
|
||||||
sqlDB, _ := gormDb.DB()
|
sqlDB, err := gormDb.DB()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
// SetMaxIdleConns 用于设置连接池中空闲连接的最大数量。
|
// SetMaxIdleConns 用于设置连接池中空闲连接的最大数量。
|
||||||
sqlDB.SetMaxIdleConns(options.MaxIdleConns)
|
sqlDB.SetMaxIdleConns(options.MaxIdleConns)
|
||||||
// SetMaxOpenConns 设置打开数据库连接的最大数量。
|
// SetMaxOpenConns 设置打开数据库连接的最大数量。
|
||||||
@@ -65,4 +69,4 @@ func NewPostgreSql(dsn string, options *types.SqlOptions) (*gorm.DB, error) {
|
|||||||
sqlDB.SetConnMaxLifetime(options.ConnMaxLifetime)
|
sqlDB.SetConnMaxLifetime(options.ConnMaxLifetime)
|
||||||
|
|
||||||
return gormDb, nil
|
return gormDb, nil
|
||||||
}
|
}
|
||||||
@@ -94,7 +94,11 @@ func (s *ServiceRegister) Close() error {
|
|||||||
|
|
||||||
func (s *ServiceRegister) SetAnonymous(key string, urls []string) {
|
func (s *ServiceRegister) SetAnonymous(key string, urls []string) {
|
||||||
// remove reppeat, clear service all anonymous uri.
|
// remove reppeat, clear service all anonymous uri.
|
||||||
anonymous, _ := s.cli.Get(context.Background(), key)
|
anonymous, err := s.cli.Get(context.Background(), key)
|
||||||
|
if err != nil {
|
||||||
|
print.Error("[BSM Register] Get Anonymous Fail: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var as []string
|
var as []string
|
||||||
if len(anonymous.Kvs) > 0 {
|
if len(anonymous.Kvs) > 0 {
|
||||||
@@ -106,7 +110,7 @@ func (s *ServiceRegister) SetAnonymous(key string, urls []string) {
|
|||||||
newAnonymous := strings.Join(as, ",")
|
newAnonymous := strings.Join(as, ",")
|
||||||
|
|
||||||
// put anonymous to etcd
|
// put anonymous to etcd
|
||||||
_, err := s.cli.Put(context.Background(), key, newAnonymous)
|
_, err = s.cli.Put(context.Background(), key, newAnonymous)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
print.Error("[BSM Register] Anonymous Fail.")
|
print.Error("[BSM Register] Anonymous Fail.")
|
||||||
|
|||||||
@@ -6,36 +6,48 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 字符串转Int
|
// String2Int 字符串转Int
|
||||||
//
|
//
|
||||||
// intStr:数字的字符串
|
// intStr:数字的字符串
|
||||||
func String2Int(intStr string) (intNum int) {
|
func String2Int(intStr string) (intNum int) {
|
||||||
intNum, _ = strconv.Atoi(intStr)
|
intNum, err := strconv.Atoi(intStr)
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 字符串转Int64
|
// String2Int64 字符串转Int64
|
||||||
//
|
//
|
||||||
// intStr:数字的字符串
|
// intStr:数字的字符串
|
||||||
func String2Int64(intStr string) (int64Num int64) {
|
func String2Int64(intStr string) (int64Num int64) {
|
||||||
intNum, _ := strconv.Atoi(intStr)
|
intNum, err := strconv.Atoi(intStr)
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
int64Num = int64(intNum)
|
int64Num = int64(intNum)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 字符串转Float64
|
// String2Float64 字符串转Float64
|
||||||
//
|
//
|
||||||
// floatStr:小数点数字的字符串
|
// floatStr:小数点数字的字符串
|
||||||
func String2Float64(floatStr string) (floatNum float64) {
|
func String2Float64(floatStr string) (floatNum float64) {
|
||||||
floatNum, _ = strconv.ParseFloat(floatStr, 64)
|
floatNum, err := strconv.ParseFloat(floatStr, 64)
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 字符串转Float32
|
// String2Float32 字符串转Float32
|
||||||
//
|
//
|
||||||
// floatStr:小数点数字的字符串
|
// floatStr:小数点数字的字符串
|
||||||
func String2Float32(floatStr string) (floatNum float32) {
|
func String2Float32(floatStr string) (floatNum float32) {
|
||||||
floatNum64, _ := strconv.ParseFloat(floatStr, 32)
|
floatNum64, err := strconv.ParseFloat(floatStr, 32)
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
floatNum = float32(floatNum64)
|
floatNum = float32(floatNum64)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
12
utils/dir.go
12
utils/dir.go
@@ -25,11 +25,17 @@ func CreateDir(dirName string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetRunPath() string {
|
func GetRunPath() string {
|
||||||
path, _ := os.Executable()
|
path, err := os.Executable()
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCurrentPath() string {
|
func GetCurrentPath() string {
|
||||||
path, _ := os.Getwd()
|
path, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,14 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
const (
|
||||||
|
B = 1
|
||||||
|
KB = 1024 * B
|
||||||
|
MB = 1024 * KB
|
||||||
|
GB = 1024 * MB
|
||||||
|
)
|
||||||
|
|
||||||
// 将字符串写入文件
|
// 将字符串写入文件
|
||||||
func StringToFile(path, content string) error {
|
func StringToFile(path, content string) error {
|
||||||
startF, err := os.Create(path)
|
startF, err := os.Create(path)
|
||||||
@@ -48,6 +56,27 @@ func CopyFile(src, dst string) (int64, error) {
|
|||||||
return nBytes, err
|
return nBytes, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func FileSize(fp string) string {
|
||||||
|
fileInfo, err := os.Stat(fp)
|
||||||
|
if err != nil {
|
||||||
|
return "0 B"
|
||||||
|
}
|
||||||
|
bytes := fileInfo.Size()
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case bytes >= GB:
|
||||||
|
return fmt.Sprintf("%.2f GB", float64(bytes)/float64(GB))
|
||||||
|
case bytes >= MB:
|
||||||
|
return fmt.Sprintf("%.2f MB", float64(bytes)/float64(MB))
|
||||||
|
case bytes >= KB:
|
||||||
|
return fmt.Sprintf("%.2f KB", float64(bytes)/float64(KB))
|
||||||
|
default:
|
||||||
|
return fmt.Sprintf("%d B", bytes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// 递归遍历文件夹
|
// 递归遍历文件夹
|
||||||
// rootDir: 文件夹根目录
|
// rootDir: 文件夹根目录
|
||||||
// s: 存储文件名的切片
|
// s: 存储文件名的切片
|
||||||
|
|||||||
27
with/databases.go
Normal file
27
with/databases.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package with
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.apinb.com/bsm-sdk/core/conf"
|
||||||
|
"git.apinb.com/bsm-sdk/core/database"
|
||||||
|
"git.apinb.com/bsm-sdk/core/print"
|
||||||
|
"git.apinb.com/bsm-sdk/core/types"
|
||||||
|
"git.apinb.com/bsm-sdk/core/vars"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Databases(cfg *conf.DBConf, db *gorm.DB, opts *types.SqlOptions) {
|
||||||
|
if cfg == nil || len(cfg.Source) == 0 {
|
||||||
|
panic("No Database Source Found !")
|
||||||
|
}
|
||||||
|
|
||||||
|
// print inform.
|
||||||
|
print.Info("[BSM - %s] Databases: %v", vars.ServiceKey, cfg)
|
||||||
|
|
||||||
|
var err error
|
||||||
|
db, err = database.NewDatabase(cfg.Driver, cfg.Source, opts)
|
||||||
|
if err != nil {
|
||||||
|
print.Error("Database Init Failed !")
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
54
with/etcd.go
Normal file
54
with/etcd.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package with
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.apinb.com/bsm-sdk/core/conf"
|
||||||
|
"git.apinb.com/bsm-sdk/core/errcode"
|
||||||
|
"git.apinb.com/bsm-sdk/core/print"
|
||||||
|
"git.apinb.com/bsm-sdk/core/vars"
|
||||||
|
"go.etcd.io/etcd/client/pkg/v3/transport"
|
||||||
|
clientv3 "go.etcd.io/etcd/client/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Etcd(cfg *conf.EtcdConf, cli *clientv3.Client) {
|
||||||
|
if cfg == nil || len(cfg.Endpoints) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
etcdCfg := clientv3.Config{
|
||||||
|
Endpoints: cfg.Endpoints,
|
||||||
|
DialTimeout: 5 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Passwd != nil {
|
||||||
|
etcdCfg.Username = cfg.Passwd.Account
|
||||||
|
etcdCfg.Password = cfg.Passwd.Password
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.TLS != nil {
|
||||||
|
tlsInfo := transport.TLSInfo{
|
||||||
|
TrustedCAFile: cfg.TLS.CaFile,
|
||||||
|
CertFile: cfg.TLS.CertFile,
|
||||||
|
KeyFile: cfg.TLS.KeyFile,
|
||||||
|
}
|
||||||
|
tlsConfig, err := tlsInfo.ClientConfig()
|
||||||
|
if err != nil {
|
||||||
|
print.Error(errcode.ErrEtcd.Error())
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
etcdCfg.TLS = tlsConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
cli, err = clientv3.New(etcdCfg)
|
||||||
|
if err != nil {
|
||||||
|
print.Error(errcode.ErrEtcd.Error())
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// print inform.
|
||||||
|
print.Info("[BSM - %s] Service Center: %v", vars.ServiceKey, cfg.Endpoints)
|
||||||
|
return
|
||||||
|
}
|
||||||
35
with/memory.go
Normal file
35
with/memory.go
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package with
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.apinb.com/bsm-sdk/core/print"
|
||||||
|
"git.apinb.com/bsm-sdk/core/vars"
|
||||||
|
"github.com/allegro/bigcache/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Memory(cli *bigcache.BigCache, opts *bigcache.Config) {
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
cli, err = bigcache.New(context.Background(), *opts)
|
||||||
|
if err != nil {
|
||||||
|
print.Error("Memory Cache Fatal Error")
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
print.Success("[BSM - %s] Memory Cache: Shards=%d, MaxEntrySize=%d", vars.ServiceKey, opts.Shards, opts.MaxEntrySize)
|
||||||
|
}
|
||||||
16
with/redis.go
Normal file
16
with/redis.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package with
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.apinb.com/bsm-sdk/core/cache/redis"
|
||||||
|
"git.apinb.com/bsm-sdk/core/print"
|
||||||
|
"git.apinb.com/bsm-sdk/core/vars"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RedisCache(cfg string, cli *redis.RedisClient) {
|
||||||
|
if cfg != "" {
|
||||||
|
cli = redis.New(cfg, vars.ServiceKey)
|
||||||
|
|
||||||
|
// print inform.
|
||||||
|
print.Info("[BSM - %s] Cache: %s, DBIndex: %d", vars.ServiceKey, cfg, cli.DB)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user