diff --git a/conf/conf.go b/conf/conf.go new file mode 100644 index 0000000..0e17170 --- /dev/null +++ b/conf/conf.go @@ -0,0 +1,16 @@ +package conf + +import ( + ofc "git.loafle.net/overflow/overflow_config" +) + +var Config Conf + +type Conf struct { + Server ofc.Server `json:"server" yaml:"server" toml:"server"` + Auth ofc.Auth `json:"auth" yaml:"auth" toml:"auth"` + Websocket ofc.Websocket `json:"websocket" yaml:"websocket" toml:"websocket"` + GRPC ofc.GRPC `json:"gRPC" yaml:"gRPC" toml:"gRPC"` + Redis ofc.Redis `json:"redis" yaml:"redis" toml:"redis"` + Handlers map[string]ofc.Handler `json:"handlers" yaml:"handlers" toml:"handlers"` +} diff --git a/config.json b/config.json index b805337..fcddc98 100644 --- a/config.json +++ b/config.json @@ -12,7 +12,7 @@ "WriteBufferSize": 8192, "EnableCompression": false }, - "grpc": { + "gRPC": { "addr": "192.168.1.50:50006", "tls": false, "pool": { @@ -57,33 +57,6 @@ } } - }, - "logging": { - "level": "debug", - "development": true, - "disableCaller": true, - "disableStacktrace": true, - "sampling": { - "initial": 100, - "thereafter": 100 - }, - "encoding": "console", - "encoderConfig": { - "messageKey": "message", - "levelKey": "level", - "timeKey": "time", - "nameKey": "name", - "callerKey": "caller", - "stacktraceKey": "stacktrace", - "lineEnding": "\n", - "levelEncoder": "color", - "timeEncoder": "ISO8601", - "durationEncoder": "string", - "callerEncoder": "full", - "nameEncoder": "full" - }, - "outputPaths": ["stdout", "/tmp/logs"], - "errorOutputPaths": ["stderr"] } } diff --git a/glide.yaml b/glide.yaml index a8eba8d..fd71b29 100644 --- a/glide.yaml +++ b/glide.yaml @@ -28,3 +28,4 @@ import: - package: google.golang.org/grpc subpackages: - metadata +- package: git.loafle.net/overflow/overflow_config diff --git a/grpc/pool.go b/grpc/pool.go index 2aa8cb2..ec205d9 100644 --- a/grpc/pool.go +++ b/grpc/pool.go @@ -4,9 +4,9 @@ import ( "context" "fmt" - "git.loafle.net/commons_go/config" cgp "git.loafle.net/commons_go/grpc_pool" "git.loafle.net/commons_go/logging" + "git.loafle.net/overflow/overflow_gateway_probe/conf" ) var _pool cgp.Pool @@ -16,9 +16,8 @@ func InitializePool(ctx context.Context) { h := &poolHandlers{ ctx: ctx, } - h.cfg = config.Sub("grpc") - h.MaxIdle = h.cfg.GetInt("pool.MaxIdle") - h.MaxCapacity = h.cfg.GetInt("pool.MaxCapacity") + h.MaxIdle = conf.Config.GRPC.Pool.MaxIdle + h.MaxCapacity = conf.Config.GRPC.Pool.MaxCapacity _pool, err = cgp.New(ctx, h) if nil != err { diff --git a/grpc/pool_handlers.go b/grpc/pool_handlers.go index a79bdcd..15dd1a1 100644 --- a/grpc/pool_handlers.go +++ b/grpc/pool_handlers.go @@ -5,20 +5,19 @@ import ( "google.golang.org/grpc" - "git.loafle.net/commons_go/config" cgp "git.loafle.net/commons_go/grpc_pool" oas "git.loafle.net/overflow/overflow_api_server/golang" + "git.loafle.net/overflow/overflow_gateway_probe/conf" ) type poolHandlers struct { cgp.PoolHandlers ctx context.Context - cfg config.Configurator } func (h *poolHandlers) OnCreate() (*grpc.ClientConn, interface{}, error) { var err error - conn, err := grpc.Dial(config.GetString("grpc.addr"), grpc.WithInsecure()) + conn, err := grpc.Dial(conf.Config.GRPC.Addr, grpc.WithInsecure()) if nil != err { return nil, nil, err } diff --git a/handler/auth_socket_handlers.go b/handler/auth_socket_handlers.go index 1a37859..7dba502 100644 --- a/handler/auth_socket_handlers.go +++ b/handler/auth_socket_handlers.go @@ -5,27 +5,28 @@ import ( "time" "git.loafle.net/commons_go/config" + "git.loafle.net/overflow/overflow_gateway_probe/conf" ogw "git.loafle.net/overflow/overflow_gateway_websocket" ) type authSocketHandlers struct { ogw.SocketHandlers ctx context.Context - cfg config.Configurator } func NewAuthSocketHandler(ctx context.Context) ogw.SocketHandler { h := &authSocketHandlers{ ctx: ctx, } - h.cfg = config.Sub("handlers.auth") - h.MaxMessageSize = h.cfg.GetInt64("socket.MaxMessageSize") - h.WriteTimeout = h.cfg.GetDuration("socket.WriteTimeout") * time.Second - h.ReadTimeout = h.cfg.GetDuration("socket.ReadTimeout") * time.Second - h.PongTimeout = h.cfg.GetDuration("socket.PongTimeout") * time.Second - h.PingTimeout = h.cfg.GetDuration("socket.PingTimeout") * time.Second - h.PingPeriod = h.cfg.GetDuration("socket.PingPeriod") * time.Second - h.BinaryMessage = h.cfg.GetBool("socket.BinaryMessage") + cfg := conf.Config.Handlers["auth"] + + h.MaxMessageSize = cfg.Socket.MaxMessageSize + h.WriteTimeout = cfg.Socket.WriteTimeout * time.Second + h.ReadTimeout = cfg.Socket.ReadTimeout * time.Second + h.PongTimeout = cfg.Socket.PongTimeout * time.Second + h.PingTimeout = cfg.Socket.PingTimeout * time.Second + h.PingPeriod = cfg.Socket.PingPeriod * time.Second + h.BinaryMessage = cfg.Socket.BinaryMessage ph := newAuthJSONRpcHandler(ctx) diff --git a/handler/probe_socket_handlers.go b/handler/probe_socket_handlers.go index e807dd1..49540f7 100644 --- a/handler/probe_socket_handlers.go +++ b/handler/probe_socket_handlers.go @@ -5,27 +5,28 @@ import ( "time" "git.loafle.net/commons_go/config" + "git.loafle.net/overflow/overflow_gateway_probe/conf" ogw "git.loafle.net/overflow/overflow_gateway_websocket" ) type probeSocketHandlers struct { ogw.SocketHandlers ctx context.Context - cfg config.Configurator } func NewProbeSocketHandler(ctx context.Context) ogw.SocketHandler { h := &probeSocketHandlers{ ctx: ctx, } - h.cfg = config.Sub("handlers.probe") - h.MaxMessageSize = h.cfg.GetInt64("socket.MaxMessageSize") - h.WriteTimeout = h.cfg.GetDuration("socket.WriteTimeout") * time.Second - h.ReadTimeout = h.cfg.GetDuration("socket.ReadTimeout") * time.Second - h.PongTimeout = h.cfg.GetDuration("socket.PongTimeout") * time.Second - h.PingTimeout = h.cfg.GetDuration("socket.PingTimeout") * time.Second - h.PingPeriod = h.cfg.GetDuration("socket.PingPeriod") * time.Second - h.BinaryMessage = h.cfg.GetBool("socket.BinaryMessage") + cfg := conf.Config.Handlers["probe"] + + h.MaxMessageSize = cfg.Socket.MaxMessageSize + h.WriteTimeout = cfg.Socket.WriteTimeout * time.Second + h.ReadTimeout = cfg.Socket.ReadTimeout * time.Second + h.PongTimeout = cfg.Socket.PongTimeout * time.Second + h.PingTimeout = cfg.Socket.PingTimeout * time.Second + h.PingPeriod = cfg.Socket.PingPeriod * time.Second + h.BinaryMessage = cfg.Socket.BinaryMessage ph := newProbeJSONRpcHandler(ctx) diff --git a/main.go b/main.go index 835f239..0d8b0e0 100644 --- a/main.go +++ b/main.go @@ -3,10 +3,10 @@ package main import ( "context" "flag" - "log" "git.loafle.net/commons_go/config" "git.loafle.net/commons_go/logging" + "git.loafle.net/overflow/overflow_gateway_probe/conf" "git.loafle.net/overflow/overflow_gateway_probe/grpc" "git.loafle.net/overflow/overflow_gateway_probe/handler" "git.loafle.net/overflow/overflow_gateway_probe/redis" @@ -19,7 +19,10 @@ func main() { flag.Parse() - loadConfig(*configPath) + config.SetConfigPath(*configPath) + if err := config.Load(&conf.Config, "config.json"); nil != err { + panic(err) + } ctx := context.Background() @@ -34,17 +37,8 @@ func main() { subscribe.Subscribe(ctx, rp.Get()) - s.HandleSocket(config.GetString("handlers.probe.entry"), psh) - s.HandleSocket(config.GetString("handlers.auth.entry"), ash) + s.HandleSocket(conf.Config.Handlers["probe"].Entry, psh) + s.HandleSocket(conf.Config.Handlers["auth"].Entry, ash) - s.ListenAndServe(config.GetString("server.addr")) -} - -func loadConfig(path string) { - config.SetConfigName("config") - config.AddConfigPath(path) - err := config.ReadInConfig() - if nil != err { - log.Fatalf("config error: %v", err) - } + s.ListenAndServe(conf.Config.Server.Addr) } diff --git a/redis/pool.go b/redis/pool.go index 45d6d5f..331366c 100644 --- a/redis/pool.go +++ b/redis/pool.go @@ -4,17 +4,16 @@ import ( "context" "time" - "git.loafle.net/commons_go/config" rp "git.loafle.net/commons_go/redis_pool" + "git.loafle.net/overflow/overflow_gateway_probe/conf" ) func NewPool(ctx context.Context) rp.Pool { h := &poolHandlers{ ctx: ctx, } - h.cfg = config.Sub("redis") - h.MaxIdle = h.cfg.GetInt("pool.MaxIdle") - h.IdleTimeout = h.cfg.GetDuration("pool.IdleTimeout") * time.Second + h.MaxIdle = conf.Config.Redis.Pool.MaxIdle + h.IdleTimeout = conf.Config.Redis.Pool.IdleTimeout * time.Second p := rp.NewPool(ctx, h) diff --git a/redis/pool_handlers.go b/redis/pool_handlers.go index c82aa44..d415362 100644 --- a/redis/pool_handlers.go +++ b/redis/pool_handlers.go @@ -6,8 +6,8 @@ import ( "github.com/garyburd/redigo/redis" - "git.loafle.net/commons_go/config" rp "git.loafle.net/commons_go/redis_pool" + "git.loafle.net/overflow/overflow_gateway_probe/conf" "go.uber.org/zap" ) @@ -15,11 +15,10 @@ type poolHandlers struct { rp.PoolHandlers ctx context.Context logger *zap.Logger - cfg config.Configurator } func (h *poolHandlers) Dial() (redis.Conn, error) { - return redis.Dial(h.cfg.GetString("network"), h.cfg.GetString("addr")) + return redis.Dial(conf.Config.Redis.Network, conf.Config.Redis.Addr) } func (h *poolHandlers) TestOnBorrow(c redis.Conn, t time.Time) error { diff --git a/server/server.go b/server/server.go index c2d7f52..80bd382 100644 --- a/server/server.go +++ b/server/server.go @@ -3,7 +3,7 @@ package server import ( "context" - "git.loafle.net/commons_go/config" + "git.loafle.net/overflow/overflow_gateway_probe/conf" ogw "git.loafle.net/overflow/overflow_gateway_websocket" ) @@ -13,7 +13,7 @@ func NewServer(ctx context.Context) ogw.Server { h := newServerHandler(ctx) _s := ogw.NewServer(ctx, h) - ofSigningKey = []byte(config.GetString("auth.signingKey")) + ofSigningKey = []byte(conf.Config.Auth.SigningKey) return _s } diff --git a/server/server_handlers.go b/server/server_handlers.go index dcd0572..a9213ee 100644 --- a/server/server_handlers.go +++ b/server/server_handlers.go @@ -6,6 +6,7 @@ import ( "time" "git.loafle.net/commons_go/config" + "git.loafle.net/overflow/overflow_gateway_probe/conf" jwt "github.com/dgrijalva/jwt-go" "github.com/valyala/fasthttp" @@ -18,11 +19,10 @@ func newServerHandler(ctx context.Context) ogw.ServerHandler { h := &serverHandlers{ ctx: ctx, } - h.cfg = config.Sub("websocket") - h.HandshakeTimeout = h.cfg.GetDuration("HandshakeTimeout") * time.Second - h.ReadBufferSize = h.cfg.GetInt("ReadBufferSize") - h.WriteBufferSize = h.cfg.GetInt("WriteBufferSize") - h.EnableCompression = h.cfg.GetBool("EnableCompression") + h.HandshakeTimeout = conf.Config.Websocket.HandshakeTimeout * time.Second + h.ReadBufferSize = conf.Config.Websocket.ReadBufferSize + h.WriteBufferSize = conf.Config.Websocket.WriteBufferSize + h.EnableCompression = conf.Config.Websocket.EnableCompression return h }