This repository has been archived by the owner on Jun 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
170 lines (151 loc) · 5.14 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
'use strict';
const cpuCount = require('os').cpus().length;
//Config
const config = require("./config.js");
//Modules
const express = require("express");
const bcrypt = require('bcrypt');
const bodyParser = require("body-parser");
const mongoose = require('mongoose');
const passport = require('passport');
const BasicStrategy = require('passport-http').BasicStrategy;
const LocalStrategy = require('passport-local').Strategy;
const JsonStrategy = require('passport-json').Strategy;
const session = require('express-session');
const cookieParser = require('cookie-parser');
const RedisStore = require('connect-redis')(session);
const middleware_module = require('./middleware_module.js');
const xss = require('xss-clean');
const cluster = require('cluster');
//Models
const User = require('./models/user_model.js');
if (cluster.isMaster) {
for (var i = 0; i < cpuCount; i++) {
cluster.fork();
}
} else {
//Init Express
const app = express();
//app.use directives
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(bodyParser.json()); // for parsing application/json
app.use(xss());
app.use(cookieParser());
app.use(session({
store: new RedisStore({
url: config.redis_url
}),
secret: config.session_secret,
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
//Mongoose initialisiation
const mongo_options = { auto_reconnect: true, reconnectTries: 100, reconnectInterval: 3000, keepAlive: 120, connectTimeoutMS: 3000 };
const db = mongoose.connection;
db.on('connecting', function () {
console.log('connecting to MongoDB...');
});
db.on('error', function (error) {
console.error('Error in MongoDb connection: ' + error);
mongoose.disconnect();
});
db.once('open', function () {
console.log('MongoDB connection opened!');
});
db.on('reconnected', function () {
console.log('MongoDB reconnected!');
});
db.on('disconnected', function () {
console.log('MongoDB disconnected!');
mongoose.connect(config.mongo_connect, mongo_options);
});
mongoose.connect(config.mongo_connect, mongo_options);
//Create bootstrap user
mongoose.connection.on('connected', function () {
mongoose.connection.db.collection('users').count(function (err, count) {
if (count == 0) {
console.log("Create bootstrap user");
bcrypt.hash(config.bootstrap_user_password, config.crypt_saltRounds, function (err, hash) {
if (err) console.log(err);
else {
var user1 = new User({ name: "bootstrap", password: hash, rep: config.bootstrap_user_reputation });
user1.save(function (err, userObj) {
if (err) console.log(err);
});
}
});
}
});
});
//Auth
passport.use(new BasicStrategy(
function (username, password, done) {
User.findOne({ name: username }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
bcrypt.compare(password, user.password, function (err, res) {
if (res == false) { return done(null, false); }
else { return done(null, user); }
});
});
}
));
passport.use(new JsonStrategy(
function (username, password, done) {
User.findOne({ name: username }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
bcrypt.compare(password, user.password, function (err, res) {
if (res == false) { return done(null, false); }
else { return done(null, user); }
});
});
}
));
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
//Router
app.use(express.static(__dirname + '/public'));
app.use('/users', require('./routes/user_route.js'));
app.use('/tickets', require('./routes/ticket_route.js'));
app.use('/comments', require('./routes/comment_route.js'));
app.use('/rooms', require('./routes/room_route.js'));
//Root (/) - GET
app.get("/api", function (req, res) {
res.send(config.info_api_root);
});
//Login route
app.get("/login", passport.authenticate('basic', { session: true }), function (req, res) {
res.sendStatus(200);
});
//Login route - post
app.post("/login", passport.authenticate('json', { session: true }), function (req, res) {
res.json({ user: req.user });
});
//Route that shows whether a user is logged in or not
app.get("/auth", middleware_module.checkloggedin_silent, function (req, res) {
res.json({ user: req.user });
});
//Logout route
app.post("/logout", middleware_module.checkloggedin, function (req, res) {
req.session.destroy();
req.logout();
res.json({ user: null });
});
//The 404 Route
app.get('*', function (req, res) {
res.sendFile('public/index.html', { root: __dirname });
});
//Start server
var server = app.listen(config.app_port, function () {
console.log("Listening on port %s...", server.address().port);
});
}