From 60dbbe55e94597e23305ebb7ed1ddbb074fa25b7 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 22 May 2019 16:12:44 +0200 Subject: [PATCH] Make default api listen address configurable, fix race conditions --- api/config.go | 15 +++++++++++---- api/database.go | 5 ++++- config/main.go | 12 ++++++------ 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/api/config.go b/api/config.go index 420572c..e146f0b 100644 --- a/api/config.go +++ b/api/config.go @@ -8,8 +8,9 @@ import ( ) var ( - listenAddressFlag string - listenAddressConfig config.StringOption + listenAddressFlag string + listenAddressConfig config.StringOption + defaultListenAddress string ) func init() { @@ -37,13 +38,19 @@ func registerConfig() error { Description: "Define on what IP and port the API should listen on. Be careful, changing this may become a security issue.", ExpertiseLevel: config.ExpertiseLevelExpert, OptType: config.OptTypeString, - DefaultValue: "127.0.0.1:18", + DefaultValue: defaultListenAddress, ValidationRegex: "^([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}:[0-9]{1,5}|\\[[:0-9A-Fa-f]+\\]:[0-9]{1,5})$", }) if err != nil { return err } - listenAddressConfig = config.GetAsString("api/listenAddress", "127.0.0.1:18") + listenAddressConfig = config.GetAsString("api/listenAddress", defaultListenAddress) return nil } + +func SetDefaultAPIListenAddress(address string) { + if defaultListenAddress == "" { + defaultListenAddress = address + } +} diff --git a/api/database.go b/api/database.go index e025913..22415fc 100644 --- a/api/database.go +++ b/api/database.go @@ -367,7 +367,10 @@ func (api *DatabaseAPI) processSub(opID []byte, sub *database.Subscription) { continue } // TODO: use upd, new and delete msgTypes - if r.Meta().IsDeleted() { + r.Lock() + isDeleted := r.Meta().IsDeleted() + r.Unlock() + if isDeleted { api.send(opID, dbMsgTypeDel, r.Key(), nil) } else { api.send(opID, dbMsgTypeUpd, r.Key(), data) diff --git a/config/main.go b/config/main.go index 99e9057..c452c3c 100644 --- a/config/main.go +++ b/config/main.go @@ -9,7 +9,7 @@ import ( ) func init() { - modules.Register("config", prep, start, stop, "database") + modules.Register("config", prep, start, nil, "database") } func prep() error { @@ -19,14 +19,14 @@ func prep() error { func start() error { configFilePath = path.Join(database.GetDatabaseRoot(), "config.json") - err := loadConfig() + err := registerAsDatabase() if err != nil && !os.IsNotExist(err) { return err } - return registerAsDatabase() -} - -func stop() error { + err = loadConfig() + if err != nil && !os.IsNotExist(err) { + return err + } return nil }