From 1d12545ddcd39eb17b802479da105017da49c0e2 Mon Sep 17 00:00:00 2001 From: Shlomo Zalman Heigh Date: Thu, 10 Sep 2020 15:31:34 -0400 Subject: [PATCH 1/3] companion: add option to hide welcome and metrics --- packages/@uppy/companion/env.test.sh | 2 + packages/@uppy/companion/env_example | 2 + .../@uppy/companion/src/standalone/index.js | 44 +++++++++++-------- website/src/docs/companion.md | 4 ++ 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/packages/@uppy/companion/env.test.sh b/packages/@uppy/companion/env.test.sh index 808d276f1d..38811bc020 100644 --- a/packages/@uppy/companion/env.test.sh +++ b/packages/@uppy/companion/env.test.sh @@ -2,6 +2,8 @@ export NODE_ENV="test" export COMPANION_PORT=3020 export COMPANION_DOMAIN="localhost:3020" export COMPANION_SELF_ENDPOINT="localhost:3020" +export COMPANION_HIDE_METRICS="false" +export COMPANION_HIDE_WELCOME="false" export COMPANION_PROTOCOL="http" export COMPANION_DATADIR="./test/output" diff --git a/packages/@uppy/companion/env_example b/packages/@uppy/companion/env_example index 0642884ea1..eea5f5151b 100644 --- a/packages/@uppy/companion/env_example +++ b/packages/@uppy/companion/env_example @@ -2,6 +2,8 @@ NODE_ENV=dev COMPANION_PORT=3020 COMPANION_DOMAIN=uppy.xxxx.com COMPANION_SELF_ENDPOINT=uppy.xxxx.com +COMPANION_HIDE_METRICS=false +COMPANION_HIDE_WELCOME=false COMPANION_PROTOCOL=https COMPANION_DATADIR=/mnt/uppy-server-data diff --git a/packages/@uppy/companion/src/standalone/index.js b/packages/@uppy/companion/src/standalone/index.js index 68224dff49..c437170acc 100644 --- a/packages/@uppy/companion/src/standalone/index.js +++ b/packages/@uppy/companion/src/standalone/index.js @@ -25,19 +25,21 @@ function server (moreCompanionOptions = {}) { const app = express() // for server metrics tracking. - const metricsMiddleware = promBundle({ includeMethod: true }) - const promClient = metricsMiddleware.promClient - const collectDefaultMetrics = promClient.collectDefaultMetrics - const promInterval = collectDefaultMetrics({ register: promClient.register, timeout: 5000 }) - - // Add version as a prometheus gauge - const versionGauge = new promClient.Gauge({ name: 'companion_version', help: 'npm version as an integer' }) - // @ts-ignore - const numberVersion = version.replace(/\D/g, '') * 1 - versionGauge.set(numberVersion) - - if (app.get('env') !== 'test') { - clearInterval(promInterval) + if (process.env.COMPANION_HIDE_METRICS !== 'true') { + const metricsMiddleware = promBundle({ includeMethod: true }) + const promClient = metricsMiddleware.promClient + const collectDefaultMetrics = promClient.collectDefaultMetrics + const promInterval = collectDefaultMetrics({ register: promClient.register, timeout: 5000 }) + + // Add version as a prometheus gauge + const versionGauge = new promClient.Gauge({ name: 'companion_version', help: 'npm version as an integer' }) + // @ts-ignore + const numberVersion = version.replace(/\D/g, '') * 1 + versionGauge.set(numberVersion) + + if (app.get('env') !== 'test') { + clearInterval(promInterval) + } } // Query string keys whose values should not end up in logging output. @@ -56,7 +58,7 @@ function server (moreCompanionOptions = {}) { * censored: boolean * }} */ - function censorQuery (rawQuery) { + function censorQuery(rawQuery) { /** @type {{ [key: string]: string }} */ const query = {} let censored = false @@ -94,7 +96,9 @@ function server (moreCompanionOptions = {}) { }) // make app metrics available at '/metrics'. - app.use(metricsMiddleware) + if (process.env.COMPANION_HIDE_METRICS !== 'true') { + app.use(metricsMiddleware) + } app.use(bodyParser.json()) app.use(bodyParser.urlencoded({ extended: false })) @@ -163,10 +167,12 @@ function server (moreCompanionOptions = {}) { }) // Routes - app.get('/', (req, res) => { - res.setHeader('Content-Type', 'text/plain') - res.send(helper.buildHelpfulStartupMessage(companionOptions)) - }) + if (process.env.COMPANION_HIDE_WELCOME !== 'true') { + app.get('/', (req, res) => { + res.setHeader('Content-Type', 'text/plain') + res.send(helper.buildHelpfulStartupMessage(companionOptions)) + }) + } let companionApp try { diff --git a/website/src/docs/companion.md b/website/src/docs/companion.md index c8877eacbc..f67de2b9c1 100644 --- a/website/src/docs/companion.md +++ b/website/src/docs/companion.md @@ -150,6 +150,10 @@ export COMPANION_PROTOCOL="YOUR SERVER PROTOCOL" export COMPANION_PORT="YOUR SERVER PORT" # corresponds to the server.port option, defaults to '' export COMPANION_PATH="/SERVER/PATH/TO/WHERE/COMPANION/LIVES" +# disables the welcome page, defaults to false +export COMPANION_HIDE_WELCOME="true" +# disables the metrics page, defaults to false +export COMPANION_HIDE_METRICS="true" # use this in place of COMPANION_PATH if the server path should not be # handled by the express.js app, but maybe by an external server configuration From c0587147eeba08a1cf93d22a11b26ffacfc4e731 Mon Sep 17 00:00:00 2001 From: Shlomo Zalman Heigh Date: Thu, 10 Sep 2020 15:39:24 -0400 Subject: [PATCH 2/3] fix scoping error --- packages/@uppy/companion/src/standalone/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/@uppy/companion/src/standalone/index.js b/packages/@uppy/companion/src/standalone/index.js index c437170acc..40a22263b6 100644 --- a/packages/@uppy/companion/src/standalone/index.js +++ b/packages/@uppy/companion/src/standalone/index.js @@ -21,12 +21,13 @@ const { version } = require('../../package.json') * * @returns {object} */ -function server (moreCompanionOptions = {}) { +function server(moreCompanionOptions = {}) { const app = express() // for server metrics tracking. + let metricsMiddleware; if (process.env.COMPANION_HIDE_METRICS !== 'true') { - const metricsMiddleware = promBundle({ includeMethod: true }) + metricsMiddleware = promBundle({ includeMethod: true }) const promClient = metricsMiddleware.promClient const collectDefaultMetrics = promClient.collectDefaultMetrics const promInterval = collectDefaultMetrics({ register: promClient.register, timeout: 5000 }) From b4d2a587d1d8b14216d13e22ec7f1d830991866b Mon Sep 17 00:00:00 2001 From: Shlomo Zalman Heigh Date: Thu, 10 Sep 2020 16:38:28 -0400 Subject: [PATCH 3/3] fix lint errors --- packages/@uppy/companion/src/standalone/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/@uppy/companion/src/standalone/index.js b/packages/@uppy/companion/src/standalone/index.js index 40a22263b6..b1eecd23a1 100644 --- a/packages/@uppy/companion/src/standalone/index.js +++ b/packages/@uppy/companion/src/standalone/index.js @@ -21,11 +21,11 @@ const { version } = require('../../package.json') * * @returns {object} */ -function server(moreCompanionOptions = {}) { +function server (moreCompanionOptions = {}) { const app = express() // for server metrics tracking. - let metricsMiddleware; + let metricsMiddleware if (process.env.COMPANION_HIDE_METRICS !== 'true') { metricsMiddleware = promBundle({ includeMethod: true }) const promClient = metricsMiddleware.promClient @@ -59,7 +59,7 @@ function server(moreCompanionOptions = {}) { * censored: boolean * }} */ - function censorQuery(rawQuery) { + function censorQuery (rawQuery) { /** @type {{ [key: string]: string }} */ const query = {} let censored = false