From 9d08dbd06eb6b048763d06fabe4ae23423408424 Mon Sep 17 00:00:00 2001 From: Douglas Gubert Date: Fri, 12 Jul 2019 17:57:47 -0300 Subject: [PATCH] [IMPROVE] Extract federation config to its own file (#14992) * Revert "[IMPROVE] Federation routes #14972" * Extract federation config logic to its own file --- .../server/PeerServer/routes/events.js | 6 +- .../server/PeerServer/routes/uploads.js | 6 +- .../server/PeerServer/routes/users.js | 8 +- app/federation/server/config.js | 74 +++++++++++++++++++ app/federation/server/index.js | 36 +-------- 5 files changed, 79 insertions(+), 51 deletions(-) create mode 100644 app/federation/server/config.js diff --git a/app/federation/server/PeerServer/routes/events.js b/app/federation/server/PeerServer/routes/events.js index fbeaf0dc7741..45153c80bd73 100644 --- a/app/federation/server/PeerServer/routes/events.js +++ b/app/federation/server/PeerServer/routes/events.js @@ -4,7 +4,7 @@ import { API } from '../../../../api'; import { FederationKeys } from '../../../../models'; import { Federation } from '../..'; -const routeMethods = { +API.v1.addRoute('federation.events', { authRequired: false }, { post() { if (!Federation.peerServer.enabled) { return API.v1.failure('Not found'); @@ -112,8 +112,4 @@ const routeMethods = { return API.v1.failure(`Error handling event:${ e.t } - ${ err.toString() }`, err.error || 'unknown-error'); } }, -}; - -Meteor.startup(() => { - API.v1.addRoute('federation.events', { authRequired: false }, routeMethods); }); diff --git a/app/federation/server/PeerServer/routes/uploads.js b/app/federation/server/PeerServer/routes/uploads.js index 8d15b82f6e10..ec24f75ca2ba 100644 --- a/app/federation/server/PeerServer/routes/uploads.js +++ b/app/federation/server/PeerServer/routes/uploads.js @@ -5,7 +5,7 @@ import { Uploads } from '../../../../models'; import { FileUpload } from '../../../../file-upload'; import { Federation } from '../..'; -const routeMethods = { +API.v1.addRoute('federation.uploads', { authRequired: false }, { get() { if (!Federation.peerServer.enabled) { return API.v1.failure('Not found'); @@ -25,8 +25,4 @@ const routeMethods = { return API.v1.success({ upload, buffer }); }, -}; - -Meteor.startup(() => { - API.v1.addRoute('federation.uploads', { authRequired: false }, routeMethods); }); diff --git a/app/federation/server/PeerServer/routes/users.js b/app/federation/server/PeerServer/routes/users.js index 726871bf2e71..89006ad583a9 100644 --- a/app/federation/server/PeerServer/routes/users.js +++ b/app/federation/server/PeerServer/routes/users.js @@ -1,11 +1,9 @@ -import { Meteor } from 'meteor/meteor'; - import { API } from '../../../../api'; import { Users } from '../../../../models'; import { FederatedUser } from '../../federatedResources'; import { Federation } from '../..'; -const routeMethods = { +API.v1.addRoute('federation.users', { authRequired: false }, { get() { if (!Federation.peerServer.enabled) { return API.v1.failure('Not found'); @@ -47,8 +45,4 @@ const routeMethods = { return API.v1.success({ federatedUsers }); }, -}; - -Meteor.startup(() => { - API.v1.addRoute('federation.users', { authRequired: false }, routeMethods); }); diff --git a/app/federation/server/config.js b/app/federation/server/config.js new file mode 100644 index 000000000000..b8f1f2b12287 --- /dev/null +++ b/app/federation/server/config.js @@ -0,0 +1,74 @@ +import mem from 'mem'; + +import { getWorkspaceAccessToken } from '../../cloud/server'; +import { FederationKeys } from '../../models/server'; +import { settings } from '../../settings/server'; +import * as SettingsUpdater from './settingsUpdater'; +import { logger } from './logger'; + +const defaultConfig = { + hub: { + active: null, + url: null, + }, + peer: { + uniqueId: null, + domain: null, + url: null, + public_key: null, + }, + cloud: { + token: null, + }, +}; + +const getConfigLocal = () => { + const _enabled = settings.get('FEDERATION_Enabled'); + + if (!_enabled) { return defaultConfig; } + + // If it is enabled, check if the settings are there + const _uniqueId = settings.get('FEDERATION_Unique_Id'); + const _domain = settings.get('FEDERATION_Domain'); + const _discoveryMethod = settings.get('FEDERATION_Discovery_Method'); + const _hubUrl = settings.get('FEDERATION_Hub_URL'); + const _peerUrl = settings.get('Site_Url'); + + if (!_domain || !_discoveryMethod || !_hubUrl || !_peerUrl) { + SettingsUpdater.updateStatus('Could not enable, settings are not fully set'); + + logger.setup.error('Could not enable Federation, settings are not fully set'); + + return defaultConfig; + } + + logger.setup.info('Updating settings...'); + + // Normalize the config values + return { + hub: { + active: _discoveryMethod === 'hub', + url: _hubUrl.replace(/\/+$/, ''), + }, + peer: { + uniqueId: _uniqueId, + domain: _domain.replace('@', '').trim(), + url: _peerUrl.replace(/\/+$/, ''), + public_key: FederationKeys.getPublicKeyString(), + }, + cloud: { + token: getWorkspaceAccessToken(), + }, + }; +}; + +export const getConfig = mem(getConfigLocal); + +const updateValue = () => mem.clear(getConfig); + +settings.get('FEDERATION_Enabled', updateValue); +settings.get('FEDERATION_Unique_Id', updateValue); +settings.get('FEDERATION_Domain', updateValue); +settings.get('FEDERATION_Status', updateValue); +settings.get('FEDERATION_Discovery_Method', updateValue); +settings.get('FEDERATION_Hub_URL', updateValue); diff --git a/app/federation/server/index.js b/app/federation/server/index.js index 834a67c10f6a..f0c6844e86a1 100644 --- a/app/federation/server/index.js +++ b/app/federation/server/index.js @@ -13,9 +13,9 @@ import './methods/dashboard'; import { addUser } from './methods/addUser'; import { searchUsers } from './methods/searchUsers'; import { ping } from './methods/ping'; -import { getWorkspaceAccessToken } from '../../cloud/server'; import { FederationKeys } from '../../models'; import { settings } from '../../settings'; +import { getConfig } from './config'; const peerClient = new PeerClient(); const peerDNS = new PeerDNS(); @@ -73,39 +73,7 @@ const updateSettings = _.debounce(Meteor.bindEnvironment(function() { if (!_enabled) { return; } - // If it is enabled, check if the settings are there - const _uniqueId = settings.get('FEDERATION_Unique_Id'); - const _domain = settings.get('FEDERATION_Domain'); - const _discoveryMethod = settings.get('FEDERATION_Discovery_Method'); - const _hubUrl = settings.get('FEDERATION_Hub_URL'); - const _peerUrl = settings.get('Site_Url'); - - if (!_domain || !_discoveryMethod || !_hubUrl || !_peerUrl) { - SettingsUpdater.updateStatus('Could not enable, settings are not fully set'); - - logger.setup.error('Could not enable Federation, settings are not fully set'); - - return; - } - - logger.setup.info('Updating settings...'); - - // Normalize the config values - const config = { - hub: { - active: _discoveryMethod === 'hub', - url: _hubUrl.replace(/\/+$/, ''), - }, - peer: { - uniqueId: _uniqueId, - domain: _domain.replace('@', '').trim(), - url: _peerUrl.replace(/\/+$/, ''), - public_key: FederationKeys.getPublicKeyString(), - }, - cloud: { - token: getWorkspaceAccessToken(), - }, - }; + const config = getConfig(); // If the settings are correctly set, let's update the configuration