-
Notifications
You must be signed in to change notification settings - Fork 67
/
app.js
169 lines (139 loc) · 5.19 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* 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 syncRouter = require('./routes/syncRoute');
const pluginRouter = require('./routes/pluginRoute');
const suggestionRouter = require('./routes/suggestionsRoute');
const visionRouter = require('./routes/visionRoutes');
const Bsearch = require('./routes/BsearchRoute');
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://staging.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);
app.use('/node_modules', express.static(path.join(__dirname, 'node_modules')));
app.use('/testfiles', express.static(path.join(__dirname, 'test')));
// 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('/ping', pingRouter);
app.use('/info', pluginInfoRouter);
app.use('/api/v1/auth', authRouter);
app.use('/api/v1/bsearch', Bsearch);
app.use('/api/v1/users', userRouter);
app.use('/api/v1/goals', goalRouter);
app.use('/api/v1/org', roomRouter);
app.use('/api/v1/search', searchRouter);
app.use('/api/v1/vision', visionRouter);
app.use('/api/v1/sidebar', sidebarRouter);
app.use('/api/v1/mission', missionRouter);
app.use('/api/v1/notifications', notificationRouter);
app.use('/api/v1/search-suggestions', suggestionRouter);
app.use('/api/v1/sync', syncRouter);
app.use('/api/v1', pluginRouter);
// app.use('/api/v1/realTimeupdates', realTimeupdateRouter);
app.use('/test', (req, res) => {
res.sendFile(path.join(__dirname, 'test/index.html'));
});
// app.use('/api/v1/realTimeu☻pdates', realTimeupdateRouter);
// app.use('/v1/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs));
// app.use('/api/v1/docs', swaggerUi.serve, swaggerUi.setup(documentation));
// Serve search static files
app.get('/search', (req, res) => {
res.sendFile(path.join(__dirname, 'public/index.html'));
});
// Send all 404 requests not handled by the server to the Client app
// Lordbees 8-9 task
app.use(express.static(path.join(__dirname, 'test/bsearch/images')));
app.get('/bsearchtest/', (req, res) => {
res.sendFile(path.join(__dirname, '/test/bsearch/index.html'));
});
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;