added svcapi ui and camunda code
[it/otf.git] / otf-frontend / server / src / feathers / index.js
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\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
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\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
15 \r
16 \r
17 // Node.js modules\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
22 \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
29 \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
36 \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
41 \r
42 // Mongoose\r
43 const mongoose = require('../lib/mongoose');\r
44 const _mongoose = require('mongoose');\r
45 \r
46 // Mongoose Plugins\r
47 const { accessibleRecordsPlugin, accessibleFieldsPlugin } = require('@casl/mongoose');\r
48 _mongoose.plugin(accessibleFieldsPlugin);\r
49 _mongoose.plugin(accessibleRecordsPlugin);\r
50 \r
51 // Winston\r
52 const logger = require('../lib/logger');\r
53 \r
54 // Redis\r
55 const redis = require('redis');\r
56 \r
57 // Create a Express/Feathers application\r
58 const app = express(feathers());\r
59 \r
60 // Load app configuration\r
61 app.configure(configuration());\r
62 \r
63 // Enable security, CORS, compression, favicon and body parsing\r
64 app.use(helmet());\r
65 app.use(cors());\r
66 app.use(compress());\r
67 app.use(express.json());\r
68 app.use(express.urlencoded({ extended: true }));\r
69 \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
76         });\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
81                 next();\r
82         });\r
83 }))\r
84 //app.configure(socketio());\r
85 \r
86 // const subscribe = redis.createClient(6379, 'localhost');\r
87 // subscribe.subscribe('otf.execution.queue');\r
88 \r
89 // subscribe.on('connect', function () {\r
90 //      console.log("Connected to reids server")\r
91 // })\r
92 \r
93 // subscribe.on('message', function (channel, message) {\r
94 //      console.log('Channel: ' + channel + ', Message: ' + message);\r
95 //      //client.sent(message);\r
96 // });\r
97 \r
98 // io.on('connection', (socket) => {\r
99 //      console.log('user connected');\r
100 \r
101 //      socket.on('message', (message) => {\r
102 //              console.log("Message Received: " + message);\r
103 //              io.emit('message', {type: 'new-message', text: message})\r
104 //      });\r
105 // });\r
106 \r
107 // Configure Mongoose driver before setting up services that use Mongoose\r
108 app.configure(mongoose);\r
109 \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
113 \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
118 \r
119         const userInterfacePath = path.join(__dirname, '..', '..', '..', 'client', 'dist');\r
120 \r
121         app.use('/', express.static(userInterfacePath));\r
122 \r
123         app.all('/*', function (req, res) {\r
124                 res.sendFile(path.join(userInterfacePath, 'index.html'), function (err) {\r
125                         if (err) {\r
126                                 res.status(500).send('Internal Server Error - This incident has been reported.');\r
127                                 logger.error(JSON.stringify(err));\r
128                         }\r
129                 });\r
130         });\r
131 \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
135 \r
136         app.hooks(appHooks);\r
137 \r
138         const port = app.get('port');\r
139         const useSSL = app.get('ssl');\r
140         var server = null;\r
141 \r
142         if(useSSL){\r
143                 // set up server with ssl (https)\r
144                 const certDirPath = path.join(__dirname, '..', '..', '..', 'server', 'config', 'cert');\r
145 \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
150         }else{\r
151                 // set up server without ssl (http)\r
152                 server = http.createServer(app).listen(port);\r
153         }\r
154 \r
155         app.setup(server);\r
156 \r
157         process.on('unhandledRejection', (reason, p) =>\r
158                 logger.error('Unhandled Rejection at: Promise ', p, reason)\r
159         );\r
160 \r
161         server.on('listening', () =>\r
162                 logger.info('Feathers application started on http://%s:%d', app.get('host'), port)\r
163         );\r
164 });\r
165 \r
166 module.exports = app;\r