-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
204 lines (185 loc) · 7.45 KB
/
index.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
/*
Main Server file
- basically just linking the real functions together
*/
/*
Required Modules
*/
/*
general express modules
- express, http: to start the server
- path: to use os paths for accessing files
- session, MongoDBStore: to use sessions as client management
- bodyParser, cookieParser: to be able to read the respective group
*/
let express = require("express");
let http = require('http');
let path = require("path");
let session = require('express-session');
let MongoDBStore = require('connect-mongodb-session')(session);
let bodyParser = require('body-parser');
let cookieParser = require("cookie-parser");
/*
more special libraries
- webPush: to send notifications
- socketio, socketChat (own): to use a live chat with web sockets
- passport, passportSocketIo: to authenticate everything and as user system
*/
let webPush = require('web-push');
let socketio = require("socket.io");
let socketChat = require("./app_modules/socketChat/socketChat.js");
let passport = require('passport');
let passportSocketIo = require("passport.socketio");
/*
own routers
- staticRouter: route static files (for some files a pure express.static just isnt fitting well)
- login, register: login/register page incl. handling the login/register attempts
- cardRouter: cards are the DOM elements of each podcast episode; they get rendered on server and send to client
- podcastTimers: for each user we store the progress of each podcast episode; this recieves and sends those informations
- subscribe: handles subscriptions to the notifications; if activated on client, it sends its webPush Endpoint every time it gets opened
- changePassword: handles the change password request of the settings popup on main page
- getMessages: route that sends the (usually latest) 10 requested messages of a room
- nameColor: set and get the color of the username (when displayed above messages)
- adminPage: the whole admin page router
*/
let staticRouter = require("./staticRouter.js");
let login = require("./pages/login/login_server.js");
let register = require("./pages/register/register_server.js");
let cardRouter = require("./app_modules/podcasts/podcasts_router.js");
let podcastTimers = require("./app_modules/user/routers/podcastTimers.js");
let subscribe = require("./app_modules/notifications/subscribe_router.js");
let changePassword = require("./app_modules/user/routers/changePassword.js");
let getMessages = socketChat.router;
let nameColor = require("./app_modules/user/routers/nameColor.js");
let adminPage = require("./pages/admin/admin_server.js");
let mainPage = require("./pages/main/main_server.js");
/*
own functions
- initPassport: defines the register and login rules
- socketChatInit: inits socket chat; surprise
- timeLogging: instead of console.log() i use timeLogging() which is the same but adds a time and optionally the current user infront of the message
*/
let initPassport = require("./app_modules/user/passport.js");
let timeLogging = require('./app_modules/logging/timeLogging.js');
let socketChatInit = socketChat.socketChatInit;
/*
own Middlewares
- checkAuthenticated: checks on /login and /register page if user is already authenticated and redirects to main page if true
- checkNotAuthenticated: checks if user is authenticated; if not he gets redirected to /login; is used nearly everywhere
- filterbubbleAuthentication: every user is only allowed to see/access certain podcast categories (defined in filterbubbles); this middleware enforces it
*/
let checkAuthenticated = require('./app_modules/authenticationMiddlewares/authenticationMiddlewares.js').checkAuthenticated;
let checkNotAuthenticated = require('./app_modules/authenticationMiddlewares/authenticationMiddlewares.js').checkNotAuthenticated;
let filterbubbleAuthentication = require("./app_modules/filterbubbles/filterbubbles_authentication_middleware.js").filterbubbleAuthentication;
/*
secrets
- information that should not get stored on github
- are stored in an object in a secrets.js file
*/
let secrets = require('./secrets.js');
/*
App Variables
*/
let app = express();
let port = "3000";
// generated vapi keys for webPush
// ./node_modules/.bin/web-push generate-vapid-keys
const publicVapidKey= secrets.publicVapidKey;
const privateVapidKey= secrets.privateVapidKey;
/*
App Configuration
*/
// to store the current active express sessions
var sessionStore = new MongoDBStore({
uri: 'mongodb://localhost:27017/podcastApp',
collection: 'mySessions'
});
// to read the req.body
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
// static directory for everything that does not need to be protected
app.use(express.static(path.join(__dirname, "static")));
// pug layout folder
app.set('view-engine', 'pug');
app.set("views", __dirname);
// express-session init (a session timeouts after 1 month)
app.use(session({
secret: secrets.sessionSecret,
resave: true,
saveUninitialized: false,
cookie: {
maxAge: 30 * 86400 * 1000, //1 month
},
store: sessionStore
}))
// passport inits
app.use(passport.initialize());
app.use(passport.session());
initPassport();
// web-push init
webPush.setVapidDetails(secrets.mail, publicVapidKey, privateVapidKey);
// prototype functions that will return *current real date + time* on later use
Date.prototype.today = function () {
return ((this.getDate() < 10)?"0":"") + this.getDate() +"/"+(((this.getMonth()+1) < 10)?"0":"") + (this.getMonth()+1) +"/"+ this.getFullYear();
}
Date.prototype.timeNow = function () {
return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
}
// for socket.io
let server = http.createServer(app);
let io = socketio(server);
io.use(passportSocketIo.authorize({
key: 'connect.sid',
secret: secrets.passportSocketSecret,
store: sessionStore,
passport: passport,
cookieParser: cookieParser
}));
// own socket.io function/event handling
socketChatInit(io);
/*
Middlewares
- we dont check authentication on /logout and /manifest.json (bc it throws errors if we do)
- on /login and /register we test if client is already authenticated and if yes we redirect to main page
- user can only access /podcast-episodes/ fragments if they are allowed to / in the appropriate filterbubble
- for everything else you need to be logged in
*/
app.get("*", function(req, res, next){
if (req.url == "/logout" || req.url == "/manifest.json" )
return next();
if (req.url.startsWith("/login") || req.url.startsWith("/register")) {
return checkNotAuthenticated(req, res, next); //test if authenticated and if yes redirect to "/"
}
if (req.url.startsWith('/podcast-episodes/')) {
return filterbubbleAuthentication(req, res, next); //Filterbubble authentication Middleware
}
checkAuthenticated(req, res, next); //test if authenticated and if not redirect to "/login"
});
/*
Routes Definitions
*/
app.use("/", staticRouter);
app.use("/admin", adminPage);
app.use("/login", login);
app.use("/register", register);
//archive page
app.get("/", mainPage);
app.use("/", cardRouter);
app.use("/changePassword", changePassword);
app.use("/podcastTimers", podcastTimers);
app.use("/subscribe", subscribe);
app.use("/getMessages", getMessages);
app.use("/nameColor", nameColor);
//logout
app.get('/logout', function(req, res) {
req.logout();
res.redirect("/login");
});
/*
Server Activation
*/
server.listen(port, () => {
timeLogging("Listening to requests on http://localhost:" + port);
});