<commit_before>'use strict';

var path = require('path');
var os = require('os');

var coreModules = require ("soajs.core.modules");
var core = coreModules.core;
var provision = coreModules.provision;
var lib = require ("soajs.core.libs");

var express = require("./../classes/express");

var autoRegHost = process.env.SOAJS_SRV_AUTOREGISTERHOST || true;
if (autoRegHost && typeof(autoRegHost) !== 'boolean') {
    autoRegHost = (autoRegHost === 'true');
}

/**
 *
 * @param param {}
 */
function service(param) {
    var _self = this;

    //NOTE: added this to trigger a warning if someone is still using old style configuration
    if (!param)
        param = {"oldStyleConfiguration": false};
    if (param && param.config && param.config.serviceName) {
        param.oldStyleConfiguration = true;
        for (var configParam in param.config) {
            if (Object.hasOwnProperty.call(param.config, configParam)) {
                param[configParam] = param.config[configParam];
            }
        }
        delete param.config;
    }
    if (process.env.SOAJS_SOLO && process.env.SOAJS_SOLO === "true") {
        param.extKeyRequired = false;
        //param.roaming = false;
        param.session = false;
        param.oauth = false;
        param.awareness = false;
        param.awarenessEnv = false;
    }
    if (param.extKeyRequired === true) {
        param.multitenant = true;
        param.security = true;
        param.acl = true;
    }
    else {
        param.multitenant = false;
        param.security = false;
        param.acl = false;
    }

    var defaultParam = ["bodyParser", "methodOverride", "cookieParser", "logger", "inputmask", "awareness"];
    var len = defaultParam.length;
    for (var i = 0; i < len; i++) {
        if (!Object.hasOwnProperty.call(param, defaultParam[i])) {
            param[defaultParam[i]] = true;
        }
    }

    var soajs = {};
    soajs.param = param;
    _self.app = express();
    _self.appMaintenance = express();

    _self.app.soajs = soajs;
}

function extractAPIsList(schema) {
    var excluded = ['commonFields'];
    var METHOD = ['get', 'post', 'put', 'delete'];
    var apiList = [];

    var processRoute = function (routeObj, routeName, method) {
        var oneApi = {
            'l': routeObj._apiInfo.l,
            'v': routeName,
            'm': method
        };

        if (routeObj._apiInfo.group) {
            oneApi.group = routeObj._apiInfo.group;
        }

        if (routeObj._apiInfo.groupMain) {
            oneApi.groupMain = routeObj._apiInfo.groupMain;
        }
        return (oneApi);
    };
    var processRoutes = function (routes, method) {
        for (var route in routes) {
            if (Object.hasOwnProperty.call(routes, route)) {
                if (excluded.indexOf(route) !== -1) {
                    continue;
                }

                if (METHOD.indexOf(route) !== -1) {
                    processRoutes(routes[route], route);
                }
                else {
                    var oneApi = processRoute(routes[route], route, method);
                    apiList.push(oneApi);
                }
            }
        }
    };
    processRoutes(schema, "");
    return apiList;
}

