From 97f3e920f162395c66423bdec87f436a0ddfe692 Mon Sep 17 00:00:00 2001 From: crusader Date: Fri, 14 Jul 2017 12:20:37 +0900 Subject: [PATCH] Config manager of server is added. --- config.json | 21 +++++++++++++++++++ config.yml | 2 +- config/config.go | 48 +++++++++++++++++++++++++++++-------------- config/config_test.go | 10 ++++++--- 4 files changed, 62 insertions(+), 19 deletions(-) create mode 100644 config.json diff --git a/config.json b/config.json new file mode 100644 index 0000000..325601f --- /dev/null +++ b/config.json @@ -0,0 +1,21 @@ +{ + "websocket": { + "writeTimeout": 0, + "readTimeout": 0, + "pongTimeout": 60, + "pingTimeout": 10, + "maxMessageSize": 1024, + "readBufferSize": 4096, + "writeBufferSize": 4096 + }, + "gRpc": { + "host": "localhost", + "port": 8000, + "tls": false, + "pool": { + "initialCapacity": 30, + "maxCapacity": 100, + "increaseCapacity": 10 + } + } +} \ No newline at end of file diff --git a/config.yml b/config.yml index 55e8a1b..ef04314 100644 --- a/config.yml +++ b/config.yml @@ -7,7 +7,7 @@ websocket: maxMessageSize: 1024 readBufferSize: 4096 writeBufferSize: 4096 -grpc: +gRpc: host: localhost port: 8000 tls: false diff --git a/config/config.go b/config/config.go index 645a7ca..5025c5d 100644 --- a/config/config.go +++ b/config/config.go @@ -1,13 +1,17 @@ package config -import "git.loafle.net/overflow/overflow-service-websocket/commons/config" +import ( + "git.loafle.net/overflow/overflow_service_websocket/commons/config" +) + +var _config *Config type Config struct { - Websocket Websocket - Grpc GRpc + Websocket WebsocketConfig `json:"websocket"` + GRpc GRpcConfig `json:"gRpc"` } -type Websocket struct { +type WebsocketConfig struct { WriteTimeout int8 `default:"0"` ReadTimeout int8 `default:"0"` PongTimeout int8 `default:"60"` @@ -17,17 +21,31 @@ type Websocket struct { WriteBufferSize int `default:"4096"` } -type GRpc struct { - Host string `required:"true"` - Port int16 `required:"true"` - Tls bool `default:"false"` +type GRpcConfig struct { + Host string `required:"true"` + Port int16 `required:"true"` + Tls bool `default:"false"` + Pool PoolConfig `json:"pool"` } -func LoadConfig(filename string) (*Config, error) { - var c Config - err := config.Load(&c, filename) - if nil != err { - return nil, err - } - return &c, nil +type PoolConfig struct { + InitialCapacity int `defalut:"1"` + MaxCapacity int `defalut:"1"` + IncreaseCapacity int `defalut:"1"` +} + +func InitConfig(path string) error { + if nil != _config { + return nil + } + _config = &Config{} + err := config.Load(_config, path) + if nil != err { + return err + } + return nil +} + +func GetConfig() *Config { + return _config } diff --git a/config/config_test.go b/config/config_test.go index ae61266..fd8bed5 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -10,9 +10,13 @@ import ( func TestLoadConfig(t *testing.T) { os.Chdir("../") wd, _ := os.Getwd() - path := filepath.Join(wd, "config.yml") + path := filepath.Join(wd, "config.json") - c, _ := LoadConfig(path) + err := InitConfig(path) + if nil != err { + log.Fatalln("Config error") + } + c := GetConfig() - log.Println(c.Grpc.Host) + log.Println(c.Websocket.MaxMessageSize) }