dataRT/config/config.go

120 lines
2.9 KiB
Go
Raw Normal View History

2025-08-15 16:01:02 +08:00
package config
2025-09-05 18:35:46 +08:00
import (
"encoding/json"
"flag"
"os"
)
type config struct {
serverConf *serverConfig
logConf *logConfig
postgresConf map[string]*postgresConfig
influxConf map[string]*influxConfig
redisConf map[string]*redisConfig
mongoConf map[string]*mongoConfig
2025-09-19 16:17:46 +08:00
rabbitConf map[string][]*rabbitConfig
2025-09-05 18:35:46 +08:00
}
var conf *config
var confPath string
func init() {
flag.StringVar(&confPath, "conf_path", "./configs", "conf path")
flag.Parse()
conf = new(config)
conf.serverConf = new(serverConfig)
serverConf := confPath + string(os.PathSeparator) + serverConfigName()
conf.unmarshalJsonFile(serverConf, conf.serverConf)
conf.logConf = new(logConfig)
logConf := confPath + string(os.PathSeparator) + logConfigName()
conf.unmarshalJsonFile(logConf, conf.logConf)
conf.postgresConf = make(map[string]*postgresConfig)
postgresConf := confPath + string(os.PathSeparator) + postgresConfigName()
conf.unmarshalJsonFile(postgresConf, &conf.postgresConf)
conf.influxConf = make(map[string]*influxConfig)
influxConf := confPath + string(os.PathSeparator) + influxConfigName()
conf.unmarshalJsonFile(influxConf, &conf.influxConf)
conf.redisConf = make(map[string]*redisConfig)
redisConf := confPath + string(os.PathSeparator) + redisConfigName()
conf.unmarshalJsonFile(redisConf, &conf.redisConf)
conf.mongoConf = make(map[string]*mongoConfig)
mongoConf := confPath + string(os.PathSeparator) + mongoConfigName()
conf.unmarshalJsonFile(mongoConf, &conf.mongoConf)
2025-09-19 16:17:46 +08:00
conf.rabbitConf = make(map[string][]*rabbitConfig)
2025-09-05 18:35:46 +08:00
rabbitConf := confPath + string(os.PathSeparator) + rabbitConfigName()
conf.unmarshalJsonFile(rabbitConf, &conf.rabbitConf)
}
func Conf() *config {
return conf
}
func (c *config) ServerConf() *serverConfig {
if c == nil {
panic("config is nil")
}
return c.serverConf
}
func (c *config) LogConf() *logConfig {
if c == nil {
panic("config is nil")
}
return c.logConf
}
func (c *config) PostgresConf(tag string) *postgresConfig {
if c == nil || c.postgresConf == nil {
panic("postgres config is nil")
}
return c.postgresConf[tag]
}
func (c *config) InfluxConf(tag string) *influxConfig {
if c == nil || c.influxConf == nil {
panic("influx config is nil")
}
return c.influxConf[tag]
}
func (c *config) RedisConf(tag string) *redisConfig {
if c == nil || c.redisConf == nil {
panic("redis config is nil")
}
return c.redisConf[tag]
}
func (c *config) MongoConf(tag string) *mongoConfig {
if c == nil || c.mongoConf == nil {
panic("mongo config is nil")
}
return c.mongoConf[tag]
}
2025-09-19 16:17:46 +08:00
func (c *config) RabbitConf(tag string) []*rabbitConfig {
2025-09-05 18:35:46 +08:00
if c == nil || c.rabbitConf == nil {
panic("rabbit config is nil")
}
return c.rabbitConf[tag]
}
func (c *config) unmarshalJsonFile(file string, dest any) {
if filejson, err := os.ReadFile(file); err != nil {
panic(err.Error())
} else {
err := json.Unmarshal([]byte(filejson), dest)
if err != nil {
panic(err.Error())
}
}
}