-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
149 lines (117 loc) · 3.96 KB
/
server.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
// Server
require('dotenv').config();
const express = require('express');
const http = require('http')
const https = require('https')
const fs = require('fs')
const path = require('path');
const session = require('express-session');
const flash = require('connect-flash');
const cookieParser = require('cookie-parser');
const socketio = require('socket.io');
const appPort = process.env.APP_PORT || 3000;
// App Services
const { passport } = require('./app/passport.js');
const createSearchService = require('./app/search.js');
// GraphQL
const { installHandler } = require('./graphql/api_handler.js');
const { graphqlUploadExpress } = require('graphql-upload');
// Routes
const { defineHomeRoutes } = require('./routes/home.js');
const { defineLessonRoutes } = require('./routes/lessons.js');
const { defineAccountProfileRoutes } = require('./routes/accountProfile.js');
const { defineVideoRoutes } = require('./routes/videos.js');
const { defineLikeRoutes } = require('./routes/likes.js');
const { defineVideoChatRoutes } = require('./routes/videoChat.js');
const { defineAuthenticationRoutes } = require('./routes/authentication.js');
const { defineUserRoutes } = require('./routes/users.js');
const { defineStripeRoutes } = require('./routes/stripe.js');
const { defineReportABugRoutes } = require('./routes/report-a-bug.js');
const { defineBecomeVerifiedRoutes } = require('./routes/become-verified.js');
// Database
const {
addSocketUser,
getSocketUserByUserID ,
deleteSocketUser,
} = require('./database/methods/socketUsers.js');
// Configure Server
const app = express();
app.set('views', __dirname + '/views');
app.set('view engine', '.jsx');
app.engine('jsx', require('express-react-views').createEngine());
// For login sessions
app.use(session({
secret: 'supersecret',
resave: true,
saveUninitialized: true
}));
app.use(flash());
app.use(cookieParser());
app.use(express.urlencoded());
app.use(express.static(path.join(__dirname, 'public')));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.json());
(async function start(){
try{
// Install GraphQL API Handler
app.route('/graphql').post(graphqlUploadExpress());
await installHandler(app);
// Home route
defineHomeRoutes(app);
// Lessons routes
defineLessonRoutes(app);
// Video routes
const VideoSearchService = await createSearchService('videos', ['title', 'languageOfTopic']);
defineVideoRoutes(app, VideoSearchService);
// Like routes
defineLikeRoutes(app);
// Account Profile routes
defineAccountProfileRoutes(app);
// Video Chat routes
defineVideoChatRoutes(app);
// Authentication routes
defineAuthenticationRoutes(app);
// User routes
defineUserRoutes(app);
// Stripe routes
defineStripeRoutes(app);
// Report a bug
defineReportABugRoutes(app);
// Become Verified
defineBecomeVerifiedRoutes(app);
const httpsOptions = {
key: fs.readFileSync('./security/cert.key'),
cert: fs.readFileSync('./security/cert.pem')
}
let server;
if(appPort == 443){
server = https.createServer(httpsOptions, app);
} else {
server = http.createServer(app);
}
// Start app
server.listen(appPort, () => {
console.log(`App up on port ${appPort}`);
});
const io = socketio(server);
io.on('connection', (socket) => {
// socket.emit('Hello Client');
socket.on('disconnect', () => {
deleteSocketUser(socket.id);
});
socket.on('Hello Server', async (userID) => {
const user = await getSocketUserByUserID(userID);
if (user) return;
addSocketUser(socket.id, userID);
});
socket.on('Call Sent', async (forUserID, content) => {
// console.log('Server received call.');
const user = await getSocketUserByUserID(forUserID);
socket.to(user.socketID).emit('Call Incoming', { content, from: socket.id });
});
});
} catch(err){
console.log(`Error: ${err}`);
}
})();