hpds_jkw_web/config/config.go

144 lines
4.7 KiB
Go
Raw Permalink Normal View History

2023-01-06 10:09:23 +08:00
package config
import (
"fmt"
"github.com/spf13/viper"
"os"
consulapi "github.com/hashicorp/consul/api"
_ "github.com/spf13/viper/remote"
"gopkg.in/yaml.v3"
)
type WebConfig struct {
2023-01-11 18:05:29 +08:00
Name string `yaml:"name,omitempty"`
Host string `yaml:"host,omitempty"`
Port int `yaml:"port,omitempty"`
Mode string `yaml:"mode,omitempty"`
TrainDir string `yaml:"trainDir,omitempty"`
2023-01-11 18:05:29 +08:00
Consul ConsulConfig `yaml:"consul,omitempty"`
Db DbConfig `yaml:"db"`
Cache CacheConfig `yaml:"cache"`
Logging LogOptions `yaml:"logging"`
Minio MinioConfig `yaml:"minio"`
MineData MineDataConfig `yaml:"mineData"`
2023-03-23 18:03:09 +08:00
Node HpdsNode `yaml:"node,omitempty"`
Funcs []FuncConfig `yaml:"functions,omitempty"`
2023-01-06 10:09:23 +08:00
}
type ConsulConfig struct {
Host string `yaml:"host,omitempty"`
Port int `yaml:"port,omitempty"`
Interval int `yaml:"interval,omitempty"`
Timeout int `yaml:"timeout,omitempty"`
Deregister int `yaml:"deregister,omitempty"`
Tags []string `yaml:"tags,omitempty"`
}
type DbConfig struct {
Conn string `yaml:"conn"`
DriveName string `yaml:"drive_name"`
}
type CacheConfig struct {
Host string `yaml:"host,omitempty"`
Port int `yaml:"port,omitempty"`
Pass string `yaml:"pass,omitempty"`
DB int `yaml:"db,omitempty"`
PoolSize int `yaml:"pool_size,omitempty"`
}
2023-01-06 19:15:52 +08:00
type LogOptions struct {
Path string `yaml:"path" json:"path" toml:"path"` // 文件保存地方
Prefix string `yaml:"prefix" json:"prefix" toml:"prefix"` // 日志文件前缀
ErrorFileSuffix string `yaml:"errorFileSuffix" json:"errorFileSuffix" toml:"errorFileSuffix"` // error日志文件后缀
WarnFileSuffix string `yaml:"warnFileSuffix" json:"warnFileSuffix" toml:"warnFileSuffix"` // warn日志文件后缀
InfoFileSuffix string `yaml:"infoFileSuffix" json:"infoFileSuffix" toml:"infoFileSuffix"` // info日志文件后缀
DebugFileSuffix string `yaml:"debugFileSuffix" json:"debugFileSuffix" toml:"debugFileSuffix"` // debug日志文件后缀
Level string `yaml:"level" json:"level" toml:"level"` // 日志等级
MaxSize int `yaml:"maxSize" json:"maxSize" toml:"maxSize"` // 日志文件大小M
MaxBackups int `yaml:"maxBackups" json:"maxBackups" toml:"maxBackups"` // 最多存在多少个切片文件
MaxAge int `yaml:"maxAge" json:"maxAge" toml:"maxAge"` // 保存的最大天数
Development bool `yaml:"development" json:"development" toml:"development"` // 是否是开发模式
}
2023-01-11 18:05:29 +08:00
type MinioConfig struct {
2023-01-12 10:21:40 +08:00
Protocol string `yaml:"protocol"` //http or https
2023-01-11 18:05:29 +08:00
Endpoint string `yaml:"endpoint"`
AccessKeyId string `yaml:"accessKeyId"`
SecretAccessKey string `yaml:"secretAccessKey"`
Bucket string `yaml:"bucket"`
}
type MineDataConfig struct {
AccessKey string `yaml:"accessKey"`
}
2023-03-23 18:03:09 +08:00
type HpdsNode struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Token string `yaml:"token,omitempty"`
}
type FuncConfig struct {
Name string `yaml:"name"`
DataTag uint8 `yaml:"dataTag"`
MqType uint `yaml:"mqType"` //消息类型, 发布1订阅2
}
2023-01-06 10:09:23 +08:00
func ParseConfigByFile(path string) (cfg *WebConfig, err error) {
buffer, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return load(buffer)
}
func load(buf []byte) (cfg *WebConfig, err error) {
cViper := viper.New()
cViper.SetConfigType("yaml")
cfg = new(WebConfig)
2023-03-23 18:03:09 +08:00
cfg.Funcs = make([]FuncConfig, 0)
//cViper.ReadConfig(bytes.NewBuffer(buf))
err = yaml.Unmarshal(buf, cfg)
//err = cViper.Unmarshal(cfg)
//if err != nil {
// return nil, err
//}
2023-01-06 10:09:23 +08:00
return
}
func UpdateLocalConfig(cfg *WebConfig, fn string) error {
data, err := yaml.Marshal(cfg)
if err != nil {
return err
}
err = os.WriteFile(fn, data, 0600)
return err
}
func UpdateRemoteConfig(cfg *WebConfig) error {
consulClient, err := consulapi.NewClient(&consulapi.Config{Address: fmt.Sprintf("%s:%d", cfg.Consul.Host, cfg.Consul.Port)})
if err != nil {
return err
}
val, err := yaml.Marshal(cfg)
if err != nil {
return err
}
p := &consulapi.KVPair{Key: fmt.Sprintf("hpds-pavement/hpds_web/%s/%s", cfg.Mode, cfg.Name), Value: val}
if _, err = consulClient.KV().Put(p, nil); err != nil {
return err
}
return nil
}
func GetRemoteConfig(remoteAddr, path string) (cfg *WebConfig, err error) {
consulClient, err := consulapi.NewClient(&consulapi.Config{Address: remoteAddr})
if err != nil {
return nil, err
}
kv, _, err := consulClient.KV().Get(path, nil)
if err != nil {
return nil, err
}
return load(kv.Value)
}