service/library/configcenter/configcenter.go

203 lines
8.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package configcenter
import (
"fmt"
"io/ioutil"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
// 默认配置
type DefaultConfig struct {
Log LoggerConfig `json:"log" yaml:"log"` // 日志配置
App AppConfig `json:"app" yaml:"app"` // 服务配置
}
// 日志配置
type LoggerConfig struct {
StdoutEnable bool `json:"stdout_enable" yaml:"stdout_enable"` // 是否允许控制台输出
StdoutLevel string `json:"stdout_level" yaml:"stdout_level"` // 控制台输出日志等级
FileEnable bool `json:"file_enable" yaml:"file_enable"` // 是否允许写文件
FileLevel string `json:"file_level" yaml:"file_level"` // 文件输出日志等级
FileAbsPath string `json:"file_abs_path" yaml:"file_abs_path"` // 日志文件绝对路径
}
// 服务配置
type AppConfig struct {
AppName string `json:"app_name" yaml:"app_name"` // 服务名
Ip string `json:"ip" yaml:"ip"` // ip
Port int `json:"port" yaml:"port"` // 端口
}
// mongo配置
type MongoConfig struct {
Uri string `json:"uri" yaml:"uri"` // 实例地址
Username string `json:"username" yaml:"username"` // 用户名
Password string `json:"password" yaml:"password"` // 密码
MaxPoolSize uint64 `json:"max_pool_size" yaml:"max_pool_size"` // 最大连接池
ConnectionTimeoutMs int64 `json:"connection_timeout_ms" yaml:"connection_timeout_ms"` // 连接超时ms
SocketTimeoutMs int64 `json:"socket_timeout_ms" yaml:"socket_timeout_ms"` // socket连接超时ms
}
// mysql配置
type MysqlConfig struct {
Uri string `json:"uri" yaml:"uri"` // 实例地址ip+端口
Username string `json:"username" yaml:"username"` // 用户名
Password string `json:"password" yaml:"password"` // 密码
TimeoutS int `json:"timeout" yaml:"timeout"` // 超时,秒
ReadTimeoutS int `json:"read_timeout_s" yaml:"read_timeout_s"` // 读超时,秒
WriteTimeoutS int `json:"write_timeout_s" yaml:"write_timeout_s"` // 写超时,秒
}
// redis配置
type RedisConfig struct {
Uri string `json:"uri" yaml:"uri"` // 实例地址ip+端口
Password string `json:"password" yaml:"password"` // 鉴权密码
MaxActive int `json:"max_active" yaml:"max_active"` // 最大活动连接数值为0时表示不限制
MaxIdle int `json:"max_idle" yaml:"max_idle"` // 最大空闲连接数
IdleTimeout int `json:"idle_timeout" yaml:"idle_timeout"` // 空闲连接的超时时间超过该时间则关闭连接。单位为秒。默认值是5分钟。值为0时表示不关闭空闲连接。此值应该总是大于redis服务的超时时间。
Prefix string `json:"prefix" yaml:"prefix"` // key的前缀
}
// 微信支付客户端配置
type WxpayClientConfig struct {
MchId string `json:"mchid" yaml:"mchid"` // 商户id
AppId string `json:"appid" yaml:"appid"` // appid
SerialNo string `json:"serial_no" yaml:"serial_no"` // 商户API证书的证书序列号
ApiV3Key string `json:"apiv3_key" yaml:"apiv3_key"` // APIv3Key商户平台获取
PrivateKeyPath string `json:"private_key_path" yaml:"private_key_path"` // 商户API证书下载后私钥 apiclient_key.pem 读取后的字符串内容
NotifyUrl string `json:"notify_url" yaml:"notify_url"` // 回调地址
AppSecret string `json:"app_secret" yaml:"app_secret"` //
}
// 账号相关验密配置
type CryptoConfig struct {
*AESConfig `json:"aes" yaml:"aes"` // AES
*RSAConfig `json:"rsa" yaml:"rsa"` // RSA
*TokenConfig `json:"token" yaml:"token"` // token签名
*SHA256Config `json:"sha256" yaml:"sha256"` // SHA256
*HygAESConfig `json:"hyg_aes" yml:"hyg_aes"` // 慧用工的AES
*HygRSAConfig `json:"hyg_rsa" yaml:"hyg_rsa"` // 慧用工的RSA
}
// 支付宝客户端配置
type AlipayClientConfig struct {
Appid string `json:"appid" yaml:"appid"` // 商户id
PrivateKey string `json:"private_key" yaml:"private_key"` // 私钥
NotifyUrl string `json:"notify_url" yaml:"notify_url"` // 回调地址
AppCertPath string `json:"app_cert_path" yaml:"app_cert_path"`
AlipayRootCertPath string `json:"alipay_root_cert_path" yaml:"alipay_root_cert_path"`
AlipayPublicCertPath string `json:"alipay_public_cert_path" yaml:"alipay_public_cert_path"`
}
// Apollo配置
type ApolloConfig struct {
AppID string `json:"app_id" yaml:"app_id"`
Cluster string `json:"cluster" yaml:"cluster"`
IP string `json:"ip" yaml:"ip"`
NamespaceName string `json:"namespace_name" yaml:"namespace_name"`
Secret string `json:"secret" yaml:"secret"`
IsBackUpConfig bool `json:"is_back_up_config" yaml:"is_back_up_config"`
SyncServerTimeout int `json:"sync_server_timeout" yaml:"sync_server_timeout"`
}
// 媒体服务配置
type MediaConfig struct {
AccessKey string `json:"access_key" yaml:"access_key"`
AccessSecret string `json:"access_secret" yaml:"access_secret"`
Endpoint string `json:"endpoint" yaml:"endpoint"`
Bucket string `json:"bucket" yaml:"bucket"`
CallbackUrl string `json:"callback_url" yaml:"callback_url"`
ExpireTime int64 `json:"expire_time" yaml:"expire_time"`
}
// 阿里云短信服务配置
type DysmsapiConfig struct {
AccessKeyId string `json:"access_key_id" yaml:"access_key_id"`
AccessKeySecret string `json:"access_key_secret" yaml:"access_key_secret"`
SignName string `json:"sign_name" yaml:"sign_name"`
TemplateCode string `json:"template_code" yaml:"template_code"`
}
// 图片内容审核服务配置
type ImageAuditConfig struct {
AccessKeyId string `json:"access_key_id" yaml:"access_key_id"`
AccessKeySecret string `json:"access_key_secret" yaml:"access_key_secret"`
Scenes string `json:"scenes" yaml:"scenes"`
TaskBufferSize int `json:"task_buffer_size" yaml:"task_buffer_size"`
TaskPacketSize int `json:"task_packet_size" yaml:"task_packet_size"`
}
// 文字内容审核服务配置
type TextAuditConfig struct {
Labels string `json:"labels" yaml:"labels"`
TaskBufferSize int `json:"task_buffer_size" yaml:"task_buffer_size"`
TaskPacketSize int `json:"task_packet_size" yaml:"task_packet_size"`
}
// 视频内容审核服务配置
type VideoModerationConfig struct {
AccessKeyId string `json:"access_key_id" yaml:"access_key_id"`
AccessKeySecret string `json:"access_key_secret" yaml:"access_key_secret"`
NotifyUrl string `json:"notify_url" yaml:"notify_url"`
AliAcctId string `json:"ali_acct_id" yaml:"ali_acct_id"`
Seed string `json:"seed" bson:"seed"`
}
// 服务器信息配置
type ServerInfoConfig struct {
FileServerDomainName string `json:"file_server_domain_name" yaml:"file_server_domain_name"`
MainServerIps string `json:"main_server_ips" yaml:"main_server_ips"`
MainServerPorts string `json:"main_server_ports" yaml:"main_server_ports"`
MainServerUsers string `json:"main_server_users" yaml:"main_server_users"`
MainServerPasswords string `json:"main_server_passwords" yaml:"main_server_passwords"`
}
// 钉钉机器人信息配置
type DingTalkRobotConfig struct {
AccessToken string `json:"access_token" yaml:"access_token"`
Secret string `json:"secret" yaml:"secret"`
}
// xxl-job服务配置
type XxlJobConfig struct {
ServerAddr string `json:"server_addr" yaml:"server_addr"`
AccessToken string `json:"access_token" yaml:"access_token"`
ExecutorIp string `json:"executor_ip" yaml:"executor_ip"`
ExecutorPort string `json:"executor_port" yaml:"executor_port"`
RegistryKey string `json:"registry_key" yaml:"registry_key"`
LogPath string `json:"log_path" yaml:"log_path"`
}
// elasticsearch服务配置
type ElasticSearchConfig struct {
Uri string `json:"uri" yaml:"uri"` // 实例地址
Username string `json:"username" yaml:"username"` // 用户名
Password string `json:"password" yaml:"password"` // 密码
Sniff bool `json:"sniff" yaml:"sniff"` // sniffer
}
// 服务发现
type ConsulServiceDiscovery struct {
Host string `json:"host" yaml:"host"`
ServiceName string `json:"service_name" yaml:"service_name"`
}
func LoadConfig(configFilePath string, cfg interface{}) error {
cfgStr, err := ioutil.ReadFile(configFilePath)
if err != nil {
msg := fmt.Sprintf("LoadConfig fail, path: %v, err: %v", configFilePath, err)
err = errors.New(msg)
return err
}
err = yaml.Unmarshal(cfgStr, cfg)
if err != nil {
msg := fmt.Sprintf("yaml Unmarshal fail, path: %v, err: %v", configFilePath, err)
err = errors.New(msg)
return err
}
return nil
}