<commit_before>var Hoek = require('hoek'),
    Boom = require('boom'),
    Joi = require('joi'),
    Path = require('path');

// Declare internals

var internals = {
    defaults: {
        protocol: null, // If not specified, uses the same protocol as server info
        endpoint: '/docs',
        auth: false,
        basePath: '',
        pathPrefixSize: 1,
        payloadType: 'json',
        produces: ['application/json'],
        ui: true,
        listing: true,
        index: false
    }
};


exports.register = function(plugin, options, next) {

    var settings = Hoek.applyToDefaults(internals.defaults, options || {});


    // add routing for documentation API endpoint ie /docs
    plugin.route({
        method: 'GET',
        path: settings.endpoint,
        config: internals.docs(settings)
    });

    if (settings.ui) {

        plugin.views({
            engines: {
                html: {
                    module: require('handlebars')
                }
            },
            path: __dirname + '/../public/swaggerui'
        });

        plugin.route({
            method: 'GET',
            path: settings.endpoint + '/swaggerui/index.html',
            handler: {
                view: {
                    template: 'index.html',
                    context: {
                        settings: settings
                    }
                }
            }
        });

        // add routing for swaggerui library directory ie /docs/swaggerui
        plugin.route({
            method: 'GET',
            path: settings.endpoint + '/swaggerui/{path*}',
            handler: {
                directory: {
                    path: __dirname + '/../public/swaggerui/',
                    listing: settings.listing,
                    index: settings.index
                }
            }
        });


    }
    next();
};


internals.docs = function(settings) {

    return {
        auth: settings.auth,
        validate: {
            query: {
                path: Joi.string(),
                api_key: Joi.string()
            }
        },
        handler: function(request, reply) {

            var requestSettings = Hoek.clone(settings);

            // prepend full protocol, hostname and port onto endpoints for shred
            var protocol = requestSettings.protocol || request.server.info.protocol || 'http';
            var hostname = protocol + '://' + request.headers.host;
            if (!requestSettings.endpoint.match(/^https?:\/\//)) {
                requestSettings.endpoint = hostname + settings.endpoint;
            }
            if (!requestSettings.basePath.match(/^https?:\/\//)) {
                requestSettings.basePath = hostname + settings.basePath;
            }

            var routes = (request.server.routingTable) ? request.server.routingTable() : request.server.table(),
                resourceName = request.query.path;


            // if we have a resource name filter routes
            if (resourceName) {
                // remove routes that do not belong to the resource
                routes = routes.filter(function(item) {
                    if (item.method !== 'options' && internals.isResourceRoute(item.path, resourceName)) {
                        return true;
                    }
                    return false;
                });<commit_msg>Use the basePath from the settings in the endpoint<commit_after>var Hoek = require('hoek'),
    Boom = require('boom'),
    Joi = require('joi'),
    Path = require('path');

// Declare internals

var internals = {
    defaults: {
        protocol: null, // If not specified, uses the same protocol as server info
        endpoint: '/docs',
        auth: false,
        basePath: '',
        pathPrefixSize: 1,
        payloadType: 'json',
        produces: ['application/json'],
        ui: true,
        listing: true,
        index: false
    }
};


exports.register = function(plugin, options, next) {

    var settings = Hoek.applyToDefaults(internals.defaults, options || {});


    // add routing for documentation API endpoint ie /docs
    plugin.route({
        method: 'GET',
        path: settings.endpoint,
        config: internals.docs(settings)
    });

    if (settings.ui) {

        plugin.views({
            engines: {
                html: {
                    module: require('handlebars')
                }
            },
            path: __dirname + '/../public/swaggerui'
        });

        plugin.route({
            method: 'GET',
            path: settings.endpoint + '/swaggerui/index.html',
            handler: {
                view: {
                    template: 'index.html',
                    context: {
                        settings: settings
                    }
                }
            }
        });

        // add routing for swaggerui library directory ie /docs/swaggerui
        plugin.route({
            method: 'GET',
            path: settings.endpoint + '/swaggerui/{path*}',
            handler: {
                directory: {
                    path: __dirname + '/../public/swaggerui/',
                    listing: settings.listing,
                    index: settings.index
                }
            }
        });


    }
    next();
};


internals.docs = function(settings) {

    return {
        auth: settings.auth,
        validate: {
            query: {
                path: Joi.string(),
                api_key: Joi.string()
            }
        },
        handler: function(request, reply) {

            var requestSettings = Hoek.clone(settings);

            // prepend full protocol, hostname and port onto endpoints for shred
            var protocol = requestSettings.protocol || request.server.info.protocol || 'http';
            var hostname = protocol + '://' + request.headers.host;
            if (!requestSettings.basePath.match(/^https?:\/\//)) {
                requestSettings.basePath = hostname + settings.basePath;
            }
            if (!requestSettings.endpoint.match(/^https?:\/\//)) {
                requestSettings.endpoint = requestSettings.basePath + settings.endpoint;
            }

            var routes = (request.server.routingTable) ? request.server.routingTable() : request.server.table(),
                resourceName = request.query.path;


            // if we have a resource name filter routes
            if (resourceName) {
                // remove routes that do not belong to the resource
                routes = routes.filter(function(item) {
                    if (item.method !== 'options' && internals.isResourceRoute(item.path, resourceName)) {
                        return true;
                    }
                    return false;
                });
{'fraction_non_alphanumeric': 0.11505997334517992, 'fraction_numerical': 0.0002961646675551607, 'mean_word_length': 1.4069850320741268, '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': '1296031', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1634, 'n_words': 400}