feat: import services and standardize Go 1.26.5
This commit is contained in:
31
apps/base/sender/internal/impl/impl.go
Normal file
31
apps/base/sender/internal/impl/impl.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package impl
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-apps/sender/internal/config"
|
||||
"git.apinb.com/bsm-sdk/core/cache/redis"
|
||||
"git.apinb.com/bsm-sdk/core/with"
|
||||
cache "github.com/patrickmn/go-cache"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var (
|
||||
RedisService *redis.RedisClient // Redis 缓存服务客户端
|
||||
EtcdService *clientv3.Client // Etcd 客户端
|
||||
DBService *gorm.DB // 数据库服务
|
||||
MemorySerice *cache.Cache // 内存缓存服务(BigCache)
|
||||
)
|
||||
|
||||
// NewImpl 初始化所有依赖服务(内存、Redis、数据库、Etcd)
|
||||
func NewImpl() {
|
||||
// 初始化内存缓存服务
|
||||
MemorySerice = with.Memory(nil)
|
||||
// 初始化 Redis 缓存服务
|
||||
RedisService = with.RedisCache(config.Spec.Cache)
|
||||
// 初始化数据库服务
|
||||
DBService = with.Databases(config.Spec.Databases, nil)
|
||||
// 初始化 Etcd 客户端
|
||||
EtcdService = with.Etcd(config.Spec.Etcd)
|
||||
// 初始化服务提供商
|
||||
withProvider()
|
||||
}
|
||||
92
apps/base/sender/internal/impl/provider.go
Normal file
92
apps/base/sender/internal/impl/provider.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package impl
|
||||
|
||||
import (
|
||||
"net/smtp"
|
||||
"strings"
|
||||
|
||||
"git.apinb.com/bsm-apps/sender/internal/config"
|
||||
AliYunClient "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
||||
dysmsapi "github.com/alibabacloud-go/dysmsapi-20180501/v2/client"
|
||||
"github.com/aliyun/credentials-go/credentials"
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
|
||||
TencentCloud "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20210111"
|
||||
)
|
||||
|
||||
var (
|
||||
Provider *ProviderClient
|
||||
)
|
||||
|
||||
type ProviderClient struct {
|
||||
Aliyun *dysmsapi.Client
|
||||
Google *smtp.Client
|
||||
QQ *smtp.Client
|
||||
Tencent *TencentCloud.Client
|
||||
}
|
||||
|
||||
func (p *ProviderClient) init() {
|
||||
Provider = &ProviderClient{}
|
||||
}
|
||||
|
||||
func withProvider() {
|
||||
Provider.init()
|
||||
for key, conf := range config.Spec.SMTP {
|
||||
switch strings.ToLower(key) {
|
||||
case "google":
|
||||
Provider.Google = NewSMTP(conf)
|
||||
case "qq":
|
||||
Provider.QQ = NewSMTP(conf)
|
||||
}
|
||||
}
|
||||
|
||||
for key, conf := range config.Spec.SMS {
|
||||
switch strings.ToLower(key) {
|
||||
case "aliyun":
|
||||
Provider.Aliyun = NewAliyun(conf)
|
||||
case "tencent":
|
||||
Provider.Tencent = NewTencent(conf)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewSMTP(conf *config.SmtpConf) *smtp.Client {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewAliyun(conf *config.SmsConf) *dysmsapi.Client {
|
||||
|
||||
config := new(credentials.Config).
|
||||
SetType("access_key").
|
||||
SetAccessKeyId(conf.AccessKeyId).
|
||||
SetAccessKeySecret(conf.AccessKeySecret)
|
||||
|
||||
akCredential, err := credentials.NewCredential(config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
cfg := &AliYunClient.Config{
|
||||
Endpoint: &conf.Endpoint,
|
||||
Credential: akCredential,
|
||||
}
|
||||
|
||||
client, err := dysmsapi.NewClient(cfg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
func NewTencent(conf *config.SmsConf) *TencentCloud.Client {
|
||||
credential := common.NewCredential(conf.AccessKeyId, conf.AccessKeySecret)
|
||||
clientProfile := profile.NewClientProfile()
|
||||
clientProfile.HttpProfile.Endpoint = conf.Endpoint
|
||||
client, err := TencentCloud.NewClient(credential, conf.Region, clientProfile)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
Reference in New Issue
Block a user