dataRT/config/config.go

144 lines
3.6 KiB
Go

package config
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
kafkaConf map[string]*kafkaConfig
rabbitConf map[string]*rabbitConfig
urlConf map[string]*urlConfig
}
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)
conf.kafkaConf = make(map[string]*kafkaConfig)
kafkaConf := confPath + string(os.PathSeparator) + kafkaConfigName()
conf.unmarshalJsonFile(kafkaConf, &conf.kafkaConf)
conf.rabbitConf = make(map[string]*rabbitConfig)
rabbitConf := confPath + string(os.PathSeparator) + rabbitConfigName()
conf.unmarshalJsonFile(rabbitConf, &conf.rabbitConf)
conf.urlConf = make(map[string]*urlConfig)
urlConf := confPath + string(os.PathSeparator) + urlConfigName()
conf.unmarshalJsonFile(urlConf, &conf.urlConf)
}
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]
}
func (c *config) KafkaConf(tag string) *kafkaConfig {
if c == nil || c.kafkaConf == nil {
panic("kafka config is nil")
}
return c.kafkaConf[tag]
}
func (c *config) RabbitConf(tag string) *rabbitConfig {
if c == nil || c.rabbitConf == nil {
panic("rabbit config is nil")
}
return c.rabbitConf[tag]
}
func (c *config) URLConf(tag string) *urlConfig {
if c == nil || c.urlConf == nil {
panic("modelrt config is nil")
}
return c.urlConf[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())
}
}
}