Config manager of server is added.

This commit is contained in:
crusader 2017-07-14 12:20:37 +09:00
parent c32d2b4916
commit 97f3e920f1
4 changed files with 62 additions and 19 deletions

21
config.json Normal file
View File

@ -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
}
}
}

View File

@ -7,7 +7,7 @@ websocket:
maxMessageSize: 1024 maxMessageSize: 1024
readBufferSize: 4096 readBufferSize: 4096
writeBufferSize: 4096 writeBufferSize: 4096
grpc: gRpc:
host: localhost host: localhost
port: 8000 port: 8000
tls: false tls: false

View File

@ -1,13 +1,17 @@
package config 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 { type Config struct {
Websocket Websocket Websocket WebsocketConfig `json:"websocket"`
Grpc GRpc GRpc GRpcConfig `json:"gRpc"`
} }
type Websocket struct { type WebsocketConfig struct {
WriteTimeout int8 `default:"0"` WriteTimeout int8 `default:"0"`
ReadTimeout int8 `default:"0"` ReadTimeout int8 `default:"0"`
PongTimeout int8 `default:"60"` PongTimeout int8 `default:"60"`
@ -17,17 +21,31 @@ type Websocket struct {
WriteBufferSize int `default:"4096"` WriteBufferSize int `default:"4096"`
} }
type GRpc struct { type GRpcConfig struct {
Host string `required:"true"` Host string `required:"true"`
Port int16 `required:"true"` Port int16 `required:"true"`
Tls bool `default:"false"` Tls bool `default:"false"`
Pool PoolConfig `json:"pool"`
} }
func LoadConfig(filename string) (*Config, error) { type PoolConfig struct {
var c Config InitialCapacity int `defalut:"1"`
err := config.Load(&c, filename) MaxCapacity int `defalut:"1"`
if nil != err { IncreaseCapacity int `defalut:"1"`
return nil, err }
}
return &c, nil 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
} }

View File

@ -10,9 +10,13 @@ import (
func TestLoadConfig(t *testing.T) { func TestLoadConfig(t *testing.T) {
os.Chdir("../") os.Chdir("../")
wd, _ := os.Getwd() 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)
} }