-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
275 lines (244 loc) · 7.63 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const cors = require('cors');
const passport = require('passport');
const LocalStrategy = require('passport-local');
const swaggerJSDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const usersRouter = require('./routes/users');
const courseRouter = require('./routes/courses');
const ExpressError = require('./utils/ExpressError');
const mongoose = require('mongoose');
const session = require('express-session');
const port = process.env.PORT || 8000;
const MongoStore = require('connect-mongo');
const User = require('./models/user');
const fetch = require('node-fetch');
const { URLSearchParams } = require('url');
const dayjs = require('dayjs');
const { Background } = require('./models/background');
const duration = require('dayjs/plugin/duration');
const { createServer } = require('http');
const sslRedirect = require('heroku-ssl-redirect').default;
const app = express();
const httpServer = createServer(app);
const io = require('socket.io')(httpServer, {
cors: {
origin: ['https://www.uniplanit.com', 'http://localhost:3000'],
methods: ['GET', 'POST'],
},
});
const {
addFriend,
acceptFriendRequest,
rejectFriendRequest,
getFriendRequest,
removeFriend,
getFriend,
} = require('./handlers/friendHandler')(io);
const { sendPrivateMessage } = require('./handlers/messageHandler')(io);
const {
getUserCourse,
addUserCourse,
deleteUserCourse,
deleteCourseSection,
generateTimetable,
getFavTimetable,
deleteFavTimetable,
getGeneratedTimetable,
addFavTimetable,
updateFavTimetable,
updateSelectedTimetable,
} = require('./handlers/courseHandler')(io);
dayjs.extend(duration);
require('dotenv').config();
// mongoose
mongoose
.connect(process.env.MONGO_URL)
.then(() => console.log('Mongo connection open'))
.catch((err) => console.error(err));
mongoose.connection.on('error', (err) => {
console.error(err);
});
mongoose.connection.on('disconnected', (err) => {
console.log(`disconnected from mongo ${err}`);
});
// session set up
const mongoStoreOptions = {
mongoUrl: process.env.MONGO_URL,
autoRemove: 'native',
touchAfter: 24 * 3600,
};
const sessionOptions = {
secret: process.env.SECRET,
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true,
// set to true in production
maxAge: 24 * 60 * 60 * 1000,
secure: false,
},
store: MongoStore.create(mongoStoreOptions),
};
const swaggerDefinition = {
openai: '3.0.0',
info: {
title: 'Event API for MyCalendar',
version: '1.0.0',
description:
'Event Restful API for MyCalendar. Used for CRUD operations related to events',
},
host: `localhost:${port}`,
basePath: `/`,
};
const swaggerSpec = swaggerJSDoc({
swaggerDefinition,
apis: [`${__dirname}/routes/*.js`, `${__dirname}/routes/parameters.yaml`],
});
if (app.get('env') === 'production') {
app.set('trust proxy', 1);
sessionOptions.cookie.secure = true;
}
app.use(session(sessionOptions));
app.use(passport.initialize());
app.use(passport.session());
//passport stuff
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser()); //session encoding
passport.deserializeUser(User.deserializeUser()); //session decoding
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(sslRedirect());
app.use('/users', usersRouter);
app.use('/courses', courseRouter);
app.use(
cors({
origin: ['https://www.uniplanit.com', 'http://localhost:3000'],
})
);
app.get('/photo', async (req, res, next) => {
// Return a background picture for login and sign up
const now = dayjs().utc();
const lastSavedBackground = await Background.findOne();
if (!lastSavedBackground) {
fetch(
'https://api.unsplash.com/photos/random?' +
new URLSearchParams({
query: 'landscape',
}),
{
headers: {
'Accept-Version': 'v1',
Authorization: `Client-ID ${process.env.UNSPLASH_ACCESS}`,
},
}
)
.then((response) => response.json())
.then((data) => {
const newBackground = new Background({
imgUrl: data.urls.full,
savedTime: new Date(now.startOf('d')),
});
newBackground.save();
res.json(newBackground.imgUrl);
})
.catch((err) => next(new ExpressError(err, 500)));
} else if (
now.diff(lastSavedBackground.savedTime) >
dayjs.duration(1, 'hours').as('ms')
) {
fetch(
'https://api.unsplash.com/photos/random?' +
new URLSearchParams({
query: 'landscape',
}),
{
headers: {
'Accept-Version': 'v1',
Authorization: `Client-ID ${process.env.UNSPLASH_ACCESS}`,
},
}
)
.then((response) => response.json())
.then((data) => {
lastSavedBackground.imgUrl = data.urls.full;
lastSavedBackground.savedTime = new Date(now.startOf('h').utc());
lastSavedBackground.save();
res.json(lastSavedBackground.imgUrl);
})
.catch((err) => next(new ExpressError(err, 500)));
} else {
res.json(lastSavedBackground.imgUrl);
}
});
// socket io
const wrap = (middleware) => (socket, next) =>
middleware(socket.request, {}, next);
io.use(wrap(session(sessionOptions)));
io.use(wrap(passport.initialize()));
io.use(wrap(passport.session()));
io.use(async (socket, next) => {
const session = socket.request.session;
if (socket.request.user) {
session.socketId = socket.id;
socket.userId = socket.request.user.id;
socket.join(socket.userId);
await session.save();
next();
} else {
console.log('Not authenticated');
next(new ExpressError('Not authenticated', 401));
}
});
io.on('connection', async (socket) => {
console.log(`A user has connected ${socket.id}`);
socket.on('get friend', getFriend);
socket.on('add friend', addFriend);
socket.on('remove friend', removeFriend);
socket.on('accept friend request', acceptFriendRequest);
socket.on('reject friend request', rejectFriendRequest);
socket.on('private message', sendPrivateMessage);
socket.on('get friend request', getFriendRequest);
socket.on('get user course', getUserCourse);
socket.on('add user course', addUserCourse);
socket.on('delete course', deleteUserCourse);
socket.on('delete section', deleteCourseSection);
socket.on('get generated timetable', getGeneratedTimetable);
socket.on('generate timetable', generateTimetable);
socket.on('add fav timetable', addFavTimetable);
socket.on('get fav timetable', getFavTimetable);
socket.on('delete fav timetable', deleteFavTimetable);
socket.on('update fav timetable', updateFavTimetable);
socket.on('update selected timetable', updateSelectedTimetable);
});
io.on('disconnect', (socket) => {
socket.on('disconnect', (reason) => {
console.log(reason);
});
});
app.use(express.static(path.join(__dirname, '/client/build')));
app.use((req, res) => {
res.sendFile(path.join(__dirname, '/client/build/index.html'));
});
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(new ExpressError('Page not found', 404));
});
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// redirect to home page
res.redirect('localhost:3000');
});
httpServer.listen(port, () => {
console.log('listening on ' + port);
});
module.exports = app;