Skip to content

Commit

Permalink
Auto assign server_id for bootstrapped config files.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tidwall committed Jan 4, 2022
1 parent 6c7d523 commit b6833a2
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions internal/server/config.go
Expand Up @@ -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(),
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b6833a2

Please sign in to comment.