+ // Rift framework imports
+ var constants = require('./framework/core/api_utils/constants');
+ var skyquakeEmitter = require('./framework/core/modules/skyquakeEmitter');
+ var auth_routes = require('./framework/core/modules/routes/auth');
+ var navigation_routes = require('./framework/core/modules/routes/navigation');
+ var socket_routes = require('./framework/core/modules/routes/sockets');
+ var restconf_routes = require('./framework/core/modules/routes/restconf');
+ var inactivity_routes = require('./framework/core/modules/routes/inactivity');
+ var descriptor_routes = require('./framework/core/modules/routes/descriptorModel');
+ var configuration_routes = require('./framework/core/modules/routes/configuration');
+ var configurationAPI = require('./framework/core/modules/api/configuration');
+ var userManagement_routes = require('./framework/core/modules/routes/userManagement');
+ var projectManagement_routes = require('./framework/core/modules/routes/projectManagement');
+ var session_routes = require('./framework/core/modules/routes/sessions');
+ var schemaAPI = require('./framework/core/modules/api/schemaAPI');
+ var modelAPI = require('./framework/core/modules/api/modelAPI');
+ var appConfigAPI = require('./framework/core/modules/api/appConfigAPI');
+
+ schemaAPI.init();
+ modelAPI.init();
+ appConfigAPI.init();
+ /**
+ * Processing when a plugin is added or modified
+ * @param {string} plugin_name - Name of the plugin
+ */
+ function onPluginAdded(plugin_name) {
+ // Load plugin config
+ var plugin_config = reload('./plugins/' + plugin_name + '/config.json');
+
+ // Load all app's views
+ app.use('/' + plugin_name, express.static('./plugins/' + plugin_name + '/' + plugin_config.root));
+
+ // Load all app's routes
+ app.use('/' + plugin_name, require('./plugins/' + plugin_name + '/routes'));
+
+ // Publish navigation links
+ if (plugin_config.routes && _.isArray(plugin_config.routes)) {
+ skyquakeEmitter.emit('config_discoverer.navigation_discovered', plugin_name, plugin_config);
+ }
+
+ }
+
+ /**
+ * Serve jquery
+ */
+ app.use('/jquery', express.static('./node_modules/jquery/dist/jquery.min.js'));
+ /**
+ * Serve images
+ */
+ app.use('/img', express.static('./framework/style/img'));
+
+ /**
+ * Start listening on a port
+ * @param {string} port - Port to listen on
+ * @param {object} httpServer - httpServer created with http(s).createServer
+ */
+ function startListening(port, httpServer) {
+ var server = httpServer.listen(port, function () {
+ var host = server.address().address;
+
+ var port = server.address().port;
+
+ console.log('Express server listening on port', port);
+ });
+ return server;
+ }
+
+ /**
+ * Initialize skyquake
+ */
+ function init() {
+ skyquakeEmitter.on('plugin_discoverer.plugin_discovered', onPluginAdded);
+ skyquakeEmitter.on('plugin_discoverer.plugin_updated', onPluginAdded);
+ }
+
+ /**
+ * Configure skyquake
+ */
+ function config() {
+ // Conigure any globals
+ process.env.NODE_TLS_REJECT_UNAUTHORIZED=0;
+
+ // Configure auth manager
+ authManager.configure(authConfig);
+
+ // Configure auth router
+ auth_routes.routes(authManager);
+ app.use(auth_routes.router);
+
+ //Configure session route(s)
+ session_routes.routes(sessionsConfig);
+ app.use(session_routes.router);
+
+ // Configure navigation router
+ app.use(navigation_routes);
+
+ // Configure restconf router
+ app.use(restconf_routes);
+
+ //Configure inactivity route(s)
+ app.use(inactivity_routes);
+
+ // Configure global config with ssl enabled/disabled
+ var globalConfig = {
+ ssl_enabled: httpsConfigured,
+ api_server: apiServer,
+ api_server_protocol: apiServerProtocol,
+ api_server_port_number: constants.LAUNCHPAD_PORT,
+ idp_server_address: launchpadAddress,
+ idp_server_protocol: idpServerProtocol,
+ idp_server_port_number: idpServerPortNumber,
+ csrf_target: csrfTarget
+ };
+
+ if (uploadServer) {
+ globalConfig.upload_server = uploadServer;
+ }
+ if (devDownloadServer) {
+ globalConfig.dev_download_server = devDownloadServer;
+ }
+
+ configurationAPI.globalConfiguration.update(globalConfig);
+
+ // Configure configuration route(s)
+ app.use(configuration_routes);
+
+ // Configure schema api
+ app.use(schemaAPI.getRouter());
+
+ // Configure model api
+ app.use(modelAPI.getRouter());
+
+ // Configure config api
+ app.use(appConfigAPI.getRouter());
+
+ //Configure descriptor route(s)
+ app.use(descriptor_routes);
+
+ //Configure user management route(s)
+ app.use(userManagement_routes);
+
+ //Configure project management route(s)
+ app.use(projectManagement_routes);
+
+ // app.get('/testme', function(req, res) {
+ // res.sendFile(__dirname + '/index.html');
+ // });
+
+ // Configure HTTP/HTTPS server and populate socketConfig.
+ if (httpsConfigured) {
+ console.log('HTTPS configured. Will create 2 servers');
+ secureHttpServer = https.createServer(sslOptions, app);
+ // Add redirection on SERVER_PORT
+ httpServer = http.createServer(function(req, res) {
+ var host = req.headers['host'];
+ host = host.replace(/:\d+$/, ":" + constants.SECURE_SERVER_PORT);
+
+ res.writeHead(301, { "Location": "https://" + host + req.url });
+ res.end();
+ });
+
+ socketConfig.httpServer = secureHttpServer;
+ } else {
+ httpServer = http.createServer(app);
+ socketConfig.httpServer = httpServer;
+ }
+
+ // Configure socket manager
+ socketManager.configure(socketConfig);
+
+ // Configure socket router
+ socket_routes.routes(socketManager);
+ app.use(socket_routes.router);
+
+ // Serve multiplex-client
+ app.get('/multiplex-client', function(req, res) {
+ res.sendFile(__dirname + '/node_modules/websocket-multiplex/multiplex_client.js');
+ });
+
+ // handle requests for gzip'd files
+ app.get('*gzip*', function (req, res, next) {
+ res.set('Content-Encoding', 'gzip');
+ next();
+ });
+
+ }
+
+ /**
+ * Run skyquake functionality
+ */
+ function run() {
+
+ // Start plugin_discoverer
+ var navigation_manager = require('./framework/core/modules/navigation_manager');
+ var plugin_discoverer = require('./framework/core/modules/plugin_discoverer');
+
+ // Initialize asynchronous modules
+ navigation_manager.init();
+ plugin_discoverer.init();
+
+ // Configure asynchronous modules
+ navigation_manager.config()
+ plugin_discoverer.config({
+ plugins_path: './plugins'
+ });
+
+ // Run asynchronous modules
+ navigation_manager.run();
+ plugin_discoverer.run();
+
+
+ // Server start
+ if (httpsConfigured) {
+ console.log('HTTPS configured. Will start 2 servers');
+ // Start listening on SECURE_SERVER_PORT (8443)
+ var secureServer = startListening(constants.SECURE_SERVER_PORT, secureHttpServer);
+ }
+ // Start listening on SERVER_PORT (8000)
+ var server = startListening(constants.SERVER_PORT, httpServer);
+
+ }