forked from zurichat/zc_plugin_company_goals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
141 lines (117 loc) · 4.33 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* eslint-disable no-unused-vars */
/* eslint-disable import/order */
const path = require('path');
const compression = require('compression');
const cookieParser = require('cookie-parser');
const cors = require('cors');
const dotenv = require('dotenv');
const express = require('express');
// const helmet = require('helmet');
const morgan = require('morgan');
const xss = require('xss-clean');
const swaggerUi= require('swagger-ui-express');
const yaml = require('yamljs');
const documentation = yaml.load('./docs/documentation.yaml');
dotenv.config();
const globalErrorHandler = require('./controllers/errorController');
// Require Routes
const goalRouter = require('./routes/goalRoutes');
const pluginInfoRouter = require('./routes/infoRoute');
const searchRouter = require('./routes/searchRoute');
const missionRouter = require('./routes/missionRoute.js');
const pingRouter = require('./routes/pingRoute');
const sidebarRouter = require('./routes/sidebarRoute.js');
const roomRouter = require('./routes/roomRoute');
const userRouter = require('./routes/userRoute');
const notificationRouter = require('./routes/notificationRoute');
const authRouter = require('./routes/auth');
const visionRouter = require('./routes/visionRoutes');
const realTimeupdateRouter = require('./routes/realTimeupdates');
const AppError = require('./utils/appError');
const rateLimiter = require('./utils/rateLimiter');
const app = express();
// Implement cors
if (process.env.NODE_ENV === 'production') {
app.use(cors({ origin: ['*'] }));
} else {
const whitelist = ['http://localhost:9000', 'https://zuri.chat', 'http://localhost:4000'];
const corsOptions = {
origin(origin, callback) {
if (whitelist.indexOf(origin) !== -1 || !origin) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
};
app.use(cors(corsOptions));
}
// app.options('*', cors());
// Add secure headers
// app.use(helmet());
// Development logging
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
// Reading data from the body into req.body. The limit option manages how large the data can be
app.use(
express.json({
limit: '10kb',
})
);
// Parse cookies
app.use(cookieParser());
// Data Sanitization against XSS
app.use(xss());
// Compress text sent to client
app.use(compression());
// swagger setup
// const swaggerUi = require('swagger-ui-express');
// const swaggerJSDocument = require('swagger-jsdoc');
// const swaggerOptions = {
// definition: {
// openapi: '3.0.0',
// info: {
// title: 'Company Goals Plugin API',
// version: '1.0.0',
// description: 'Company Goals plugin api for zuri chat application documentation',
// servers: ['https://goals.zuri.chat/api'],
// },
// },
// apis: ['./routes/*.js'],
// };
// const swaggerDocs = swaggerJSDocument(swaggerOptions);
// To serve frontend build files in production
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, 'client/dist')));
app.use(express.static(path.join(__dirname, 'serve-client/dist')));
}
app.get('/zuri-plugin-company-goals.js', (req, res) => {
res.sendFile(path.join(__dirname, 'client/dist/zuri-plugin-company-goals.js'));
});
// Api routes
app.use('/api/v1/goals', goalRouter);
app.use('/api/v1/rooms', rateLimiter(), roomRouter);
app.use('/api/v1/users', rateLimiter(), userRouter);
app.use('/api/v1/search', rateLimiter(), searchRouter);
app.use('/ping', rateLimiter(), pingRouter);
app.use('/api/v1/sidebar', rateLimiter(), sidebarRouter);
app.use('/info', rateLimiter(), pluginInfoRouter);
app.use('/api/v1/vision', visionRouter);
app.use('/api/v1/mission', missionRouter);
app.use('/api/v1/notifications', notificationRouter);
app.use('/api/v1/realTimeupdates', realTimeupdateRouter);
// app.use('/v1/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs));
app.use('/api/v1/docs', swaggerUi.serve, swaggerUi.setup(documentation));
app.use('/api/v1/auth', authRouter);
// Send all 404 requests not handled by the server to the Client app
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'serve-client/dist', 'index.html'));
});
// To catch all unhandled routes
app.all('*', (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl} on this server!`, 404));
});
// Global error handler
app.use(globalErrorHandler);
module.exports = app;