Config manager of server is added.
This commit is contained in:
parent
c32d2b4916
commit
97f3e920f1
21
config.json
Normal file
21
config.json
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@ websocket:
|
|||
maxMessageSize: 1024
|
||||
readBufferSize: 4096
|
||||
writeBufferSize: 4096
|
||||
grpc:
|
||||
gRpc:
|
||||
host: localhost
|
||||
port: 8000
|
||||
tls: false
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user