1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
3 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
4 # you may not use this file except in compliance with the License. #
\r
5 # You may obtain a copy of the License at #
\r
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
9 # Unless required by applicable law or agreed to in writing, software #
\r
10 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
12 # See the License for the specific language governing permissions and #
\r
13 # limitations under the License. #
\r
14 ##############################################################################*/
\r
18 const path = require('path');
\r
19 const https = require('https');
\r
20 const http = require('http');
\r
21 const fs = require('fs');
\r
23 // Express.js modules
\r
24 const express = require('@feathersjs/express');
\r
25 const compress = require('compression');
\r
26 const helmet = require('helmet');
\r
27 const cors = require('cors');
\r
28 const favicon = require('serve-favicon');
\r
30 // Feathers.js modules
\r
31 const feathers = require('@feathersjs/feathers');
\r
32 const configuration = require('@feathersjs/configuration');
\r
33 const socketio = require('@feathersjs/socketio'); //require('@feathersjs/socketio-client');
\r
34 const io = require('socket.io'); //socket.io-client
\r
35 const socket = io();
\r
37 const services = require('./services');
\r
38 const appHooks = require('./app.hooks');
\r
39 const channels = require('./channels');
\r
40 const authentication = require('./authentication');
\r
43 const mongoose = require('../lib/mongoose');
\r
44 const _mongoose = require('mongoose');
\r
47 const { accessibleRecordsPlugin, accessibleFieldsPlugin } = require('@casl/mongoose');
\r
48 _mongoose.plugin(accessibleFieldsPlugin);
\r
49 _mongoose.plugin(accessibleRecordsPlugin);
\r
52 const logger = require('../lib/logger');
\r
55 const redis = require('redis');
\r
57 // Create a Express/Feathers application
\r
58 const app = express(feathers());
\r
60 // Load app configuration
\r
61 app.configure(configuration());
\r
63 // Enable security, CORS, compression, favicon and body parsing
\r
66 app.use(compress());
\r
67 app.use(express.json());
\r
68 app.use(express.urlencoded({ extended: true }));
\r
70 // Set up Plugins and providers
\r
71 app.configure(express.rest());
\r
72 app.configure(socketio(function (io) {
\r
73 io.on('connection', (socket) => {
\r
74 console.log('someone has connected')
\r
75 io.emit('message', "HI from nodejs");
\r
77 // Registering Socket.io middleware
\r
78 io.use(function (socket, next) {
\r
79 // Exposing a request property to services and hooks
\r
80 socket.feathers.referrer = socket.request.referrer;
\r
84 //app.configure(socketio());
\r
86 // const subscribe = redis.createClient(6379, 'localhost');
\r
87 // subscribe.subscribe('otf.execution.queue');
\r
89 // subscribe.on('connect', function () {
\r
90 // console.log("Connected to reids server")
\r
93 // subscribe.on('message', function (channel, message) {
\r
94 // console.log('Channel: ' + channel + ', Message: ' + message);
\r
95 // //client.sent(message);
\r
98 // io.on('connection', (socket) => {
\r
99 // console.log('user connected');
\r
101 // socket.on('message', (message) => {
\r
102 // console.log("Message Received: " + message);
\r
103 // io.emit('message', {type: 'new-message', text: message})
\r
107 // Configure Mongoose driver before setting up services that use Mongoose
\r
108 app.configure(mongoose);
\r
110 // Set up database dependent components once the connection is ready to prevent unexpected results
\r
111 _mongoose.connection.on('open', (ref) => {
\r
112 app.configure(authentication);
\r
114 // Set up our services (see `services/index.js`)
\r
115 app.configure(services);
\r
116 // Set up event channels (see channels.js)
\r
117 app.configure(channels);
\r
119 const userInterfacePath = path.join(__dirname, '..', '..', '..', 'client', 'dist');
\r
121 app.use('/', express.static(userInterfacePath));
\r
123 app.all('/*', function (req, res) {
\r
124 res.sendFile(path.join(userInterfacePath, 'index.html'), function (err) {
\r
126 res.status(500).send('Internal Server Error - This incident has been reported.');
\r
127 logger.error(JSON.stringify(err));
\r
132 // Configure a middleware for 404s and the error handler
\r
133 app.use(express.notFound());
\r
134 app.use(express.errorHandler({ logger }));
\r
136 app.hooks(appHooks);
\r
138 const port = app.get('port');
\r
139 const useSSL = app.get('ssl');
\r
143 // set up server with ssl (https)
\r
144 const certDirPath = path.join(__dirname, '..', '..', '..', 'server', 'config', 'cert');
\r
146 server = https.createServer({
\r
147 key: fs.readFileSync(path.normalize(certDirPath + path.sep + 'privateKey.pem')),
\r
148 cert: fs.readFileSync(path.normalize(certDirPath + path.sep + 'otf.pem'))
\r
149 }, app).listen(port);
\r
151 // set up server without ssl (http)
\r
152 server = http.createServer(app).listen(port);
\r
157 process.on('unhandledRejection', (reason, p) =>
\r
158 logger.error('Unhandled Rejection at: Promise ', p, reason)
\r
161 server.on('listening', () =>
\r
162 logger.info('Feathers application started on http://%s:%d', app.get('host'), port)
\r
166 module.exports = app;
\r