service.prototype.init = function (callback) {
    var _self = this;
    var registry = null;
    var soajs = _self.app.soajs;

    soajs.param.serviceName = soajs.param.serviceName.toLowerCase();
    soajs.param.serviceGroup = soajs.param.serviceGroup || "No Group Service";
    soajs.param.serviceVersion = soajs.param.serviceVersion || 1;
    soajs.param.serviceVersion = parseInt(soajs.param.serviceVersion);
    if (isNaN(soajs.param.serviceVersion)) {
        throw new Error('Service version must be integer: [' + soajs.param.serviceVersion + ']');
    }
    soajs.param.servicePort = soajs.param.servicePort || null;
    soajs.param.extKeyRequired = soajs.param.extKeyRequired || false;
    soajs.param.requestTimeout = soajs.param.requestTimeout || null;
    soajs.param.requestTimeoutRenewal = soajs.param.requestTimeoutRenewal || null;
    soajs.param.awarenessEnv = soajs.param.awarenessEnv || false;
    soajs.param.serviceIp = process.env.SOAJS_SRVIP || null;
    soajs.param.serviceHATask = null;
    soajs.param.swagger = soajs.param.swagger || false;

    var fetchedHostIp = null;
    var serviceIpNotDetected = false;
    if (!autoRegHost && !process.env.SOAJS_DEPLOY_HA) {
        soajs.param.serviceIp = '127.0.0.1';
    }
    if (!soajs.param.serviceIp && !process.env.SOAJS_DEPLOY_HA) {
        core.getHostIp(function (getHostIpResponse) {
            fetchedHostIp = getHostIpResponse;
            if (fetchedHostIp && fetchedHostIp.result) {
                soajs.param.serviceIp = fetchedHostIp.ip;
                if (fetchedHostIp.extra && fetchedHostIp.extra.swarmTask) {
                    soajs.param.serviceHATask = fetchedHostIp.extra.swarmTask;
                }
            } else {
                serviceIpNotDetected = true;
                soajs.param.serviceIp = "127.0.0.1";
            }
            resume();
        });
    }
    else {
        resume();
    }

    function resume () {
        soajs.apiList = extractAPIsList(soajs.param.schema);

        core.registry.load({
            "serviceName": soajs.param.serviceName,
            "serviceGroup": soajs.param.serviceGroup,
            "serviceVersion": soajs.param.serviceVersion,
            "designatedPort": soajs.param.servicePort,
            "extKeyRequired": soajs.param.extKeyRequired,
            "requestTimeout": soajs.param.requestTimeout,
            "requestTimeoutRenewal": soajs.param.requestTimeoutRenewal,
            "awareness": soajs.param.awareness,
            "serviceIp": soajs.param.serviceIp,
	        "swagger": soajs.param.swagger,
            "apiList": soajs.apiList
        }, function (reg) {
            registry = reg;

            soajs.serviceConf = lib.registry.getServiceConf(soajs.param.serviceName, registry);

            _self.log = core.getLogger(soajs.param.serviceName, registry.serviceConfig.logger);

            if (process.env.SOAJS_SOLO && process.env.SOAJS_SOLO === "true")
                _self.log.info("SOAJS is in SOLO mode, the following got turned OFF [extKeyRequired, session, oauth, awareness, awarenessEnv].");

            if (soajs.param.oldStyleConfiguration)
                _self.log.warn("Old style configuration detected. Please start using the new way of passing param when creating a new service.");<commit_msg>added swagger route support<commit_after>'use strict';

var path = require('path');
var os = require('os');
var fs = require('fs');

var coreModules = require ("soajs.core.modules");
var core = coreModules.core;
var provision = coreModules.provision;
var lib = require ("soajs.core.libs");

var express = require("./../classes/express");

var autoRegHost = process.env.SOAJS_SRV_AUTOREGISTERHOST || true;
if (autoRegHost && typeof(autoRegHost) !== 'boolean') {
    autoRegHost = (autoRegHost === 'true');
}

/**
 *
 * @param param {}
 */
function service(param) {
    var _self = this;

    //NOTE: added this to trigger a warning if someone is still using old style configuration
    if (!param)
        param = {"oldStyleConfiguration": false};
    if (param && param.config && param.config.serviceName) {
        param.oldStyleConfiguration = true;
        for (var configParam in param.config) {
            if (Object.hasOwnProperty.call(param.config, configParam)) {
                param[configParam] = param.config[configParam];
            }
        }
        delete param.config;
    }
    if (process.env.SOAJS_SOLO && process.env.SOAJS_SOLO === "true") {
        param.extKeyRequired = false;
        //param.roaming = false;
        param.session = false;
        param.oauth = false;
        param.awareness = false;
        param.awarenessEnv = false;
    }
    if (param.extKeyRequired === true) {
        param.multitenant = true;
        param.security = true;
        param.acl = true;
    }
    else {
        param.multitenant = false;
        param.security = false;
        param.acl = false;
    }

    var defaultParam = ["bodyParser", "methodOverride", "cookieParser", "logger", "inputmask", "awareness"];
    var len = defaultParam.length;
    for (var i = 0; i < len; i++) {
        if (!Object.hasOwnProperty.call(param, defaultParam[i])) {
            param[defaultParam[i]] = true;
        }
    }

    var soajs = {};
    soajs.param = param;
    _self.app = express();
    _self.appMaintenance = express();

    _self.app.soajs = soajs;
}

function extractAPIsList(schema) {
    var excluded = ['commonFields'];
    var METHOD = ['get', 'post', 'put', 'delete'];
    var apiList = [];

    var processRoute = function (routeObj, routeName, method) {
        var oneApi = {
            'l': routeObj._apiInfo.l,
            'v': routeName,
            'm': method
        };

        if (routeObj._apiInfo.group) {
            oneApi.group = routeObj._apiInfo.group;
        }

        if (routeObj._apiInfo.groupMain) {
            oneApi.groupMain = routeObj._apiInfo.groupMain;
        }
        return (oneApi);
    };
    var processRoutes = function (routes, method) {
        for (var route in routes) {
            if (Object.hasOwnProperty.call(routes, route)) {
                if (excluded.indexOf(route) !== -1) {
                    continue;
                }

                if (METHOD.indexOf(route) !== -1) {
                    processRoutes(routes[route], route);
                }
                else {
                    var oneApi = processRoute(routes[route], route, method);
                    apiList.push(oneApi);
                }
            }
        }
    };
    processRoutes(schema, "");
    return apiList;
}

service.prototype.init = function (callback) {
    var _self = this;
    var registry = null;
    var soajs = _self.app.soajs;

    soajs.param.serviceName = soajs.param.serviceName.toLowerCase();
    soajs.param.serviceGroup = soajs.param.serviceGroup || "No Group Service";
    soajs.param.serviceVersion = soajs.param.serviceVersion || 1;
    soajs.param.serviceVersion = parseInt(soajs.param.serviceVersion);
    if (isNaN(soajs.param.serviceVersion)) {
        throw new Error('Service version must be integer: [' + soajs.param.serviceVersion + ']');
    }
    soajs.param.servicePort = soajs.param.servicePort || null;
    soajs.param.extKeyRequired = soajs.param.extKeyRequired || false;
    soajs.param.requestTimeout = soajs.param.requestTimeout || null;
    soajs.param.requestTimeoutRenewal = soajs.param.requestTimeoutRenewal || null;
    soajs.param.awarenessEnv = soajs.param.awarenessEnv || false;
    soajs.param.serviceIp = process.env.SOAJS_SRVIP || null;
    soajs.param.serviceHATask = null;
    soajs.param.swagger = soajs.param.swagger || false;

    var fetchedHostIp = null;
    var serviceIpNotDetected = false;
    if (!autoRegHost && !process.env.SOAJS_DEPLOY_HA) {
        soajs.param.serviceIp = '127.0.0.1';
    }
    if (!soajs.param.serviceIp && !process.env.SOAJS_DEPLOY_HA) {
        core.getHostIp(function (getHostIpResponse) {
            fetchedHostIp = getHostIpResponse;
            if (fetchedHostIp && fetchedHostIp.result) {
                soajs.param.serviceIp = fetchedHostIp.ip;
                if (fetchedHostIp.extra && fetchedHostIp.extra.swarmTask) {
                    soajs.param.serviceHATask = fetchedHostIp.extra.swarmTask;
                }
            } else {
                serviceIpNotDetected = true;
                soajs.param.serviceIp = "127.0.0.1";
            }
            resume();
        });
    }
    else {
        resume();
    }

    function resume () {
        soajs.apiList = extractAPIsList(soajs.param.schema);

        core.registry.load({
            "serviceName": soajs.param.serviceName,
            "serviceGroup": soajs.param.serviceGroup,
            "serviceVersion": soajs.param.serviceVersion,
            "designatedPort": soajs.param.servicePort,
            "extKeyRequired": soajs.param.extKeyRequired,
            "requestTimeout": soajs.param.requestTimeout,
            "requestTimeoutRenewal": soajs.param.requestTimeoutRenewal,
            "awareness": soajs.param.awareness,
            "serviceIp": soajs.param.serviceIp,
	        "swagger": soajs.param.swagger,
            "apiList": soajs.apiList
        }, function (reg) {
            registry = reg;

            soajs.serviceConf = lib.registry.getServiceConf(soajs.param.serviceName, registry);

            _self.log = core.getLogger(soajs.param.serviceName, registry.serviceConfig.logger);

            //turn on swagger path
            if (fs.existsSync('./swagger.yml')) {
                _self.app.use('/swagger.yml', express.static('./swagger.yml'));
                _self.log.info("Swagger route [/swagger.yml] is ON.");
            }

            if (process.env.SOAJS_SOLO && process.env.SOAJS_SOLO === "true")
                _self.log.info("SOAJS is in SOLO mode, the following got turned OFF [extKeyRequired, session, oauth, awareness, awarenessEnv].");

            if (soajs.param.oldStyleConfiguration)
                _self.log.warn("Old style configuration detected. Please start using the new way of passing param when creating a new service.");
{'fraction_non_alphanumeric': 0.124296329655483, 'fraction_numerical': 0.002401861442618029, 'mean_word_length': 2.2505489143693582, 'pattern_counts': {'":': 24, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '188391', 'n_tokens_mistral': 4125, 'n_tokens_neox': 4037, 'n_words': 786}