added svcapi ui and camunda code
[it/otf.git] / otf-frontend / server / src / feathers / index.js
diff --git a/otf-frontend/server/src/feathers/index.js b/otf-frontend/server/src/feathers/index.js
new file mode 100644 (file)
index 0000000..ad37c1b
--- /dev/null
@@ -0,0 +1,166 @@
+/*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
+#                                                                              #\r
+#   Licensed under the Apache License, Version 2.0 (the "License");            #\r
+#   you may not use this file except in compliance with the License.           #\r
+#   You may obtain a copy of the License at                                    #\r
+#                                                                              #\r
+#       http://www.apache.org/licenses/LICENSE-2.0                             #\r
+#                                                                              #\r
+#   Unless required by applicable law or agreed to in writing, software        #\r
+#   distributed under the License is distributed on an "AS IS" BASIS,          #\r
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
+#   See the License for the specific language governing permissions and        #\r
+#   limitations under the License.                                             #\r
+##############################################################################*/\r
+\r
+\r
+// Node.js modules\r
+const path = require('path');\r
+const https = require('https');\r
+const http = require('http');\r
+const fs = require('fs');\r
+\r
+// Express.js modules\r
+const express = require('@feathersjs/express');\r
+const compress = require('compression');\r
+const helmet = require('helmet');\r
+const cors = require('cors');\r
+const favicon = require('serve-favicon');\r
+\r
+// Feathers.js modules\r
+const feathers = require('@feathersjs/feathers');\r
+const configuration = require('@feathersjs/configuration');\r
+const socketio = require('@feathersjs/socketio'); //require('@feathersjs/socketio-client'); \r
+const io = require('socket.io'); //socket.io-client\r
+const socket = io();\r
+\r
+const services = require('./services');\r
+const appHooks = require('./app.hooks');\r
+const channels = require('./channels');\r
+const authentication = require('./authentication');\r
+\r
+// Mongoose\r
+const mongoose = require('../lib/mongoose');\r
+const _mongoose = require('mongoose');\r
+\r
+// Mongoose Plugins\r
+const { accessibleRecordsPlugin, accessibleFieldsPlugin } = require('@casl/mongoose');\r
+_mongoose.plugin(accessibleFieldsPlugin);\r
+_mongoose.plugin(accessibleRecordsPlugin);\r
+\r
+// Winston\r
+const logger = require('../lib/logger');\r
+\r
+// Redis\r
+const redis = require('redis');\r
+\r
+// Create a Express/Feathers application\r
+const app = express(feathers());\r
+\r
+// Load app configuration\r
+app.configure(configuration());\r
+\r
+// Enable security, CORS, compression, favicon and body parsing\r
+app.use(helmet());\r
+app.use(cors());\r
+app.use(compress());\r
+app.use(express.json());\r
+app.use(express.urlencoded({ extended: true }));\r
+\r
+// Set up Plugins and providers\r
+app.configure(express.rest());\r
+app.configure(socketio(function (io) {\r
+       io.on('connection', (socket) => {\r
+               console.log('someone has connected')\r
+               io.emit('message', "HI from nodejs");\r
+       });\r
+       // Registering Socket.io middleware\r
+       io.use(function (socket, next) {\r
+               // Exposing a request property to services and hooks\r
+               socket.feathers.referrer = socket.request.referrer;\r
+               next();\r
+       });\r
+}))\r
+//app.configure(socketio());\r
+\r
+// const subscribe = redis.createClient(6379, 'localhost');\r
+// subscribe.subscribe('otf.execution.queue');\r
+\r
+// subscribe.on('connect', function () {\r
+//     console.log("Connected to reids server")\r
+// })\r
+\r
+// subscribe.on('message', function (channel, message) {\r
+//     console.log('Channel: ' + channel + ', Message: ' + message);\r
+//     //client.sent(message);\r
+// });\r
+\r
+// io.on('connection', (socket) => {\r
+//     console.log('user connected');\r
+\r
+//     socket.on('message', (message) => {\r
+//             console.log("Message Received: " + message);\r
+//             io.emit('message', {type: 'new-message', text: message})\r
+//     });\r
+// });\r
+\r
+// Configure Mongoose driver before setting up services that use Mongoose\r
+app.configure(mongoose);\r
+\r
+// Set up database dependent components once the connection is ready to prevent unexpected results\r
+_mongoose.connection.on('open', (ref) => {\r
+       app.configure(authentication);\r
+\r
+       // Set up our services (see `services/index.js`)\r
+       app.configure(services);\r
+       // Set up event channels (see channels.js)\r
+       app.configure(channels);\r
+\r
+       const userInterfacePath = path.join(__dirname, '..', '..', '..', 'client', 'dist');\r
+\r
+       app.use('/', express.static(userInterfacePath));\r
+\r
+       app.all('/*', function (req, res) {\r
+               res.sendFile(path.join(userInterfacePath, 'index.html'), function (err) {\r
+                       if (err) {\r
+                               res.status(500).send('Internal Server Error - This incident has been reported.');\r
+                               logger.error(JSON.stringify(err));\r
+                       }\r
+               });\r
+       });\r
+\r
+       // Configure a middleware for 404s and the error handler\r
+       app.use(express.notFound());\r
+       app.use(express.errorHandler({ logger }));\r
+\r
+       app.hooks(appHooks);\r
+\r
+       const port = app.get('port');\r
+       const useSSL = app.get('ssl');\r
+       var server = null;\r
+\r
+       if(useSSL){\r
+               // set up server with ssl (https)\r
+               const certDirPath = path.join(__dirname, '..', '..', '..', 'server', 'config', 'cert');\r
+\r
+               server = https.createServer({\r
+                       key: fs.readFileSync(path.normalize(certDirPath + path.sep + 'privateKey.pem')),\r
+                       cert: fs.readFileSync(path.normalize(certDirPath + path.sep + 'otf.pem'))\r
+               }, app).listen(port);\r
+       }else{\r
+               // set up server without ssl (http)\r
+               server = http.createServer(app).listen(port);\r
+       }\r
+\r
+       app.setup(server);\r
+\r
+       process.on('unhandledRejection', (reason, p) =>\r
+               logger.error('Unhandled Rejection at: Promise ', p, reason)\r
+       );\r
+\r
+       server.on('listening', () =>\r
+               logger.info('Feathers application started on http://%s:%d', app.get('host'), port)\r
+       );\r
+});\r
+\r
+module.exports = app;\r