UNPKG

@aarconada/urserver

Version:

Basic Server definitions to develope REST API with a node + express Server

569 lines (500 loc) 46.9 kB
/** * Created by ubuntu on 14/08/18. */ 'use strict'; const bodyparser = require('body-parser'); const _ = require('lodash'); const cors = require('./cors'); const express = require('express'); const fileupload = require('express-fileupload'); const utils = require('./utils'); class server { constructor(serverConfiguration) { this.configuration = serverConfiguration || {}; this.configuration.serverName = serverConfiguration.serverName || ''; this.configuration.description = serverConfiguration.description || ''; this.configuration.https = serverConfiguration.https || {}; this.configuration.https.enabled = serverConfiguration.https.enabled || false; this.configuration.https.folder = serverConfiguration.https.folder || '/cert'; this.configuration.https.keyFilename = serverConfiguration.https.keyFilename || null; this.configuration.https.certFilename = serverConfiguration.https.certFilename || null; this.configuration.https.caFilename = serverConfiguration.https.caFilename || null; this.configuration.header = serverConfiguration.header || {}; this.configuration.header.set_allow_origin = serverConfiguration.header.set_allow_origin || false; this.configuration.header.allow_origin = serverConfiguration.header.allow_origin || '*'; this.configuration.header.allow_credentials = serverConfiguration.header.allow_credentials || 'true'; this.configuration.header.allow_headers = serverConfiguration.header.allow_headers || 'X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method'; this.configuration.header.allow_methods = serverConfiguration.header.allow_methods || 'GET, POST, OPTIONS, PUT, DELETE'; this.configuration.protocol = this.configuration.https.enabled === true ? 'https' : 'http'; this.configuration.port = serverConfiguration.port || 3000; this.configuration.appFolder = serverConfiguration.appFolder || __dirname; this.configuration.resourcesFolder = serverConfiguration.resourcesFolder || null; this.configuration.host = serverConfiguration.host || 'localhost'; this.configuration.baseUrl = serverConfiguration.baseUrl || this.configuration.protocol + '://' + this.configuration.host + (this.configuration.port.toString().indexOf('pipe') !== -1 ? '' : ':' + this.configuration.port); this.configuration.apiPreffixPath = serverConfiguration.apiPreffixPath || '/api'; this.configuration.apiUrl = this.configuration.baseUrl + this.configuration.apiPreffixPath; this.configuration.help = serverConfiguration.help || {}; this.configuration.help.enabled = serverConfiguration.help.enabled || true; this.configuration.help.route = serverConfiguration.help.route || '/help'; this.configuration.ping = serverConfiguration.ping || {}; this.configuration.ping.enabled = serverConfiguration.ping.enabled || true; this.configuration.ping.route = serverConfiguration.ping.route || '/ping'; this.configuration.token = serverConfiguration.token || {}; this.configuration.token.secret = serverConfiguration.token.secret || 'TOKENSECRET'; this.configuration.token.header = serverConfiguration.token.header || 'authorization'; this.configuration.token.expiration = serverConfiguration.token.expiration || {}; this.configuration.token.expiration.bearer = serverConfiguration.token.expiration.bearer || '30days'; this.configuration.token.expiration.validation = serverConfiguration.token.expiration.validation || '7days'; this.configuration.token.expiration.recover = serverConfiguration.token.expiration.recover || '7days'; this.configuration.redis = serverConfiguration.redis || {}; this.configuration.redis.allowConfig = serverConfiguration.allowConfig || true; this.configuration.redis.connection = serverConfiguration.redis.connection || {}; this.configuration.redis.connection.host = serverConfiguration.redis.connection.host || '127.0.0.1'; this.configuration.redis.connection.port = serverConfiguration.redis.connection.port || 6379; this.configuration.redis.connection.options = serverConfiguration.redis.connection.options || {}; this.configuration.redis.expiration = serverConfiguration.redis.expiration || 2 * 30 * 24 * 60 * 60; //*** THIS VALUE REPRESENTS THE NUMBER OF SECONDS THAT THE KEYS WILL LIVE IN REDIS (MUST BE GREATER THAN EXPIRATION_TOKEN this.configuration.email = serverConfiguration.email || {}; this.configuration.email.templatePath = serverConfiguration.email.templatePath || 'templates'; this.configuration.email.sender = serverConfiguration.email.sender || ''; this.configuration.email.configuration = serverConfiguration.email.configuration || {}; this.configuration.email.configuration.service = serverConfiguration.email.configuration.service || ''; this.configuration.email.configuration.auth = serverConfiguration.email.configuration.auth || {}; this.configuration.email.configuration.auth.user = serverConfiguration.email.configuration.auth.user || ''; this.configuration.email.configuration.auth.pass = serverConfiguration.email.configuration.auth.pass || ''; this.configuration.google = serverConfiguration.google || {}; this.configuration.google.storage = serverConfiguration.google.storage || {}; this.configuration.google.storage.enabled = serverConfiguration.google.storage.enabled || false; this.configuration.google.storage.configuration = serverConfiguration.google.storage.configuration || {}; this.configuration.google.storage.configuration.keyFilename = serverConfiguration.google.storage.configuration.keyFilename || ''; this.configuration.google.storage.configuration.projectId = serverConfiguration.google.storage.configuration.projectId || ''; this.configuration.google.storage.storageBucket = serverConfiguration.google.storage.storageBucket || ''; this.configuration.sequelize = serverConfiguration.sequelize || {}; this.configuration.sequelize.enabled = serverConfiguration.sequelize.enabled || false; this.configuration.sequelize.mustSync = serverConfiguration.sequelize.mustSync || false; this.configuration.sequelize.database = serverConfiguration.sequelize.database || {}; this.configuration.sequelize.database.name = serverConfiguration.sequelize.database.name || ''; this.configuration.sequelize.database.login = serverConfiguration.sequelize.database.login || ''; this.configuration.sequelize.database.password = serverConfiguration.sequelize.database.password || ''; this.configuration.sequelize.connection = serverConfiguration.sequelize.connection || {}; this.configuration.sequelize.connection.host = serverConfiguration.sequelize.connection.host || 'localhost'; this.configuration.sequelize.connection.dialect = serverConfiguration.sequelize.connection.dialect || 'mysql'; this.configuration.sequelize.sync = serverConfiguration.sequelize.sync || {}; this.configuration.sequelize.sync.alter = serverConfiguration.sequelize.sync.alter || true; this.configuration.authentication = serverConfiguration.authentication || {}; this.configuration.authentication.basic = serverConfiguration.authentication.basic || {}; this.configuration.authentication.basic.enabled = serverConfiguration.authentication.basic.enabled || false; this.configuration.authentication.basic.logintries = serverConfiguration.authentication.basic.logintries || 3; this.configuration.authentication.basic.validation = serverConfiguration.authentication.basic.validation || {}; this.configuration.authentication.basic.validation.enabled = serverConfiguration.authentication.basic.validation.enabled || true; this.configuration.authentication.basic.validation.template = serverConfiguration.authentication.basic.validation.template || null; this.configuration.authentication.basic.validation.OKtemplate = serverConfiguration.authentication.basic.validation.OKtemplate || null; this.configuration.authentication.basic.validation.KOtemplate = serverConfiguration.authentication.basic.validation.KOtemplate || null; this.configuration.authentication.basic.refresh = serverConfiguration.authentication.basic.refresh || {}; this.configuration.authentication.basic.refresh.template = serverConfiguration.authentication.basic.refresh.template || null; this.configuration.authentication.basic.refresh.OKtemplate = serverConfiguration.authentication.basic.refresh.OKtemplate || null; this.configuration.authentication.basic.refresh.KOtemplate = serverConfiguration.authentication.basic.refresh.KOtemplate || null; this.configuration.authentication.basic.change = serverConfiguration.authentication.basic.change || {}; this.configuration.authentication.basic.change.template = serverConfiguration.authentication.basic.change.template || null; this.configuration.authentication.basic.change.OKtemplate = serverConfiguration.authentication.basic.change.OKtemplate || null; this.configuration.authentication.basic.change.KOtemplate = serverConfiguration.authentication.basic.change.KOtemplate || null; this.configuration.authentication.anonymous = serverConfiguration.authentication.anonymous || {}; this.configuration.authentication.anonymous.enabled = serverConfiguration.authentication.anonymous.enabled || false; this.configuration.authentication.ldap = serverConfiguration.authentication.ldap || {}; this.configuration.authentication.ldap.enabled = serverConfiguration.authentication.ldap.enabled || false; this.configuration.authentication.ldap.serverUrl = serverConfiguration.authentication.ldap.serverUrl || null; this.configuration.authentication.ldap.readerDN = serverConfiguration.authentication.ldap.readerDN || ''; this.configuration.authentication.ldap.readerPass = serverConfiguration.authentication.ldap.readerPass || ''; this.configuration.authentication.activedirectory = serverConfiguration.authentication.activedirectory || {}; this.configuration.authentication.activedirectory.enabled = serverConfiguration.authentication.activedirectory.enabled || false; this.configuration.authentication.activedirectory.serverUrl = serverConfiguration.authentication.activedirectory.serverUrl || null; this.configuration.authentication.activedirectory.baseDN = serverConfiguration.authentication.activedirectory.baseDN || ''; this.configuration.socket = serverConfiguration.socket || {}; this.configuration.socket.enabled = serverConfiguration.socket.enabled || true; this.configuration.socket.header = serverConfiguration.socket.header || this.configuration.token.header; this.configuration.socket.port = serverConfiguration.socket.port || (this.configuration.port + 1); this.configuration.socket.origins = serverConfiguration.socket.origins || '*'; this.configuration.notification = serverConfiguration.notification || {}; this.configuration.notification.enabled = serverConfiguration.notification.enabled || false; this.configuration.notification.inotify = serverConfiguration.notification.inotify || {}; this.configuration.notification.inotify.enabled = serverConfiguration.notification.inotify.enabled || false; this.configuration.notification.inotify.protocol = serverConfiguration.notification.inotify.protocol || 'http:'; this.configuration.notification.inotify.host = serverConfiguration.notification.inotify.host || null; this.configuration.notification.inotify.port = serverConfiguration.notification.inotify.port || 8080; this.configuration.notification.inotify.pathSendToTokens = serverConfiguration.notification.inotify.pathSendToTokens || '/iecisa-push-web/notification/sendToTokens'; this.configuration.notification.inotify.apiToken = serverConfiguration.notification.inotify.apiToken || null; this.configuration.notification.inotify.apiSecret = serverConfiguration.notification.inotify.apiSecret || null; this.configuration.notification.inotify.cryptoAlgorithm = serverConfiguration.notification.inotify.cryptoAlgorithm || 'sha256'; this.configuration.notification.inotify.idApp = serverConfiguration.notification.inotify.idApp || null; this.configuration.notification.inotify.dateFormat = serverConfiguration.notification.inotify.dateFormat || 'YYYYMMDDTHHmmss[Z]'; this.configuration.notification.inotify.icon = serverConfiguration.notification.inotify.icon || 'ic_notification'; this.configuration.notification.inotify.sound = serverConfiguration.notification.inotify.sound || 'default'; this.configuration.notification.inotify.color = serverConfiguration.notification.inotify.color || 'color_notification'; this.configuration.notification.custom = serverConfiguration.notification.custom || {}; this.configuration.notification.custom.enabled = serverConfiguration.notification.custom.enabled || false; this.configuration.notification.custom.findNotificationCallback = serverConfiguration.notification.custom.findNotificationCallback || null; this.configuration.notification.custom.definitions = serverConfiguration.notification.custom.definitions || null; this.configuration.notification.model = serverConfiguration.notification.model || {}; this.configuration.notification.model.enabled = serverConfiguration.notification.model.enabled || false; this.configuration.notification.model.definitions = serverConfiguration.notification.model.definitions || null; this.configuration.debugger = serverConfiguration.debugger || {}; this.configuration.debugger.enabled = serverConfiguration.debugger.enabled || true; this.configuration.logger = serverConfiguration.logger || {}; this.configuration.logger.enabled = serverConfiguration.logger.enabled || true; this.configuration.logger.path = serverConfiguration.logger.path || 'log'; this.configuration.logger.logDebug = serverConfiguration.logger.logDebug || true; this.configuration.logger.maxLogFiles = serverConfiguration.logger.maxLogFiles || 5; this.configuration.logger.endpoints = serverConfiguration.logger.endpoints || {}; this.configuration.logger.endpoints.enabled = serverConfiguration.logger.endpoints.enabled || true; this.configuration.logger.endpoints.allowedRoles = serverConfiguration.logger.endpoints.allowedRoles || []; this.configuration.logger.endpoints.route = serverConfiguration.logger.endpoints.route || '/log'; this.configuration.generators = serverConfiguration.generators || {}; this.configuration.generators.path = serverConfiguration.generators.path || 'generators'; this.configuration.generators.primeng = serverConfiguration.generators.primeng || {}; this.configuration.generators.primeng.enabled = serverConfiguration.generators.primeng.enabled || false; this.configuration.generators.primeng.url = serverConfiguration.generators.primeng.url || '/generator/primeng'; this.configuration.generators.primeng.originpath = serverConfiguration.generators.primeng.originpath || 'primeng'; this.configuration.generators.primeng.resultpath = serverConfiguration.generators.primeng.resultpath || 'primeng'; this.configuration.generators.primeng.extension = serverConfiguration.generators.primeng.extension || '.template'; } addDependencies() { this.app = null; this.fileSystem = require('./filesystem'); this.logger = require('./logger'); this.debugger = require('./debug'); this.debug('Initializing Server...'); this.debug('Debugger intilialized with instance name ', this.configuration.serverName); this.endpointmanager = new (require('./endpointmanager'))(); this.server = null; this.utils = utils; this.session = require('./session'); this.token = require('./token'); this.defaultResponses = require('./response'); this.email = require('./email'); this.googlestorage = require('./googlestorage'); this.sequelize = require('./sequelize'); this.roles = require('./role'); this.authentication = require('./authentication'); this.i18n = require('./i18n'); this.socket = require('./socket'); this.generators = require('./generators'); this.notification = require('./notification'); this.workflow = require('./workflow'); this.imapMonitor = require('./imap'); } debug() { if(this.debugger) { if(this.configuration.logger.logDebug && this.debugger.logger !== null) { this.debugger.instance.enable(this.configuration.serverName + ":log"); this.debugger.logger.apply(this, arguments); this.debugger.instance.enable(this.configuration.serverName); } if(this.debugger.debugger !== null && this.configuration.debugger.enabled) this.debugger.debugger.apply(this, arguments); } } handleServerHelp(serverInstance) { return function(req, res, next) { serverInstance.debug('Generating documentation to server ' + serverInstance.configuration.serverName); utils.sendHtml(res, serverInstance.documentation()); }; }; addHelpEndpoint() { if(this.configuration.help.enabled) { this.debug('Help enabled at', this.configuration.apiPreffixPath + this.configuration.help.route); const router = express.Router(); const helpRoute = router.route(this.configuration.help.route); helpRoute.get(this.handleServerHelp(this)); this.app.use(this.configuration.apiPreffixPath, router); } } handleServerPing(serverInstance) { return function(req, res, next) { serverInstance.debug('Ping requested to server'); utils.sendOk(res, 'pong'); }; }; addPingEndpoint() { if(this.configuration.ping.enabled) { this.debug('Ping enabled at', this.configuration.apiPreffixPath + this.configuration.ping.route); const router = express.Router(); const pingRoute = router.route(this.configuration.ping.route); pingRoute.get(this.handleServerPing(this)); this.app.use(this.configuration.apiPreffixPath, router); } } initialize() { this.debug('Initializing Server...'); this.debug('Creating dependencies...'); this.app = express(); this.debug('Initializing Dependencies...'); this.addHelpEndpoint(); this.addPingEndpoint(); this.logger.initialize(); this.debug('Adding basic middleware...'); this.app.use(bodyparser.urlencoded({extended: true})); this.app.use(bodyparser.json()); if (this.configuration.resourcesFolder !== null) { this.app.use(express.static(this.configuration.resourcesFolder)); } this.app.use(fileupload()); const that = this; this.app.use(function(req, res, next) { if(res) { if(that.configuration.header.set_allow_origin) { res.header('Access-Control-Allow-Origin', that.configuration.header.allow_origin); } res.header('Access-Control-Allow-Credentials', that.configuration.header.allow_credentials); res.header('Access-Control-Allow-Headers', that.configuration.token.header + ', ' + that.configuration.header.allow_headers); res.header('Access-Control-Allow-Methods', that.configuration.header.allow_methods); res.header('Allow', that.configuration.header.allow_methods); } if(next) next(); }); this.app.use(this.i18n); this.debug('Initializing plugins...'); this.socket.initialize(); this.notification.initialize(); this.debug('Server initialized...'); } start() { this.initialize(); this.debug('Starting server...'); try { this.debug('Trying to create server', this.configuration.serverName); if(this.configuration.sequelize.enabled) { this.sequelize.generateEndpoints(); } this.notification.attachNotificationsToModel(); this.debug('Adding routes...'); this.endpointmanager.endpoints.forEach(currentEndpoint => { this.app.use(this.configuration.apiPreffixPath, currentEndpoint.router()); }); this.app.use(this.notFound); this.debug('Creating server...'); var that = this; if (this.configuration.https.enabled) { const serverOptions = {}; if(!_.isNull(this.configuration.https.keyFilename)) serverOptions.key = this.fileSystem.readFile(this.configuration.https.folder, this.configuration.https.keyFilename); if(!_.isNull(this.configuration.https.certFilename)) serverOptions.cert = this.fileSystem.readFile(this.configuration.https.folder, this.configuration.https.certFilename); if(!_.isNull(this.configuration.https.caFilename)) serverOptions.ca = this.fileSystem.readFile(this.configuration.https.folder, this.configuration.https.caFilename); this.server = require('https').createServer(serverOptions, this.app); } else this.server = require('http').createServer(this.app); this.server.listen(this.configuration.port, '0.0.0.0', function () { that.debug('%s Server listening on port %s', that.configuration.https.enabled ? 'HTTPS' : 'HTTP', that.configuration.port); }); } catch (err) { this.debug(err); } }; documentation() { var serverTable = '<table style="font-family: Arial, Helvetica, sans-serif;border: 1px solid #AAAAAA; width: 100%; text-align: left; border-collapse: collapse;">'; serverTable += '<tr>'; serverTable += '<td colspan="2" style="font-size: larger; font-weight: bold;background-color: #A4A4A4; text-align:center;border: 1px solid #AAAAAA;padding: 10px;">' + this.configuration.serverName + '</td>'; serverTable += '</tr>'; serverTable += '<tr>'; serverTable += '<td style="font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Description</td>'; serverTable += '<td style="width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + this.configuration.description + '</td>'; serverTable += '</tr>'; serverTable += '<tr>'; serverTable += '<td style="font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Uses HTTPS</td>'; serverTable += '<td style="width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + (this.configuration.https.enabled ? 'YES' : 'NO') + '</td>'; serverTable += '</tr>'; serverTable += '<tr>'; serverTable += '<td style="font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Host</td>'; serverTable += '<td style="width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + this.configuration.host + '</td>'; serverTable += '</tr>'; serverTable += '<tr>'; serverTable += '<td style="font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Port</td>'; serverTable += '<td style="width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + (this.configuration.port.toString().indexOf('pipe') !== -1 ? 'IIS Managed' : this.configuration.port) + '</td>'; serverTable += '</tr>'; serverTable += '<tr>'; serverTable += '<td style="font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Relative URL</td>'; serverTable += '<td style="width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + this.configuration.apiPreffixPath + '</td>'; serverTable += '</tr>'; serverTable += '<tr>'; serverTable += '<td style="font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Base URL</td>'; serverTable += '<td style="width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + this.configuration.baseUrl + '</td>'; serverTable += '</tr>'; serverTable += '<tr>'; serverTable += '<td style="font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Full URL</td>'; serverTable += '<td style="width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + this.configuration.baseUrl + this.configuration.apiPreffixPath + '</td>'; serverTable += '</tr>'; serverTable += '<tr>'; serverTable += '<td style="font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Server path</td>'; serverTable += '<td style="width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + this.configuration.appFolder + '</td>'; serverTable += '</tr>'; serverTable += '<tr>'; serverTable += '<td style="font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Resources folder name</td>'; serverTable += '<td style="width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + this.configuration.resourcesFolder + '</td>'; serverTable += '</tr>'; serverTable += '<tr>'; serverTable += '<td style="font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Socket enabled</td>'; serverTable += '<td style="width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + this.configuration.socket.enabled + '</td>'; serverTable += '</tr>'; serverTable += '<tr>'; serverTable += '<td style="font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Socket port</td>'; serverTable += '<td style="width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + this.configuration.socket.port + '</td>'; serverTable += '</tr>'; if(Object.keys(this.roles.definedRoles).length > 0) { serverTable += '<tr>'; serverTable += '<td colspan="2" style="font-weight: bold;background-color: #A4A4A4; text-align:center;border: 1px solid #BBBBBB;padding: 10px;">ROLES</td>'; serverTable += '</tr>'; Object.keys(this.roles.definedRoles).sort((a, b)=> (a > b) ? 1: 0).forEach(currentRoleName => { serverTable += '<tr>'; serverTable += '<td colspan="5" style="font-weight: bold;background-color: #A4A4A4; text-align:center;border: 1px solid #BBBBBB;padding: 10px;">'; serverTable += '<table style="font-family: Arial, Helvetica, sans-serif;border: 1px solid #AAAAAA; width: 100%; text-align: left; border-collapse: collapse;">'; serverTable += '<tr>'; serverTable += '<td colspan="2" style="font-weight: bold;background-color: #BABABA; text-align:center;border: 1px solid #AAAAAA;padding: 10px;">' + currentRoleName + '</td>'; serverTable += '</tr>' var currentRole = this.roles.definedRoles[currentRoleName]; serverTable += '<tr>'; serverTable += '<td style="font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Id</td>'; serverTable += '<td style="width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + currentRole.id + '</td>'; serverTable += '</tr>'; serverTable += '<tr>'; serverTable += '<td style="font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Name</td>'; serverTable += '<td style="width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + currentRole.name + '</td>'; serverTable += '</tr>'; serverTable += '<tr>'; serverTable += '<td style="font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Authentication methods</td>'; this.debug('Anonymous', (currentRole.authentication & this.authentication.types.Anonymous) === this.authentication.types.Anonymous); serverTable += '<td style="width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + ((currentRole.authentication & this.authentication.types.Anonymous) === this.authentication.types.Anonymous ? 'Anonymous ' : '') + ((currentRole.authentication & this.authentication.types.Basic) === this.authentication.types.Basic ? 'Basic ' : '') + ((currentRole.authentication & this.authentication.types.Facebook) === this.authentication.types.Facebook ? 'Facebook ' : '') + ((currentRole.authentication & this.authentication.types.Google) === this.authentication.types.Google ? 'Google ' : '') + ((currentRole.authentication & this.authentication.types.LDAP) === this.authentication.types.LDAP ? 'LDAP ' : '') + ((currentRole.authentication & this.authentication.types.ActiveDirectory) === this.authentication.types.ActiveDirectory ? 'Active Directory ' : '') + '</td>'; serverTable += '</tr>'; serverTable += '</table>'; serverTable += '</td>'; serverTable += '</tr>'; }); } serverTable += '<tr>'; serverTable += '<td colspan="2" style="font-weight: bold;background-color: #A4A4A4; text-align:center;border: 1px solid #BBBBBB;padding: 10px;">GENERATORS</td>'; serverTable += '</tr>'; if(this.configuration.generators.primeng.enabled) { serverTable += '<tr>'; serverTable += '<td style="font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Prime-NG</td>'; serverTable += '<td style="width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;"><a href="' + this.configuration.baseUrl + this.configuration.apiPreffixPath + this.configuration.generators.primeng.url + '">Generate</a></td>'; serverTable += '</tr>'; } serverTable += '<tr>'; serverTable += '<td colspan="2" style="font-weight: bold;background-color: #A4A4A4; text-align:center;border: 1px solid #BBBBBB;padding: 10px;"><div onclick="var x = document.getElementsByClassName(\'notification\');var i;for(i=0; i < x.length;i++){x[i].style.display = x[i].style.display == \'none\' ? \'\': \'none\';}" style="cursor:pointer;">NOTIFICATIONS</div></td>'; serverTable += '</tr>'; serverTable += '<tr class="notification" style="display:none;">'; serverTable += '<td class="notification" style="display:none;font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Enabled</td>'; serverTable += '<td class="notification" style="display:none;width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + (this.configuration.notification.enabled ? 'YES' : 'NO') + '</td>'; serverTable += '</tr>'; if(this.configuration.notification.enabled) { serverTable += '<tr class="notification" style="display:none;">'; serverTable += '<td class="notification" style="display:none;font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Uses iNotify</td>'; serverTable += '<td class="notification" style="display:none;width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + (this.configuration.notification.inotify.enabled ? 'YES' : 'NO') + '</td>'; serverTable += '</tr>'; if (this.configuration.notification.custom.enabled) { serverTable += '<tr class="notification" style="display:none;">'; serverTable += '<td class="notification" colspan="5" style="display:none;font-weight: bold;background-color: #A4A4A4; text-align:center;border: 1px solid #BBBBBB;padding: 10px;">'; serverTable += '<table class="notification" style="display:none;font-family: Arial, Helvetica, sans-serif;border: 1px solid #AAAAAA; width: 100%; text-align: left; border-collapse: collapse;">'; serverTable += '<tr class="notification" style="display:none;">'; serverTable += '<td class="notification" colspan="2" style="display:none;font-weight: bold;background-color: #A4A4A4; text-align:center;border: 1px solid #BBBBBB;padding: 10px;">CUSTOM NOTIFICATIONS</td>'; serverTable += '</tr>'; this.configuration.notification.custom.definitions.sort((a, b) => (a.name > b.name) ? 1 : 0).forEach(currentDefinition => { serverTable += '<tr class="notification" style="display:none;">'; serverTable += '<td class="notification" colspan="2" style="display:none;font-weight: bold;background-color: #BABABA; text-align:center;border: 1px solid #AAAAAA;padding: 10px;">' + currentDefinition.name + '</td>'; serverTable += '</tr>'; serverTable += '<tr class="notification" style="display:none;">'; serverTable += '<td class="notification" style="display:none;font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Description</td>'; serverTable += '<td class="notification" style="display:none;width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + currentDefinition.description + '</td>'; serverTable += '</tr>'; serverTable += '<tr class="notification" style="display:none;">'; serverTable += '<td class="notification" style="display:none;font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Type name</td>'; serverTable += '<td class="notification" style="display:none;width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + currentDefinition.type + '</td>'; serverTable += '</tr>'; serverTable += '<tr class="notification" style="display:none;">'; serverTable += '<td class="notification" style="display:none;font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Send via email</td>'; serverTable += '<td class="notification" style="display:none;width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + (currentDefinition.email === true ? 'YES' : 'NO') + '</td>'; serverTable += '</tr>'; serverTable += '<tr class="notification" style="display:none;">'; serverTable += '<td class="notification" style="display:none;font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Send via iNotify</td>'; serverTable += '<td class="notification" style="display:none;width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + (currentDefinition.iNotify === true ? 'YES' : 'NO') + '</td>'; serverTable += '</tr>'; }); serverTable += '</table>'; serverTable += '</td>'; serverTable += '</tr>'; } if (this.configuration.notification.model.enabled) { serverTable += '<tr class="notification" style="display:none;">'; serverTable += '<td class="notification" colspan="5" style="display:none;font-weight: bold;background-color: #A4A4A4; text-align:center;border: 1px solid #BBBBBB;padding: 10px;">'; serverTable += '<table class="notification" style="display:none;font-family: Arial, Helvetica, sans-serif;border: 1px solid #AAAAAA; width: 100%; text-align: left; border-collapse: collapse;">'; serverTable += '<tr class="notification" style="display:none;">'; serverTable += '<td class="notification" colspan="2" style="display:none;font-weight: bold;background-color: #A4A4A4; text-align:center;border: 1px solid #BBBBBB;padding: 10px;">MODEL BASED NOTIFICATIONS</td>'; serverTable += '</tr>'; this.configuration.notification.model.definitions.sort((a, b) => (a.name > b.name) ? 1 : 0).forEach(currentDefinition => { serverTable += '<tr class="notification" style="display:none;">'; serverTable += '<td class="notification" colspan="2" style="display:none;font-weight: bold;background-color: #BABABA; text-align:center;border: 1px solid #AAAAAA;padding: 10px;">' + currentDefinition.name + '</td>'; serverTable += '</tr>'; serverTable += '<tr class="notification" style="display:none;">'; serverTable += '<td class="notification" style="display:none;font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Description</td>'; serverTable += '<td class="notification" style="display:none;width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + currentDefinition.description + '</td>'; serverTable += '</tr>'; serverTable += '<tr class="notification" style="display:none;">'; serverTable += '<td class="notification" style="display:none;font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Model name</td>'; serverTable += '<td class="notification" style="display:none;width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + currentDefinition.modelName + '</td>'; serverTable += '</tr>'; serverTable += '<tr class="notification" style="display:none;">'; serverTable += '<td class="notification" style="display:none;font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Send on</td>'; serverTable += '<td class="notification" style="display:none;width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + currentDefinition.sendOn + '</td>'; serverTable += '</tr>'; serverTable += '<tr class="notification" style="display:none;">'; serverTable += '<td class="notification" style="display:none;font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Send via email</td>'; serverTable += '<td class="notification" style="display:none;width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + (currentDefinition.email === true ? 'YES' : 'NO') + '</td>'; serverTable += '</tr>'; serverTable += '<tr class="notification" style="display:none;">'; serverTable += '<td class="notification" style="display:none;font-weight: bold;background-color: #BABABA; width: 25%;border: 1px solid #AAAAAA;padding: 10px;">Send via iNotify</td>'; serverTable += '<td class="notification" style="display:none;width: auto;border: 1px solid #AAAAAA;padding: 10px;background: #FFFFFF;">' + (currentDefinition.iNotify === true ? 'YES' : 'NO') + '</td>'; serverTable += '</tr>'; }); serverTable += '</table>'; serverTable += '</td>'; serverTable += '</tr>'; } } serverTable += '<tr>'; serverTable += '<td colspan="2" style="font-weight: bold;background-color: #A4A4A4; text-align:center;border: 1px solid #BBBBBB;padding: 10px;"><div onclick="var x = document.getElementsByClassName(\'endpoint\');var i;for(i=0; i < x.length;i++){x[i].style.display = x[i].style.display == \'none\' ? \'\': \'none\';}" style="cursor:pointer;">ENDPOINTS</div></td>'; serverTable += '</tr>'; var endpointCounter = 0; this.endpointmanager.endpoints.sort((a, b) => (a.configuration.name > b.configuration.name) ? 1 : -1).forEach(currentEndpoint => { var endpointScript = serverTable += '<tr class="endpoint" style="display:none;">'; serverTable += '<td class="endpoint" colspan="2" style="display:none;font-weight: bold;text-align: left;background-color: #999999; width: 10%;border: 1px solid #AAAAAA;padding: 10px;"><div onclick="document.getElementById(\'endpointDiv' + endpointCounter + '\').style.display = document.getElementById(\'endpointDiv' + endpointCounter +'\').style.display == \'none\' ? \'\': \'none\';" style="cursor:pointer;">' + currentEndpoint.configuration.name + ' (' + currentEndpoint.configuration.route + ')</div></td>'; serverTable += '</tr>'; serverTable += '<tr class="endpoint" style="display:none;">'; serverTable += '<td class="endpoint" colspan="2" style="display:none;text-align: center;background-color: #BABABA; width: 10%;border: 1px solid #BBBBBB;"><div id="endpointDiv' + endpointCounter + '" style="display:none;" style="padding-top:20px;">' + currentEndpoint.documentation() + '</div></td>'; serverTable += '</tr>'; endpointCounter ++; }); serverTable += '</table>'; return serverTable; } notFound(req, res, next) { utils.sendHTTPErrorMessage(res, 404, "Not Found"); }; }; var serverInstance; var initializing = false; module.exports = function(value) { while (initializing); if(!serverInstance && value) { initializing = true; serverInstance = new server(value); initializing = false; serverInstance.addDependencies(); if(value) serverInstance.debug('Server instance created with configuration values'); return serverInstance; } else { return serverInstance; } };