From 1186f5dbff284b6bc1d49e54f841eb8e2bff83ae Mon Sep 17 00:00:00 2001 From: zhuxu Date: Fri, 5 Sep 2025 18:35:46 +0800 Subject: [PATCH] config --- README.md | 3 + config/config.go | 142 +++++++++++++++++++++++ config/influx.go | 72 ++++++++++++ config/kafka.go | 213 ++++++++++++++++++++++++++++++++++ config/log.go | 137 ++++++++++++++++++++++ config/mongo.go | 87 ++++++++++++++ config/postgres.go | 117 +++++++++++++++++++ config/rabbit.go | 258 ++++++++++++++++++++++++++++++++++++++++++ config/redis.go | 147 ++++++++++++++++++++++++ config/server.go | 63 +++++++++++ config/url.go | 72 ++++++++++++ configs/influx.json | 8 ++ configs/kafka.json | 17 +++ configs/log.json | 9 ++ configs/mongo.json | 9 ++ configs/postgres.json | 11 ++ configs/rabbit.json | 12 ++ configs/redis.json | 13 +++ configs/server.json | 7 ++ configs/url.json | 8 ++ go.mod | 10 ++ go.sum | 16 +++ log/log.go | 1 - 23 files changed, 1431 insertions(+), 1 deletion(-) create mode 100644 README.md create mode 100644 config/influx.go create mode 100644 config/kafka.go create mode 100644 config/log.go create mode 100644 config/mongo.go create mode 100644 config/postgres.go create mode 100644 config/rabbit.go create mode 100644 config/redis.go create mode 100644 config/server.go create mode 100644 config/url.go create mode 100644 configs/influx.json create mode 100644 configs/kafka.json create mode 100644 configs/log.json create mode 100644 configs/mongo.json create mode 100644 configs/postgres.json create mode 100644 configs/rabbit.json create mode 100644 configs/redis.json create mode 100644 configs/url.json create mode 100644 go.sum delete mode 100644 log/log.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..18602ed --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# dataRT + +[![Build Status](http://192.168.46.100:4080/api/badges/CL-Softwares/dataRT/status.svg?ref=refs/heads/dev)](http://192.168.46.100:4080/CL-Softwares/dataRT) \ No newline at end of file diff --git a/config/config.go b/config/config.go index d912156..2e613f7 100644 --- a/config/config.go +++ b/config/config.go @@ -1 +1,143 @@ 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()) + } + } +} diff --git a/config/influx.go b/config/influx.go new file mode 100644 index 0000000..28f6f7b --- /dev/null +++ b/config/influx.go @@ -0,0 +1,72 @@ +package config + +type influxConfig struct { + URL string `json:"url" yaml:"url"` + Token string `json:"token" yaml:"token"` + Org string `json:"org" yaml:"org"` + Timeout int `json:"timeout" yaml:"timeout"` +} + +func NewInfluxConfig() *influxConfig { + return new(influxConfig) +} + +func (conf *influxConfig) GetURL() string { + if conf == nil { + panic("influx config is nil") + } + return conf.URL +} + +func (conf *influxConfig) SetURL(url string) { + if conf == nil { + panic("influx config is nil") + } + conf.URL = url +} + +func (conf *influxConfig) GetToken() string { + if conf == nil { + panic("influx config is nil") + } + return conf.Token +} + +func (conf *influxConfig) SetToken(token string) { + if conf == nil { + panic("influx config is nil") + } + conf.Token = token +} + +func (conf *influxConfig) GetOrg() string { + if conf == nil { + panic("influx config is nil") + } + return conf.Org +} + +func (conf *influxConfig) SetOrg(org string) { + if conf == nil { + panic("influx config is nil") + } + conf.Org = org +} + +func (conf *influxConfig) GetTimeout() int { + if conf == nil { + panic("influx config is nil") + } + return conf.Timeout +} + +func (conf *influxConfig) SetTimeout(timeout int) { + if conf == nil { + panic("influx config is nil") + } + conf.Timeout = timeout +} + +func influxConfigName() string { + return "influx.json" +} diff --git a/config/kafka.go b/config/kafka.go new file mode 100644 index 0000000..7d470c5 --- /dev/null +++ b/config/kafka.go @@ -0,0 +1,213 @@ +package config + +type kafkaConsumer struct { + GroupID string `json:"groupid" yaml:"groupid"` + // OffsetNewest, -1, stands for the log head offset, i.e. the offset that will be + // assigned to the next message that will be produced to the partition. You + // can send this to a client's GetOffset method to get this offset, or when + // calling ConsumePartition to start consuming new messages. + // + // OffsetOldest, -2, stands for the oldest offset available on the broker for a + // partition. You can send this to a client's GetOffset method to get this + // offset, or when calling ConsumePartition to start consuming from the + // oldest offset that is still available on the broker. + InitialOffset int64 `json:"initialoffset" yaml:"initialoffset"` +} + +type kafkaProducer struct { + // NoResponse, 0, doesn't send any response, the TCP ACK is all you get. + // + // WaitForLocal, 1, waits for only the local commit to succeed before responding. + // + // WaitForAll, -1, waits for all in-sync replicas to commit before responding. + // The minimum number of in-sync replicas is configured on the broker via + // the `min.insync.replicas` configuration key. + RequiredAcks int16 `json:"requiredacks" yaml:"requiredacks"` + // manual/random/roundrobin/customhash/hash/referencehash/consistentcrchash + Partitioner string `json:"partitioner" yaml:"partitioner"` + ReturnSuccesses bool `json:"returnsuccesses" yaml:"returnsuccesses"` + ReturnErrors bool `json:"returnerrors" yaml:"returnerrors"` + // CompressionNone, 0, no compression + // CompressionGZIP, 1, compression using GZIP + // CompressionSnappy, 2, compression using snappy + // CompressionLZ4, 3, compression using LZ4 + // CompressionZSTD, 4, compression using ZSTD + Compression int8 `json:"compression" yaml:"compression"` +} + +type kafkaConfig struct { + Brokers []string `json:"brokers" yaml:"brokers"` + Topics []string `json:"topics" yaml:"topics"` + Consumer *kafkaConsumer `json:"consumer" yaml:"consumer"` + Producer *kafkaProducer `json:"producer" yaml:"producer"` +} + +func NewKafkaConfig() *kafkaConfig { + return new(kafkaConfig) +} + +func (conf *kafkaConfig) GetBrokers() []string { + if conf == nil { + panic("kafka config is nil") + } + return conf.Brokers +} + +func (conf *kafkaConfig) SetBrokers(brokers []string) { + if conf == nil { + panic("kafka config is nil") + } + conf.Brokers = brokers +} + +func (conf *kafkaConfig) GetTopics() []string { + if conf == nil { + panic("kafka config is nil") + } + return conf.Topics +} + +func (conf *kafkaConfig) SetTopics(topics []string) { + if conf == nil { + panic("kafka config is nil") + } + conf.Topics = topics +} + +func (conf *kafkaConfig) GetConsumer() *kafkaConsumer { + if conf == nil { + panic("kafka config is nil") + } + return conf.Consumer +} + +func (conf *kafkaConfig) InitConsumer() *kafkaConsumer { + if conf == nil { + panic("kafka config is nil") + } + conf.Consumer = new(kafkaConsumer) + return conf.Consumer +} + +func (conf *kafkaConsumer) GetGroupID() string { + if conf == nil { + panic("kafka consumer is nil") + } + return conf.GroupID +} + +func (conf *kafkaConsumer) SetGroupID(groupID string) { + if conf == nil { + panic("kafka consumer is nil") + } + conf.GroupID = groupID +} + +func (conf *kafkaConsumer) GetInitialOffset() int64 { + if conf == nil { + panic("kafka consumer is nil") + } + return conf.InitialOffset +} + +func (conf *kafkaConsumer) SetInitialOffset(initialOffset int64) { + if conf == nil { + panic("kafka consumer is nil") + } + if initialOffset != -1 && initialOffset != -2 { + panic("initialOffset is invalid") + } + conf.InitialOffset = initialOffset +} + +func (conf *kafkaConfig) GetProducer() *kafkaProducer { + if conf == nil { + panic("kafka config is nil") + } + return conf.Producer +} + +func (conf *kafkaConfig) InitProducer() *kafkaProducer { + if conf == nil { + panic("kafka config is nil") + } + conf.Producer = new(kafkaProducer) + return conf.Producer +} + +func (conf *kafkaProducer) GetRequiredAcks() int16 { + if conf == nil { + panic("kafka producer is nil") + } + return conf.RequiredAcks +} + +func (conf *kafkaProducer) SetRequiredAcks(requiredAcks int16) { + if conf == nil { + panic("kafka producer is nil") + } + if requiredAcks < -1 || requiredAcks > 1 { + panic("requiredAcks is invalid") + } + conf.RequiredAcks = requiredAcks +} + +func (conf *kafkaProducer) GetPartitioner() string { + if conf == nil { + panic("kafka producer is nil") + } + return conf.Partitioner +} + +func (conf *kafkaProducer) SetPartitioner(partitioner string) { + if conf == nil { + panic("kafka producer is nil") + } + conf.Partitioner = partitioner +} + +func (conf *kafkaProducer) GetReturnSuccesses() bool { + if conf == nil { + panic("kafka producer is nil") + } + return conf.ReturnSuccesses +} + +func (conf *kafkaProducer) SetReturnSuccesses(returnSuccesses bool) { + if conf == nil { + panic("kafka producer is nil") + } + conf.ReturnSuccesses = returnSuccesses +} + +func (conf *kafkaProducer) GetReturnErrors() bool { + if conf == nil { + panic("kafka producer is nil") + } + return conf.ReturnErrors +} + +func (conf *kafkaProducer) SetReturnErrors(returnErrors bool) { + if conf == nil { + panic("kafka producer is nil") + } + conf.ReturnErrors = returnErrors +} + +func (conf *kafkaProducer) GetCompression() int8 { + if conf == nil { + panic("kafka producer is nil") + } + return conf.Compression +} + +func (conf *kafkaProducer) SetCompression(compression int8) { + if conf == nil { + panic("kafka producer is nil") + } + conf.Compression = compression +} + +func kafkaConfigName() string { + return "kafka.json" +} diff --git a/config/log.go b/config/log.go new file mode 100644 index 0000000..0dc9fe9 --- /dev/null +++ b/config/log.go @@ -0,0 +1,137 @@ +package config + +import ( + "go.uber.org/zap/zapcore" + "gopkg.in/natefinch/lumberjack.v2" +) + +type logConfig struct { + lumberjack.Logger + // DebugLevel, -1, logs are typically voluminous, and are usually disabled in + // production. + // + // InfoLevel, 0, is the default logging priority. + // + // WarnLevel, 1, logs are more important than Info, but don't need individual + // human review. + // + // ErrorLevel, 2, logs are high-priority. If an application is running smoothly, + // it shouldn't generate any error-level logs. + // + // DPanicLevel, 3, logs are particularly important errors. In development the + // logger panics after writing the message. + // + // PanicLevel, 4, logs a message, then panics. + // + // FatalLevel, 5, logs a message, then calls os.Exit(1). + LogLevel int8 `json:"loglevel" yaml:"loglevel"` +} + +func NewLogCinfig() *logConfig { + return new(logConfig) +} + +func (conf *logConfig) GetFileName() string { + if conf == nil { + panic("log config is nil") + } + return conf.Filename +} + +func (conf *logConfig) SetFileName(fileName string) { + if conf == nil { + panic("log config is nil") + } + conf.Filename = fileName +} + +func (conf *logConfig) GetMaxSize() int { + if conf == nil { + panic("log config is nil") + } + return conf.MaxSize +} + +func (conf *logConfig) SetMaxSize(maxSize int) { + if conf == nil { + panic("log config is nil") + } + conf.MaxSize = maxSize +} + +func (conf *logConfig) GetMaxAge() int { + if conf == nil { + panic("log config is nil") + } + return conf.MaxAge +} + +func (conf *logConfig) SetMaxAge(maxAge int) { + if conf == nil { + panic("log config is nil") + } + conf.MaxAge = maxAge +} + +func (conf *logConfig) GetMaxBackups() int { + if conf == nil { + panic("log config is nil") + } + return conf.MaxBackups +} + +func (conf *logConfig) SetMaxBackups(maxBackups int) { + if conf == nil { + panic("log config is nil") + } + conf.MaxBackups = maxBackups +} + +func (conf *logConfig) GetLocalTime() bool { + if conf == nil { + panic("log config is nil") + } + return conf.LocalTime +} + +func (conf *logConfig) SetLocalTime(useLocalTime bool) { + if conf == nil { + panic("log config is nil") + } + conf.LocalTime = useLocalTime +} + +func (conf *logConfig) GetCompress() bool { + if conf == nil { + panic("log config is nil") + } + return conf.Compress +} + +func (conf *logConfig) SetCompress(doCompress bool) { + if conf == nil { + panic("log config is nil") + } + conf.Compress = doCompress +} + +func (conf *logConfig) GetLogLevel() zapcore.Level { + if conf == nil { + panic("log config is nil") + } + return zapcore.Level(conf.LogLevel) +} + +func (conf *logConfig) SetLogLevel(logLevel zapcore.Level) { + if conf == nil { + panic("log config is nil") + } + if logLevel < -1 || logLevel > 5 { + panic("logLevel is invalid") + } + conf.LogLevel = int8(logLevel) +} + +func logConfigName() string { + return "log.json" +} diff --git a/config/mongo.go b/config/mongo.go new file mode 100644 index 0000000..4619b9a --- /dev/null +++ b/config/mongo.go @@ -0,0 +1,87 @@ +package config + +type mongoConfig struct { + Hosts []string `json:"hosts" yaml:"hosts"` + Username string `json:"username" yaml:"username"` + Password string `json:"password" yaml:"password"` + AuthSource string `josn:"authsource" yaml:"authsource"` + AuthMechanism string `json:"authmechanism" yaml:"authmechanism"` +} + +func NewMongoConfig() *mongoConfig { + return new(mongoConfig) +} + +func (conf *mongoConfig) GetHosts() []string { + if conf == nil { + panic("mongo config is nil") + } + return conf.Hosts +} + +func (conf *mongoConfig) SetHosts(hosts []string) { + if conf == nil { + panic("mongo config is nil") + } + conf.Hosts = hosts +} + +func (conf *mongoConfig) GetUsername() string { + if conf == nil { + panic("mongo config is nil") + } + return conf.Username +} + +func (conf *mongoConfig) SetUsername(username string) { + if conf == nil { + panic("mongo config is nil") + } + conf.Username = username +} + +func (conf *mongoConfig) GetPassword() string { + if conf == nil { + panic("mongo config is nil") + } + return conf.Password +} + +func (conf *mongoConfig) SetPassword(password string) { + if conf == nil { + panic("mongo config is nil") + } + conf.Password = password +} + +func (conf *mongoConfig) GetAuthSource() string { + if conf == nil { + panic("mongo config is nil") + } + return conf.AuthSource +} + +func (conf *mongoConfig) SetAuthSource(authSource string) { + if conf == nil { + panic("mongo config is nil") + } + conf.AuthSource = authSource +} + +func (conf *mongoConfig) GetAuthMechanism() string { + if conf == nil { + panic("mongo config is nil") + } + return conf.AuthMechanism +} + +func (conf *mongoConfig) SetAuthMechanism(authMechanism string) { + if conf == nil { + panic("mongo config is nil") + } + conf.AuthMechanism = authMechanism +} + +func mongoConfigName() string { + return "mongo.json" +} diff --git a/config/postgres.go b/config/postgres.go new file mode 100644 index 0000000..589edfc --- /dev/null +++ b/config/postgres.go @@ -0,0 +1,117 @@ +package config + +type postgresConfig struct { + Host string `json:"host" yaml:"host"` + Port int `json:"port" yaml:"port"` + User string `json:"user" yaml:"user"` + Password string `json:"password" yaml:"password"` + DBName string `json:"dbname" yaml:"dbname"` + SSLMode string `json:"sslmode" yaml:"sslmode"` + TimeZone string `json:"timezone" yaml:"timezone"` +} + +func NewPostgresConfig() *postgresConfig { + return new(postgresConfig) +} + +func (conf *postgresConfig) GetHost() string { + if conf == nil { + panic("postgres config is nil") + } + return conf.Host +} + +func (conf *postgresConfig) SetHost(host string) { + if conf == nil { + panic("postgres config is nil") + } + conf.Host = host +} + +func (conf *postgresConfig) GetPort() int { + if conf == nil { + panic("postgres config is nil") + } + return conf.Port +} + +func (conf *postgresConfig) SetPort(port int) { + if conf == nil { + panic("postgres config is nil") + } + conf.Port = port +} + +func (conf *postgresConfig) GetUser() string { + if conf == nil { + panic("postgres config is nil") + } + return conf.User +} + +func (conf *postgresConfig) SetUser(user string) { + if conf == nil { + panic("postgres config is nil") + } + conf.User = user +} + +func (conf *postgresConfig) GetPassword() string { + if conf == nil { + panic("postgres config is nil") + } + return conf.Password +} + +func (conf *postgresConfig) SetPassword(password string) { + if conf == nil { + panic("postgres config is nil") + } + conf.Password = password +} + +func (conf *postgresConfig) GetDBName() string { + if conf == nil { + panic("postgres config is nil") + } + return conf.DBName +} + +func (conf *postgresConfig) SetDBName(dbName string) { + if conf == nil { + panic("postgres config is nil") + } + conf.DBName = dbName +} + +func (conf *postgresConfig) GetSSLMode() string { + if conf == nil { + panic("postgres config is nil") + } + return conf.SSLMode +} + +func (conf *postgresConfig) SetSSLMode(sslMode string) { + if conf == nil { + panic("postgres config is nil") + } + conf.SSLMode = sslMode +} + +func (conf *postgresConfig) GetTimeZone() string { + if conf == nil { + panic("postgres config is nil") + } + return conf.TimeZone +} + +func (conf *postgresConfig) SetTimeZone(timeZone string) { + if conf == nil { + panic("postgres config is nil") + } + conf.TimeZone = timeZone +} + +func postgresConfigName() string { + return "postgres.json" +} diff --git a/config/rabbit.go b/config/rabbit.go new file mode 100644 index 0000000..28979f7 --- /dev/null +++ b/config/rabbit.go @@ -0,0 +1,258 @@ +package config + +type xqr struct { + ExchangeName string `json:"exchangename" yaml:"exchangename"` + QueueName string `json:"queuename" yaml:"queuename"` + RoutingKey string `json:"routingkey" yaml:"routingkey"` + QueueLength int64 `json:"queuelength" yaml:"queuelength"` +} + +type tlsConfig struct { + CAPath string `json:"capath" yaml:"capath"` + KeyPath string `json:"keypath" yaml:"keypath"` + CertPath string `json:"certpath" yaml:"certpath"` + Password string `json:"password" yaml:"password"` + SkipVerify bool `json:"skipverify" yaml:"skipverify"` + ServerName string `json:"servername" yaml:"servername"` +} + +type rabbitConfig struct { + Hosts []string `json:"hosts" yaml:"hosts"` + Username string `json:"username" yaml:"username"` + Password string `json:"password" yaml:"password"` + NXQR *xqr `json:"nxqr" yaml:"nxqr"` + DXQR *xqr `json:"dxqr" yaml:"dxqr"` + TLS *tlsConfig `json:"tls" yaml:"tls"` +} + +func NewRabbitConfig() *rabbitConfig { + return new(rabbitConfig) +} + +func (conf *rabbitConfig) GetHosts() []string { + if conf == nil { + panic("rabbit config is nil") + } + return conf.Hosts +} + +func (conf *rabbitConfig) SetHosts(hosts []string) { + if conf == nil { + panic("rabbit config is nil") + } + conf.Hosts = hosts +} + +func (conf *rabbitConfig) GetUsername() string { + if conf == nil { + panic("rabbit config is nil") + } + return conf.Username +} + +func (conf *rabbitConfig) SetUsername(username string) { + if conf == nil { + panic("rabbit config is nil") + } + conf.Username = username +} + +func (conf *rabbitConfig) GetPassword() string { + if conf == nil { + panic("rabbit config is nil") + } + return conf.Password +} + +func (conf *rabbitConfig) SetPassword(password string) { + if conf == nil { + panic("rabbit config is nil") + } + conf.Password = password +} + +func (conf *rabbitConfig) InitNXQR() { + if conf == nil { + panic("rabbit config is nil") + } + conf.NXQR = new(xqr) +} + +func (conf *rabbitConfig) GetNXQR() *xqr { + if conf == nil { + panic("rabbit config is nil") + } + return conf.NXQR +} + +func (conf *rabbitConfig) InitDXQR() { + if conf == nil { + panic("rabbit config is nil") + } + conf.DXQR = new(xqr) +} + +func (conf *rabbitConfig) GetDXQR() *xqr { + if conf == nil { + panic("rabbit config is nil") + } + return conf.DXQR +} + +func (conf *xqr) GetExchangeName() string { + if conf == nil { + panic("rabbit xqr is nil") + } + return conf.ExchangeName +} + +func (conf *xqr) SetExchangeName(exchangeName string) { + if conf == nil { + panic("rabbit xqr is nil") + } + conf.ExchangeName = exchangeName +} + +func (conf *xqr) GetQueueName() string { + if conf == nil { + panic("rabbit xqr is nil") + } + return conf.QueueName +} + +func (conf *xqr) SetQueueName(queueName string) { + if conf == nil { + panic("rabbit xqr is nil") + } + conf.QueueName = queueName +} + +func (conf *xqr) GetRoutingKey() string { + if conf == nil { + panic("rabbit xqr is nil") + } + return conf.RoutingKey +} + +func (conf *xqr) SetRoutingKey(routingKey string) { + if conf == nil { + panic("rabbit xqr is nil") + } + conf.RoutingKey = routingKey +} + +func (conf *xqr) GetQueueLength() int64 { + if conf == nil { + panic("rabbit xqr is nil") + } + return conf.QueueLength +} + +func (conf *xqr) SetQueueLength(queueLength int64) { + if conf == nil { + panic("rabbit xqr is nil") + } + conf.QueueLength = queueLength +} + +func (conf *rabbitConfig) InitTLS() { + if conf == nil { + panic("rabbit config is nil") + } + conf.TLS = new(tlsConfig) +} + +func (conf *rabbitConfig) GetTLS() *tlsConfig { + if conf == nil { + panic("rabbit config is nil") + } + return conf.TLS +} + +func (conf *tlsConfig) GetCAPath() string { + if conf == nil { + panic("rabbit tls is nil") + } + return conf.CAPath +} + +func (conf *tlsConfig) SetCAPath(caPath string) { + if conf == nil { + panic("rabbit tls is nil") + } + conf.CAPath = caPath +} + +func (conf *tlsConfig) GetKeyPath() string { + if conf == nil { + panic("rabbit tls is nil") + } + return conf.KeyPath +} + +func (conf *tlsConfig) SetKeyPath(keyPath string) { + if conf == nil { + panic("rabbit tls is nil") + } + conf.KeyPath = keyPath +} + +func (conf *tlsConfig) GetCertPath() string { + if conf == nil { + panic("rabbit tls is nil") + } + return conf.CertPath +} + +func (conf *tlsConfig) SetCertPath(certPath string) { + if conf == nil { + panic("rabbit tls is nil") + } + conf.CertPath = certPath +} + +func (conf *tlsConfig) GetPassword() string { + if conf == nil { + panic("rabbit tls is nil") + } + return conf.Password +} + +func (conf *tlsConfig) SetPassword(password string) { + if conf == nil { + panic("rabbit tls is nil") + } + conf.Password = password +} + +func (conf *tlsConfig) GetSkipVerify() bool { + if conf == nil { + panic("rabbit tls is nil") + } + return conf.SkipVerify +} + +func (conf *tlsConfig) SetSkipVerify(skipVerify bool) { + if conf == nil { + panic("rabbit tls is nil") + } + conf.SkipVerify = skipVerify +} + +func (conf *tlsConfig) GetServerName() string { + if conf == nil { + panic("rabbit tls is nil") + } + return conf.ServerName +} + +func (conf *tlsConfig) SetServerName(serverName string) { + if conf == nil { + panic("rabbit tls is nil") + } + conf.ServerName = serverName +} + +func rabbitConfigName() string { + return "rabbit.json" +} diff --git a/config/redis.go b/config/redis.go new file mode 100644 index 0000000..4a8ef2d --- /dev/null +++ b/config/redis.go @@ -0,0 +1,147 @@ +package config + +type redisConfig struct { + Addr string `json:"addr" yaml:"addr"` + Username string `json:"username" yaml:"username"` + Password string `json:"password" yaml:"password"` + DB int `json:"db" yaml:"db"` + RESP int `json:"resp" yaml:"resp"` + DialTimeout int `json:"dialtimeout" yaml:"dialtimeout"` + ReadTimeout int `json:"readtimeout" yaml:"readtimeout"` + WriteTimeout int `json:"writetimeout" yaml:"writetimeout"` + PoolSize int `json:"poolsize" yaml:"poolsize"` +} + +func NewRedisConfig() *redisConfig { + return new(redisConfig) +} + +func (conf *redisConfig) GetAddr() string { + if conf == nil { + panic("redis config is nil") + } + return conf.Addr +} + +func (conf *redisConfig) SetAddr(addr string) { + if conf == nil { + panic("redis config is nil") + } + conf.Addr = addr +} + +func (conf *redisConfig) GetUsername() string { + if conf == nil { + panic("redis config is nil") + } + return conf.Username +} + +func (conf *redisConfig) SetUsername(username string) { + if conf == nil { + panic("redis config is nil") + } + conf.Username = username +} + +func (conf *redisConfig) GetPassword() string { + if conf == nil { + panic("redis config is nil") + } + return conf.Password +} + +func (conf *redisConfig) SetPassword(password string) { + if conf == nil { + panic("redis config is nil") + } + conf.Password = password +} + +func (conf *redisConfig) GetDB() int { + if conf == nil { + panic("redis config is nil") + } + return conf.DB +} + +func (conf *redisConfig) SetDB(db int) { + if conf == nil { + panic("redis config is nil") + } + conf.DB = db +} + +func (conf *redisConfig) GetRESP() int { + if conf == nil { + panic("redis config is nil") + } + return conf.RESP +} + +func (conf *redisConfig) SetRESP(resp int) { + if conf == nil { + panic("redis config is nil") + } + conf.RESP = resp +} + +func (conf *redisConfig) GetDialTimeout() int { + if conf == nil { + panic("redis config is nil") + } + return conf.DialTimeout +} + +func (conf *redisConfig) SetDialTimeout(dialTimeout int) { + if conf == nil { + panic("redis config is nil") + } + conf.DialTimeout = dialTimeout +} + +func (conf *redisConfig) GetReadTimeout() int { + if conf == nil { + panic("redis config is nil") + } + return conf.ReadTimeout +} + +func (conf *redisConfig) SetReadTimeout(readTimeout int) { + if conf == nil { + panic("redis config is nil") + } + conf.ReadTimeout = readTimeout +} + +func (conf *redisConfig) GetWriteTimeout() int { + if conf == nil { + panic("redis config is nil") + } + return conf.WriteTimeout +} + +func (conf *redisConfig) SetWriteTimeout(writeTimeout int) { + if conf == nil { + panic("redis config is nil") + } + conf.WriteTimeout = writeTimeout +} + +func (conf *redisConfig) GetPoolSize() int { + if conf == nil { + panic("redis config is nil") + } + return conf.PoolSize +} + +func (conf *redisConfig) SetPoolSIze(poolSize int) { + if conf == nil { + panic("redis config is nil") + } + conf.PoolSize = poolSize +} + +func redisConfigName() string { + return "redis.json" +} diff --git a/config/server.go b/config/server.go new file mode 100644 index 0000000..ba53c66 --- /dev/null +++ b/config/server.go @@ -0,0 +1,63 @@ +package config + +import "maps" + +type serverConfig struct { + Host string `json:"host" yaml:"host"` + Port int `json:"port" yaml:"port"` + SSUType map[string]uint8 `json:"ssutype" yaml:"ssutype"` +} + +func NewServerConfig() *serverConfig { + return new(serverConfig) +} + +func (conf *serverConfig) GetHost() string { + if conf == nil { + panic("server config is nil") + } + return conf.Host +} + +func (conf *serverConfig) SetHost(host string) { + if conf == nil { + panic("server config is nil") + } + conf.Host = host +} + +func (conf *serverConfig) GetPort() int { + if conf == nil { + panic("server config is nil") + } + return conf.Port +} + +func (conf *serverConfig) SetPort(port int) { + if conf == nil { + panic("server config is nil") + } + conf.Port = port +} + +func (conf *serverConfig) GetSSUType() map[string]uint8 { + if conf == nil { + panic("server config is nil") + } + return conf.SSUType +} + +func (conf *serverConfig) SetSSUType(ssuType map[string]uint8) { + if conf == nil { + panic("server config is nil") + } + if conf.SSUType == nil { + conf.SSUType = ssuType + } else { + maps.Copy(conf.SSUType, ssuType) + } +} + +func serverConfigName() string { + return "server.json" +} diff --git a/config/url.go b/config/url.go new file mode 100644 index 0000000..4f2b42c --- /dev/null +++ b/config/url.go @@ -0,0 +1,72 @@ +package config + +type urlConfig struct { + Scheme string `json:"scheme" yaml:"scheme"` + Host string `json:"host" yaml:"host"` // host or host:port + Path string `json:"path" yaml:"path"` + Timeout int `json:"timeout" yaml:"timeout"` +} + +func NewURLConfig() *urlConfig { + return new(urlConfig) +} + +func (conf *urlConfig) GetScheme() string { + if conf == nil { + panic("url config is nil") + } + return conf.Scheme +} + +func (conf *urlConfig) SetScheme(scheme string) { + if conf == nil { + panic("url config is nil") + } + conf.Scheme = scheme +} + +func (conf *urlConfig) GetHost() string { + if conf == nil { + panic("url config is nil") + } + return conf.Host +} + +func (conf *urlConfig) SetHost(host string) { + if conf == nil { + panic("url config is nil") + } + conf.Host = host +} + +func (conf *urlConfig) GetPath() string { + if conf == nil { + panic("url config is nil") + } + return conf.Path +} + +func (conf *urlConfig) SetPath(path string) { + if conf == nil { + panic("url config is nil") + } + conf.Path = path +} + +func (conf *urlConfig) GetTimeout() int { + if conf == nil { + panic("url config is nil") + } + return conf.Timeout +} + +func (conf *urlConfig) SetTimeout(timeout int) { + if conf == nil { + panic("url config is nil") + } + conf.Timeout = timeout +} + +func urlConfigName() string { + return "url.json" +} diff --git a/configs/influx.json b/configs/influx.json new file mode 100644 index 0000000..cce373f --- /dev/null +++ b/configs/influx.json @@ -0,0 +1,8 @@ +{ + "default": { + "url": "http://192.168.46.100:8086", + "token": "kKtMhMj5ISrnrCAO1ugvL4D4c_HrbAv4HzSHGA3Ai1AeBIEmGbQpQY0qSwjaXYluOmuDAv0zAvFCRRqEWQ0zJw==", + "org": "eCL3000", + "timeout":1000 + } +} \ No newline at end of file diff --git a/configs/kafka.json b/configs/kafka.json new file mode 100644 index 0000000..40a710d --- /dev/null +++ b/configs/kafka.json @@ -0,0 +1,17 @@ +{ + "default": { + "brokers": ["localhost:9092"], + "topics": ["ssu000"], + "consumer": { + "groupid": "datart_sample", + "initialoffset": -1 + }, + "producer": { + "requiredacks":-1, + "partitioner":"hash", + "returnsuccesses":true, + "returnerrors":true, + "compression":4 + } + } +} \ No newline at end of file diff --git a/configs/log.json b/configs/log.json new file mode 100644 index 0000000..92fbf19 --- /dev/null +++ b/configs/log.json @@ -0,0 +1,9 @@ +{ + "filename": "./logs/datart.log", + "maxsize": 100, + "maxage": 7, + "maxbackups": 20, + "localtime": true, + "compress": false, + "loglevel": -1 +} \ No newline at end of file diff --git a/configs/mongo.json b/configs/mongo.json new file mode 100644 index 0000000..0678e59 --- /dev/null +++ b/configs/mongo.json @@ -0,0 +1,9 @@ +{ + "default":{ + "hosts":["192.168.46.100:27017"], + "username":"mongo", + "password":"123RTYjkl", + "authsource":"events", + "authmechanism":"SCRAM-SHA-256" + } +} \ No newline at end of file diff --git a/configs/postgres.json b/configs/postgres.json new file mode 100644 index 0000000..fda7e61 --- /dev/null +++ b/configs/postgres.json @@ -0,0 +1,11 @@ +{ + "default":{ + "host":"192.168.46.100", + "port":9432, + "user":"postgres", + "password":"123RTYjkl", + "dbname":"metamodule", + "sslmode":"disable", + "timezone":"Asia/Shanghai" + } +} \ No newline at end of file diff --git a/configs/rabbit.json b/configs/rabbit.json new file mode 100644 index 0000000..cfb51e6 --- /dev/null +++ b/configs/rabbit.json @@ -0,0 +1,12 @@ +{ + "default": { + "hosts": [""], + "username": "", + "password": "", + "nxqr": { + "exchangename": "", + "queuename": "", + "routingkey": "" + } + } +} \ No newline at end of file diff --git a/configs/redis.json b/configs/redis.json new file mode 100644 index 0000000..cd92762 --- /dev/null +++ b/configs/redis.json @@ -0,0 +1,13 @@ +{ + "default":{ + "addr":"192.168.46.100:6379", + "username":"", + "password":"", + "db":0, + "resp":3, + "dialtimeout":50, + "readtimeout":200, + "writetimeout":200, + "poolsize":20 + } +} \ No newline at end of file diff --git a/configs/server.json b/configs/server.json index e69de29..5c0b12f 100644 --- a/configs/server.json +++ b/configs/server.json @@ -0,0 +1,7 @@ +{ + "host": "", + "port": 8888, + "ssutype": { + "ssu000": 0 + } +} \ No newline at end of file diff --git a/configs/url.json b/configs/url.json new file mode 100644 index 0000000..d42487c --- /dev/null +++ b/configs/url.json @@ -0,0 +1,8 @@ +{ + "default":{ + "schema":"https", + "host":"www.baidu.com", + "path":"/", + "timeout":1000 + } +} \ No newline at end of file diff --git a/go.mod b/go.mod index 2d2023f..b9ecc81 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,13 @@ module datart go 1.24.0 + +require ( + go.uber.org/zap v1.27.0 + gopkg.in/natefinch/lumberjack.v2 v2.2.1 +) + +require ( + github.com/stretchr/testify v1.9.0 // indirect + go.uber.org/multierr v1.10.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..15aea8e --- /dev/null +++ b/go.sum @@ -0,0 +1,16 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= +go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= +gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= +gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/log/log.go b/log/log.go deleted file mode 100644 index 7330d54..0000000 --- a/log/log.go +++ /dev/null @@ -1 +0,0 @@ -package log