mirror of https://github.com/tidwall/tile38.git
Auto assign server_id for bootstrapped config files.
This commit fixes an issue where the server may start up without a "server_id" assigned, which in turn will cause a follower to be unable to connect. This issues is caused by including a pre-generated "data/config" file that does not include the "server_id" field.
This commit is contained in:
parent
6c7d523d4d
commit
b6833a2dba
|
@ -71,14 +71,13 @@ func loadConfig(path string) (*Config, error) {
|
|||
var json string
|
||||
data, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
json = `{"` + ServerID + `":"` + randomKey(16) + `"}`
|
||||
} else {
|
||||
if !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
json = string(data)
|
||||
}
|
||||
|
||||
config := &Config{
|
||||
path: path,
|
||||
_followHost: gjson.Get(json, FollowHost).String(),
|
||||
|
@ -96,6 +95,10 @@ func loadConfig(path string) (*Config, error) {
|
|||
_logConfig: gjson.Get(json, LogConfig).String(),
|
||||
}
|
||||
|
||||
if config._serverID == "" {
|
||||
config._serverID = randomKey(16)
|
||||
}
|
||||
|
||||
// load properties
|
||||
if err := config.setProperty(RequirePass, config._requirePassP, true); err != nil {
|
||||
return nil, err
|
||||
|
@ -191,7 +194,9 @@ func (config *Config) write(writeProperties bool) {
|
|||
if config._logConfigP != "" {
|
||||
var lcfg map[string]interface{}
|
||||
json.Unmarshal([]byte(config._logConfig), &lcfg)
|
||||
m[LogConfig] = lcfg
|
||||
if len(lcfg) > 0 {
|
||||
m[LogConfig] = lcfg
|
||||
}
|
||||
}
|
||||
data, err := json.MarshalIndent(m, "", "\t")
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue