Skip to content
Snippets Groups Projects
Select Git revision
  • b41d82e0fbf2726c2cf2585747e2bae9deba1522
  • main default protected
  • 1.2.2
  • 1.2.1
  • 1.2.0
  • 1.1.0
  • 1.0.0
7 results

index.js

Blame
  • index.js 1.90 KiB
    // metaRoutes.js
    
    function metaRoutes(app, { version, config, isServiceReady, metricsEndpoint }, routeOptions = {}) {
      // Default options with all routes enabled
      const defaults = {
        enableVersion: true,
        enableConfig: true,
        enableHealth: true,
        enableReady: true,
        enableMetrics: true,
      };
    
      // Merge user provided options with defaults
      const options = { ...defaults, ...routeOptions };
    
      if (options.enableVersion) {
        app.get('/_version', function (req, res) {
          res.send(version);
        });
      }
    
      if (options.enableConfig) {
        app.get('/_config', function (req, res) {
          res.send(config._show());
        });
      }
    
      if (options.enableHealth) {
        app.get('/_health', function (req, res) {
          res.send('OK');
        });
      }
    
      if (options.enableReady) {
        app.get('/_ready', function (req, res) {
          if (isServiceReady()) {
            res.send('OK');
          } else {
            res.status(503).send('Not ready');
          }
        });
      }
    
      if (options.enableMetrics) {
        app.get('/_metrics', metricsEndpoint);
      }
    }
    
    module.exports = metaRoutes;
    /**
       * GET /_version
       * @summary Returns the version of the service
       * @tags meta
       * @return {string} 200 - The version of the service
       */
      /**
       * GET /_config
       * @summary Returns the configuration of the service with redacted secrets
       * @tags meta
       * @return {object} 200 - The configuration of the service
       */
      /**
       * GET /_health
       * @summary Returns the health of the service
       * @description This endpoint is used to check if the service is healthy and should always return 200
       * @tags meta
       * @return {string} 200 - OK
       */
      /**
       * GET /_ready
       * @summary Returns the readiness of the service
       * @description This endpoint is used to check if the service is ready and should return 200 if the service is ready to serve requests
       * @tags meta
       * @return {string} 200 - OK
       * @return {string} 503 - Service Unavailable
       */