diff --git a/inferium-harvester.lua b/inferium-harvester.lua index d84f625..c884978 100644 --- a/inferium-harvester.lua +++ b/inferium-harvester.lua @@ -321,7 +321,7 @@ end local function fetchRemoteConfig() print('> fetch remote config') - local message = { type = 'get-config' } + local message = { type = 'register-and-get-config' } local lastErrMessage = nil diff --git a/inferium-server.lua b/inferium-server.lua index 17339de..3f9eed8 100644 --- a/inferium-server.lua +++ b/inferium-server.lua @@ -89,25 +89,49 @@ end -- Routes -local function getConfig(message, computerId) +local function getConfig(message) + local id = message and message.payload and message.payload.id + + if not id then + return { + type = "get-config/error", + payload = nil, + errorMessage = "no id provided in message payload" + } + end + + local config = getConfigForComputer(id) + + if not config then + return { + type = "get-config/error", + payload = nil, + errorMessage = "cannot retrieve configuration for given id" + } + end + + return { + type = "get-config/response", + payload = getConfigWithLength(config) + } +end + +local function registerAndGetConfig(_, computerId) if not computerId == nil then print('get-config error: no computerId found') return nil end - local givenHarvesterId = message and message.payload and message.payload.id + local config = getConfigForComputer(computerId) - local id = givenHarvesterId or computerId - local config = getConfigForComputer(id) - - if not givenHarvesterId and not config then + if not config then print('new harvester detected: ' .. tostring(computerId)) saveConfigForComputer(computerId, defaultConfig) config = defaultConfig end return { - type = "get-config/response", + type = "register-and-get-config/response", payload = getConfigWithLength(config) } end @@ -182,6 +206,7 @@ local function upgradeServer() end local ROUTES = { + ['register-and-get-config'] = registerAndGetConfig, ['get-config'] = getConfig, ['set-config'] = setConfig, ['delete-config'] = deleteConfig,