package config
import (
"fmt"
"os"
"time"
"gitee.com/openeuler/PilotGo/sdk/logger"
"gopkg.in/yaml.v3"
)
type HttpConf struct {
Addr string `yaml:"addr"`
}
type MysqlConf struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
User string `yaml:"user"`
Password string `yaml:"password"`
Database string `yaml:"database"`
MaxOpenConns int `yaml:"max_open_conns"`
MaxIdleConns int `yaml:"max_idle_conns"`
ConnMaxLifetime time.Duration `yaml:"conn_max_lifetime"`
ConnMaxIdleTime time.Duration `yaml:"conn_max_idle_time"`
}
type Etcd struct {
Endpoints []string `yaml:"endpoints"`
ServiceName string `yaml:"service_name"`
Version string `yaml:"version"`
DialTimeout time.Duration `yaml:"dialTimeout"`
MenuName string `yaml:"menu_name"`
Icon string `yaml:"icon"`
}
type ServerConfig struct {
Http *HttpConf `yaml:"http"`
Mysql *MysqlConf `yaml:"mysql"`
Logopts *logger.LogOpts `yaml:"log"`
Etcd *Etcd `yaml:"etcd" mapstructure:"etcd"`
}
const config_file = "./config.yml"
var global_config ServerConfig
func init() {
err := readConfig(config_file, &global_config)
if err != nil {
fmt.Printf("")
os.Exit(-1)
}
}
func Config() *ServerConfig {
return &global_config
}
func readConfig(file string, config interface{}) error {
bytes, err := os.ReadFile(file)
if err != nil {
fmt.Printf("open %s failed! err = %s\n", file, err.Error())
return err
}
err = yaml.Unmarshal(bytes, config)
if err != nil {
fmt.Printf("yaml Unmarshal %s failed!\n", string(bytes))
return err
}
return nil
}