<commit_before>'use strict';

/**
 * Module dependencies
 */

// Public node modules.
const _ = require('lodash');

// Local utilities.
const regex = require('../../../../util/regex');

/**
 * Router hook
 */

module.exports = function (strapi) {
  const hook = {

    /**
     * Default options
     */

    defaults: {
      prefix: '',
      routes: {},
      graphql: {
        enabled: true,
        route: '/graphql'
      }
    },

    /**
     * Initialize the hook
     */

    initialize: function (cb) {
      let route;
      let controller;
      let action;
      let policies = [];

      // Initialize the router.
      if (!strapi.router) {
        strapi.router = strapi.middlewares.router({
          prefix: strapi.config.prefix
        });
      }

      // Middleware used for every routes.
      // Expose the endpoint in `this`.
      function globalPolicy(endpoint, route) {
        return function * (next) {
          this.request.route = {
            endpoint: endpoint,
            controller: route.controller,
            firstWord: _.startsWith(route.endpoint, '/') ? route.endpoint.split('/')[1] : route.endpoint.split('/')[0],
            value: route
          };
          yield next;
        };
      }

      // Parse each route from the user config, load policies if any
      // and match the controller and action to the desired endpoint.
      _.forEach(strapi.config.routes, function (value, endpoint) {
        try {
          route = regex.detectRoute(endpoint);
          controller = strapi.controllers[value.controller.toLowerCase()];
          action = controller[value.action];
          policies = [];

          // Add the `globalPolicy`.
          policies.push(globalPolicy(endpoint, route));

          if (_.isArray(value.policies) && !_.isEmpty(value.policies)) {
            _.forEach(value.policies, function (policy) {
              if (strapi.policies[policy]) {
                policies.push(strapi.policies[policy]);
              } else {
                strapi.log.error('Ignored attempt to bind route `' + endpoint + '` with unknown policy `' + policy + '`.');
                process.exit(1);
              }
            });
          }
          strapi.router[route.verb.toLowerCase()](route.endpoint, strapi.middlewares.compose(policies), action);
        } catch (err) {
          strapi.log.warn('Ignored attempt to bind route `' + endpoint + '` to unknown controller/action.');
        }
      });
      
      if (strapi.config.graphql.enabled === true) {
        // Define GraphQL route with modified Waterline models to GraphQL schema
        strapi.router.get(strapi.config.graphql.route, strapi.middlewares.graphql({
          schema: strapi.schemas,
          pretty: true
        }));
      }

      // Let the router use our routes and allowed methods.
      strapi.app.use(strapi.router.routes());
      strapi.app.use(strapi.router.allowedMethods());

      // Handle router errors.
      strapi.app.use(function * (next) {
        try {
          yield next;
          const status = this.status || 404;
          if (status === 404) {
            this.throw(404);
          }
        } catch (err) {
          err.status = err.status || 500;
          err.message = err.expose ? err.message : 'Houston, we have a problem.';

          this.status = err.status;
          this.body = {
            code: err.status,
            message: err.message
          };

          this.app.emit('error', err, this);
        }
      });

      cb();
    },

    /**
     * Reload the hook
     */

    reload: function () {
      hook.initialize(function (err) {
        if (err) {
          strapi.log.error('Failed to reinitialize the router.');
          strapi.stop();
        } else {
          strapi.emit('hook:router:reloaded');
        }
      });
    }
  };

  return hook;
};
<commit_msg>Set GraphQL global variable to undefined if GraphQL is disabled<commit_after>'use strict';

/**
 * Module dependencies
 */

// Public node modules.
const _ = require('lodash');

// Local utilities.
const regex = require('../../../../util/regex');

/**
 * Router hook
 */

module.exports = function (strapi) {
  const hook = {

    /**
     * Default options
     */

    defaults: {
      prefix: '',
      routes: {},
      graphql: {
        enabled: true,
        route: '/graphql'
      }
    },

    /**
     * Initialize the hook
     */

    initialize: function (cb) {
      let route;
      let controller;
      let action;
      let policies = [];

      // Initialize the router.
      if (!strapi.router) {
        strapi.router = strapi.middlewares.router({
          prefix: strapi.config.prefix
        });
      }

      // Middleware used for every routes.
      // Expose the endpoint in `this`.
      function globalPolicy(endpoint, route) {
        return function * (next) {
          this.request.route = {
            endpoint: endpoint,
            controller: route.controller,
            firstWord: _.startsWith(route.endpoint, '/') ? route.endpoint.split('/')[1] : route.endpoint.split('/')[0],
            value: route
          };
          yield next;
        };
      }

      // Parse each route from the user config, load policies if any
      // and match the controller and action to the desired endpoint.
      _.forEach(strapi.config.routes, function (value, endpoint) {
        try {
          route = regex.detectRoute(endpoint);
          controller = strapi.controllers[value.controller.toLowerCase()];
          action = controller[value.action];
          policies = [];

          // Add the `globalPolicy`.
          policies.push(globalPolicy(endpoint, route));

          if (_.isArray(value.policies) && !_.isEmpty(value.policies)) {
            _.forEach(value.policies, function (policy) {
              if (strapi.policies[policy]) {
                policies.push(strapi.policies[policy]);
              } else {
                strapi.log.error('Ignored attempt to bind route `' + endpoint + '` with unknown policy `' + policy + '`.');
                process.exit(1);
              }
            });
          }
          strapi.router[route.verb.toLowerCase()](route.endpoint, strapi.middlewares.compose(policies), action);
        } catch (err) {
          strapi.log.warn('Ignored attempt to bind route `' + endpoint + '` to unknown controller/action.');
        }
      });
      
      // Define GraphQL route with modified Waterline models to GraphQL schema
      // or disable the global variable
      if (strapi.config.graphql.enabled === true) {
        strapi.router.get(strapi.config.graphql.route, strapi.middlewares.graphql({
          schema: strapi.schemas,
          pretty: true
        }));
      } else {
        global.graphql = undefined;
      }

      // Let the router use our routes and allowed methods.
      strapi.app.use(strapi.router.routes());
      strapi.app.use(strapi.router.allowedMethods());

      // Handle router errors.
      strapi.app.use(function * (next) {
        try {
          yield next;
          const status = this.status || 404;
          if (status === 404) {
            this.throw(404);
          }
        } catch (err) {
          err.status = err.status || 500;
          err.message = err.expose ? err.message : 'Houston, we have a problem.';

          this.status = err.status;
          this.body = {
            code: err.status,
            message: err.message
          };

          this.app.emit('error', err, this);
        }
      });

      cb();
    },

    /**
     * Reload the hook
     */

    reload: function () {
      hook.initialize(function (err) {
        if (err) {
          strapi.log.error('Failed to reinitialize the router.');
          strapi.stop();
        } else {
          strapi.emit('hook:router:reloaded');
        }
      });
    }
  };

  return hook;
};

{'fraction_non_alphanumeric': 0.13325725878027134, 'fraction_numerical': 0.003803727653100038, 'mean_word_length': 1.7060034305317324, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6946146', 'n_tokens_mistral': 2218, 'n_tokens_neox': 2193, 'n_words': 507}