Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7e7fa16441 |
@@ -1,6 +1,9 @@
|
||||
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"
|
||||
@@ -12,6 +15,24 @@ var (
|
||||
MigrateTables []any
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// NewMysql 创建MySQL数据库服务
|
||||
func NewMysql(dsn []string, options *types.SqlOptions) (gormDb *gorm.DB, err error) {
|
||||
//set connection default val.
|
||||
|
||||
16
with/cache.go
Normal file
16
with/cache.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)
|
||||
}
|
||||
}
|
||||
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 {
|
||||
panic("Etcd endpoints is Empty!")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user