```
feat(database): 新增数据库初始化函数 新增 NewDatabase 函数,支持根据驱动类型初始化 MySQL 或 Postgres 数据库连接。 该函数根据传入的驱动名称自动路由到对应的数据库连接创建逻辑,并提供错误处理。 ```
This commit is contained in:
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