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:
tidwall 2022-01-04 05:13:16 -07:00
parent 6c7d523d4d
commit b6833a2dba
1 changed files with 9 additions and 4 deletions

View File

@ -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,8 +194,10 @@ func (config *Config) write(writeProperties bool) {
if config._logConfigP != "" {
var lcfg map[string]interface{}
json.Unmarshal([]byte(config._logConfig), &lcfg)
if len(lcfg) > 0 {
m[LogConfig] = lcfg
}
}
data, err := json.MarshalIndent(m, "", "\t")
if err != nil {
panic(err)