refactor: reuse global aliyun moderation config
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package immoderation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/model/entity"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
)
|
||||
|
||||
const (
|
||||
AliyunConfigGroup = "im_third_party"
|
||||
AliyunAccessKeyIDKey = "aliyun_access_key_id"
|
||||
AliyunAccessKeySecretKey = "aliyun_access_key_secret"
|
||||
AliyunGreenRegionIDKey = "aliyun_green_region_id"
|
||||
AliyunGreenEndpointKey = "aliyun_green_endpoint"
|
||||
DefaultAliyunGreenRegionID = "cn-shanghai"
|
||||
DefaultAliyunGreenEndpoint = "green-cip.cn-shanghai.aliyuncs.com"
|
||||
)
|
||||
|
||||
type AliyunGlobalConfig struct {
|
||||
AccessKeyID string
|
||||
AccessKeySecret string
|
||||
RegionID string
|
||||
Endpoint string
|
||||
}
|
||||
|
||||
func LoadAliyunGlobalConfig(ctx context.Context) (config AliyunGlobalConfig, err error) {
|
||||
columns := dao.SysConfig.Columns()
|
||||
keys := []string{
|
||||
AliyunAccessKeyIDKey,
|
||||
AliyunAccessKeySecretKey,
|
||||
AliyunGreenRegionIDKey,
|
||||
AliyunGreenEndpointKey,
|
||||
}
|
||||
var rows []*entity.SysConfig
|
||||
if err = dao.SysConfig.Ctx(ctx).
|
||||
Fields(columns.Key, columns.Value).
|
||||
Where(columns.Group, AliyunConfigGroup).
|
||||
Where(columns.Status, 1).
|
||||
WhereIn(columns.Key, keys).
|
||||
Scan(&rows); err != nil {
|
||||
return config, gerror.Wrap(err, "获取阿里云内容审核公共配置失败")
|
||||
}
|
||||
values := make(map[string]string, len(rows))
|
||||
for _, row := range rows {
|
||||
values[row.Key] = strings.TrimSpace(row.Value)
|
||||
}
|
||||
return AliyunGlobalConfig{
|
||||
AccessKeyID: values[AliyunAccessKeyIDKey],
|
||||
AccessKeySecret: values[AliyunAccessKeySecretKey],
|
||||
RegionID: firstValue(values[AliyunGreenRegionIDKey], DefaultAliyunGreenRegionID),
|
||||
Endpoint: firstValue(values[AliyunGreenEndpointKey], DefaultAliyunGreenEndpoint),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func firstValue(value, fallback string) string {
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return fallback
|
||||
}
|
||||
return strings.TrimSpace(value)
|
||||
}
|
||||
Reference in New Issue
Block a user