199 lines
6.2 KiB
Go
Executable File
199 lines
6.2 KiB
Go
Executable File
package config
|
||
|
||
import (
|
||
"fmt"
|
||
"github.com/fsnotify/fsnotify"
|
||
"github.com/spf13/viper"
|
||
"strings"
|
||
"sync"
|
||
)
|
||
|
||
// Conf 定义全局的变量
|
||
var Conf = new(SrvConfig)
|
||
|
||
// var ConfLock = new(sync.RWMutex) // 配置读写锁,高并发情况下保证获取的是最新的配置
|
||
|
||
// viper.GetXxx()读取的方式
|
||
// 注意:
|
||
// Viper使用的是 `mapstructure`
|
||
|
||
// SrvConfig 服务配置
|
||
type SrvConfig struct {
|
||
Name string `mapstructure:"name"`
|
||
Mode string `mapstructure:"mode"`
|
||
Version string `mapstructure:"version"`
|
||
|
||
IP string `mapstructure:"ip"`
|
||
Port int `mapstructure:"port"`
|
||
|
||
Pid string `mapstructure:"pid"`
|
||
|
||
*LogConfig `mapstructure:"log"`
|
||
*OCRConfig `mapstructure:"ocr"`
|
||
*MinIoConfig `mapstructure:"minio"`
|
||
*OpenCVConfig `mapstructure:"opencv"`
|
||
*MySQLConfig `mapstructure:"mysql"`
|
||
*SnowflakeConfig `mapstructure:"snowflake"`
|
||
*GrpcConfig `mapstructure:"grpc"`
|
||
*ApolloConfig `mapstructure:"apollo"`
|
||
*Registrar `mapstructure:"registrar"`
|
||
*RcloneConfig `mapstructure:"rclone"`
|
||
*OpenAIConfig `mapstructure:"openai"`
|
||
*TemporalConfig `mapstructure:"temporal"` // Temporal配置
|
||
}
|
||
|
||
type LogConfig struct {
|
||
Level string `mapstructure:"level"`
|
||
Filename string `mapstructure:"filename"`
|
||
FilenameErr string `mapstructure:"filename_err"`
|
||
MaxSize int `mapstructure:"max_size"`
|
||
MaxAge int `mapstructure:"max_age"`
|
||
MaxBackups int `mapstructure:"max_backups"`
|
||
}
|
||
|
||
type ApolloConfig struct {
|
||
AppID string `mapstructure:"app_id"`
|
||
Cluster string `mapstructure:"cluster"` // 环境
|
||
NameSpaceNames []string `mapstructure:"name_spaceNames"`
|
||
MetaAddr string `mapstructure:"meta_addr"` // 配置中心地址
|
||
AccesskeySecret string `mapstructure:"access_key_secret"`
|
||
}
|
||
|
||
type OCRConfig struct {
|
||
URL string `mapstructure:"url"`
|
||
Det bool `mapstructure:"det"`
|
||
Rec bool `mapstructure:"rec"`
|
||
Mode int `mapstructure:"mode"`
|
||
Addr string `mapstructure:"addr"`
|
||
}
|
||
|
||
type MinIoConfig struct {
|
||
Endpoint string `mapstructure:"endpoint"`
|
||
AccessKeyId string `mapstructure:"access_key_id"`
|
||
SecretAccessKey string `mapstructure:"secret_access_key"`
|
||
Secure bool `mapstructure:"secure"`
|
||
BucketName string `mapstructure:"bucket_name"`
|
||
}
|
||
|
||
type OpenCVConfig struct {
|
||
Threshold float32 `mapstructure:"threshold"`
|
||
}
|
||
|
||
type MySQLConfig struct {
|
||
Host string `mapstructure:"host"`
|
||
User string `mapstructure:"user"`
|
||
Password string `mapstructure:"password"`
|
||
DB string `mapstructure:"dbname"`
|
||
Port int `mapstructure:"port"`
|
||
MaxOpenConns int `mapstructure:"max_open_conns"`
|
||
MaxIdleConns int `mapstructure:"max_idle_conns"`
|
||
}
|
||
|
||
type SnowflakeConfig struct {
|
||
StartTime string `mapstructure:"start_time"`
|
||
MachineID int64 `mapstructure:"machine_id"`
|
||
}
|
||
|
||
type GrpcConfig struct {
|
||
Port int `mapstructure:"port"`
|
||
MaxRecvMsgSize int `mapstructure:"max_recv_msg_size"`
|
||
MaxSendMsgSize int `mapstructure:"max_send_msg_size"`
|
||
}
|
||
|
||
type Registrar struct {
|
||
Enabled bool `mapstructure:"enabled"`
|
||
Address []string `mapstructure:"address"`
|
||
}
|
||
|
||
type RcloneConfig struct {
|
||
RemoteName string `mapstructure:"remote_name"`
|
||
BucketName string `mapstructure:"bucket_name"`
|
||
RclonePath string `mapstructure:"rclone_path"` // rclone命令所在位置,在全局环境中则填入rclone
|
||
Expire int `mapstructure:"expire"` // 公共储存通有效时间s
|
||
RcloneConfig string `mapstructure:"rclone_config"` // Rclone配置文件所在目录
|
||
CustomDomains string `mapstructure:"custom_domains"` // 自定义域
|
||
}
|
||
|
||
type OpenAIConfig struct {
|
||
BaseURL string `mapstructure:"base_url"`
|
||
ApiKey string `mapstructure:"api_key"`
|
||
Model string `mapstructure:"model"`
|
||
Prompt string `mapstructure:"prompt"`
|
||
}
|
||
|
||
type TemporalConfig struct {
|
||
Host string `mapstructure:"host"` // Temporal服务地址
|
||
Port int `mapstructure:"port"` // Temporal服务端口
|
||
}
|
||
|
||
// Init 整个服务配置文件初始化的方法
|
||
func Init(yamlContent string) (err error) {
|
||
// 方式1:直接指定配置文件路径(相对路径或者绝对路径)
|
||
// 相对路径:相对执行的可执行文件的相对路径
|
||
// viper.SetConfigFile("./conf/config.yaml")
|
||
|
||
viper.SetConfigType("yaml")
|
||
err = viper.ReadConfig(strings.NewReader(yamlContent))
|
||
if err != nil {
|
||
// 读取配置信息失败
|
||
fmt.Printf("viper.ReadInConfig failed, err:%v\n", err)
|
||
return
|
||
}
|
||
// 如果使用的是 viper.GetXxx()方式使用配置的话,就无须下面的操作
|
||
|
||
// 把读取到的配置信息反序列化到 Conf 变量中
|
||
if err := viper.Unmarshal(&Conf); err != nil {
|
||
fmt.Printf("viper.Unmarshal failed, err:%v\n", err)
|
||
}
|
||
|
||
/*viper.WatchConfig() // 配置文件监听
|
||
viper.OnConfigChange(func(in fsnotify.Event) {
|
||
ConfLock.Lock() // 获取锁
|
||
defer ConfLock.Unlock() // 释放锁
|
||
fmt.Println("配置文件修改了...")
|
||
if err := viper.Unmarshal(&Conf); err != nil {
|
||
fmt.Printf("viper.Unmarshal failed, err:%v\n", err)
|
||
}
|
||
})*/
|
||
/*err, ip := GetNetworkCard([]string{Conf.MetaAddr})
|
||
if err != nil {
|
||
return err
|
||
}
|
||
fmt.Println(ip)
|
||
Conf.IP = "10.10.1.254"*/
|
||
return
|
||
}
|
||
|
||
var ConfLock = new(sync.RWMutex) // 配置读写锁,高并发情况下保证获取的是最新的配置
|
||
|
||
func InitFromFile(filePath string) (err error) {
|
||
// 方式1:直接指定配置文件路径(相对路径或者绝对路径)
|
||
// 相对路径:相对执行的可执行文件的相对路径
|
||
// viper.SetConfigFile("./conf/config.yaml")
|
||
viper.SetConfigFile(filePath)
|
||
|
||
err = viper.ReadInConfig() // 读取配置信息
|
||
if err != nil {
|
||
// 读取配置信息失败
|
||
fmt.Printf("viper.ReadInConfig failed, err:%v\n", err)
|
||
return
|
||
}
|
||
// 如果使用的是 viper.GetXxx()方式使用配置的话,就无须下面的操作
|
||
|
||
// 把读取到的配置信息反序列化到 Conf 变量中
|
||
if err := viper.Unmarshal(Conf); err != nil {
|
||
fmt.Printf("viper.Unmarshal failed, err:%v\n", err)
|
||
}
|
||
|
||
viper.WatchConfig() // 配置文件监听
|
||
viper.OnConfigChange(func(in fsnotify.Event) {
|
||
ConfLock.Lock() // 获取锁
|
||
defer ConfLock.Unlock() // 释放锁
|
||
fmt.Println("配置文件修改了...")
|
||
if err := viper.Unmarshal(&Conf); err != nil {
|
||
fmt.Printf("viper.Unmarshal failed, err:%v\n", err)
|
||
}
|
||
})
|
||
return
|
||
}
|