Files
2022-03-17 15:59:24 +08:00

181 lines
4.3 KiB
Go

package config
import (
"flag"
"github.com/uber/jaeger-client-go"
traceConfig "github.com/uber/jaeger-client-go/config"
xlog "gitlab.33.cn/chat/dtalk/pkg/log"
"os"
"time"
"github.com/BurntSushi/toml"
"gitlab.33.cn/chat/dtalk/pkg/net/grpc"
xgrpc "gitlab.33.cn/chat/dtalk/pkg/net/grpc"
xtime "gitlab.33.cn/chat/dtalk/pkg/time"
)
var (
confPath string
regAddrs string
env string
Conf *Config
)
func init() {
var (
defAddrs = os.Getenv("REGADDRS")
defEnv = os.Getenv("DTALKENV")
)
flag.StringVar(&confPath, "conf", "answer.toml", "default config path.")
flag.StringVar(&regAddrs, "reg", defAddrs, "etcd register addrs. eg:127.0.0.1:2379")
flag.StringVar(&env, "env", defEnv, "service runtime environment")
}
// Init init config.
func Init() (err error) {
Conf = Default()
_, err = toml.DecodeFile(confPath, &Conf)
return
}
func Default() *Config {
return &Config{
AppId: "dtalk",
Engine: "standard",
Env: env,
Log: xlog.Config{
Level: xlog.DebugLevel,
Mode: xlog.ConsoleMode,
Path: "",
Display: xlog.JsonDisplay,
},
Trace: traceConfig.Configuration{
ServiceName: "answer",
Gen128Bit: true,
Sampler: &traceConfig.SamplerConfig{
Type: jaeger.SamplerTypeConst,
Param: 1,
},
Reporter: &traceConfig.ReporterConfig{
LogSpans: true,
LocalAgentHostPort: "127.0.0.1:6831",
},
},
GRPCServer: &xgrpc.ServerConfig{
Network: "tcp",
Addr: ":30002",
Timeout: xtime.Duration(time.Second),
KeepAliveMaxConnectionIdle: xtime.Duration(time.Second * 60),
KeepAliveMaxConnectionAge: xtime.Duration(time.Hour * 2),
KeepAliveMaxMaxConnectionAgeGrace: xtime.Duration(time.Second * 20),
KeepAliveTime: xtime.Duration(time.Second * 60),
KeepAliveTimeout: xtime.Duration(time.Second * 20),
},
Reg: &Reg{
Schema: "dtalk",
SrvName: "answer",
RegAddrs: regAddrs,
},
Redis: &Redis{
Network: "tcp",
Addr: "127.0.0.1:6379",
Auth: "",
Active: 60000,
Idle: 1024,
DialTimeout: xtime.Duration(200 * time.Millisecond),
ReadTimeout: xtime.Duration(500 * time.Millisecond),
WriteTimeout: xtime.Duration(500 * time.Millisecond),
IdleTimeout: xtime.Duration(120 * time.Second),
Expire: xtime.Duration(30 * time.Minute),
},
IdGenRPCClient: &RPCClient{
RegAddrs: "127.0.0.1:2379",
Schema: "dtalk",
SrvName: "generator",
Dial: xtime.Duration(time.Second),
Timeout: xtime.Duration(time.Second),
},
LogicRPCClient: &RPCClient{
RegAddrs: "127.0.0.1:2379",
Schema: "im",
SrvName: "logic",
Dial: xtime.Duration(time.Second),
Timeout: xtime.Duration(time.Second),
},
GroupRPCClient: &RPCClient{
RegAddrs: "127.0.0.1:2379",
Schema: "dtalk",
SrvName: "group",
Dial: xtime.Duration(time.Second),
Timeout: xtime.Duration(time.Second),
},
MQSub: &MQSubClient{
Brokers: []string{"127.0.0.1:9092"},
Number: 16,
MaxWorker: 1024,
},
MQPub: &MQPubServer{
Brokers: []string{"127.0.0.1:9092"},
},
}
}
type Config struct {
AppId string
Engine string
Env string
Log xlog.Config
Trace traceConfig.Configuration
//gRPC server
GRPCServer *grpc.ServerConfig
Reg *Reg
Redis *Redis
IdGenRPCClient *RPCClient
LogicRPCClient *RPCClient
GroupRPCClient *RPCClient
MQSub *MQSubClient
MQPub *MQPubServer
}
// Redis .
type Redis struct {
Network string
Addr string
Auth string
Active int
Idle int
DialTimeout xtime.Duration
ReadTimeout xtime.Duration
WriteTimeout xtime.Duration
IdleTimeout xtime.Duration
Expire xtime.Duration
}
// Reg is service register/discovery config
type Reg struct {
Schema string
SrvName string // call
RegAddrs string // etcd addrs, seperate by ','
}
// RPCClient is RPC client config.
type RPCClient struct {
RegAddrs string // etcd addrs, seperate by ','
Schema string
SrvName string // call
Dial xtime.Duration
Timeout xtime.Duration
}
type MQSubClient struct {
Brokers []string
Number uint32
MaxWorker int
}
// Kafka .
type MQPubServer struct {
Brokers []string
}