Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bc2cb53287 | |||
| cef8b55fba |
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
88
database/new.go
Normal file
88
database/new.go
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"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 []any
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewMysql 创建MySQL数据库服务
|
||||||
|
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.SqlOptionMaxIdleConns,
|
||||||
|
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, _ := gormDb.DB()
|
||||||
|
// 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 创建PostgreSQL数据库服务
|
||||||
|
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.SqlOptionMaxIdleConns,
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(MigrateTables) > 0 {
|
||||||
|
err = gormDb.AutoMigrate(MigrateTables...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -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: 存储文件名的切片
|
||||||
|
|||||||
Reference in New Issue
Block a user