Quellcode durchsuchen

companion: add option to hide welcome page and metrics (#2521)

* companion: add option to hide welcome and metrics

* fix scoping error

* fix lint errors
Shlomo Zalman Heigh vor 4 Jahren
Ursprung
Commit
3856afa238

+ 2 - 0
packages/@uppy/companion/env.test.sh

@@ -2,6 +2,8 @@ export NODE_ENV="test"
 export COMPANION_PORT=3020
 export COMPANION_PORT=3020
 export COMPANION_DOMAIN="localhost:3020"
 export COMPANION_DOMAIN="localhost:3020"
 export COMPANION_SELF_ENDPOINT="localhost:3020"
 export COMPANION_SELF_ENDPOINT="localhost:3020"
+export COMPANION_HIDE_METRICS="false"
+export COMPANION_HIDE_WELCOME="false"
 
 
 export COMPANION_PROTOCOL="http"
 export COMPANION_PROTOCOL="http"
 export COMPANION_DATADIR="./test/output"
 export COMPANION_DATADIR="./test/output"

+ 2 - 0
packages/@uppy/companion/env_example

@@ -2,6 +2,8 @@ NODE_ENV=dev
 COMPANION_PORT=3020
 COMPANION_PORT=3020
 COMPANION_DOMAIN=uppy.xxxx.com
 COMPANION_DOMAIN=uppy.xxxx.com
 COMPANION_SELF_ENDPOINT=uppy.xxxx.com
 COMPANION_SELF_ENDPOINT=uppy.xxxx.com
+COMPANION_HIDE_METRICS=false
+COMPANION_HIDE_WELCOME=false
 
 
 COMPANION_PROTOCOL=https
 COMPANION_PROTOCOL=https
 COMPANION_DATADIR=/mnt/uppy-server-data
 COMPANION_DATADIR=/mnt/uppy-server-data

+ 25 - 18
packages/@uppy/companion/src/standalone/index.js

@@ -25,19 +25,22 @@ function server (moreCompanionOptions = {}) {
   const app = express()
   const app = express()
 
 
   // for server metrics tracking.
   // 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)
+  let metricsMiddleware
+  if (process.env.COMPANION_HIDE_METRICS !== 'true') {
+    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.
   // Query string keys whose values should not end up in logging output.
@@ -94,7 +97,9 @@ function server (moreCompanionOptions = {}) {
   })
   })
 
 
   // make app metrics available at '/metrics'.
   // 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.json())
   app.use(bodyParser.urlencoded({ extended: false }))
   app.use(bodyParser.urlencoded({ extended: false }))
@@ -163,10 +168,12 @@ function server (moreCompanionOptions = {}) {
   })
   })
 
 
   // Routes
   // 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
   let companionApp
   try {
   try {

+ 4 - 0
website/src/docs/companion.md

@@ -150,6 +150,10 @@ export COMPANION_PROTOCOL="YOUR SERVER PROTOCOL"
 export COMPANION_PORT="YOUR SERVER PORT"
 export COMPANION_PORT="YOUR SERVER PORT"
 # corresponds to the server.port option, defaults to ''
 # corresponds to the server.port option, defaults to ''
 export COMPANION_PATH="/SERVER/PATH/TO/WHERE/COMPANION/LIVES"
 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
 # 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
 # handled by the express.js app, but maybe by an external server configuration