-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
104 lines (88 loc) · 3.31 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
const express = require("express"),
app = express(),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
passport = require("passport"),
LocalStrategy = require("passport-local"),
authRoutes = require("./routes/index"),
listUsersRoutes = require("./routes/listusers"),
opCenterRoutes = require("./routes/opcenter"),
userRoutes = require("./routes/user.js"),
calendarRoutes = require("./routes/calendar.js"),
flash = require("connect-flash"),
session = require('express-session'),
cookieParser = require("cookie-parser"),
favicon = require('serve-favicon'),
path = require('path'),
Config = require("./models/settings"),
Lodash = require("lodash");
const NODE_PORT = process.env.NODE_PORT || 3000;
const MONGO_IP = process.env.MONGO_IP || 'localhost';
const MONGO_PORT = process.env.MONGO_PORT || '27017';
const PASSPORT_SECRET = process.env.PASSPORT_SECRET || 'Im_dumb_and_didnt_set_my_passport_secret';
mongoose.connect("mongodb://" + MONGO_IP + ':' + MONGO_PORT + "/persman", {useNewUrlParser: true, useUnifiedTopology: true});
app.use(bodyParser.urlencoded({extended: true}));
mongoose.set('useFindAndModify', false);
app.set("view engine", "ejs");
app.use(express.static('public'));
app.use(cookieParser('secret'));
app.use(favicon(path.join(__dirname, '.', 'public', 'favicon.ico')));
//PASSPORT
app.use(require("express-session")({
secret: PASSPORT_SECRET,
resave: false,
saveUninitialized: false
}));
Config.find({}, function(err, result) {
if (err) throw err;
const defaultConfig = require("./default-settings.json");
if (result.length == 0) {
let DefaultConfig = new Config(defaultConfig);
DefaultConfig.save();
Start(DefaultConfig);
} else {
let config = result[0];
if (Object.keys(defaultConfig).length !== Object.keys(config._doc).length-2) {
let oldConfigDoc = Lodash.cloneDeep(config._doc);
delete oldConfigDoc._id;
delete oldConfigDoc.__v;
Config.findByIdAndDelete(config._id, function(err, result) {
if (err) throw err;
let newConfig = Object.assign(defaultConfig, oldConfigDoc)
config = new Config(newConfig);
config.save();
Start(config);
})
} else {
Start(config);
}
}
});
function Start(config) {
let User = require("./models/user")(config);
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(function(req, res, next){
res.locals.config = config;
res.locals.currentUser = req.user;
res.locals.success = req.flash('success');
res.locals.error = req.flash('error');
next();
});
app.use(authRoutes);
app.use(listUsersRoutes);
app.use(opCenterRoutes);
app.use(userRoutes);
app.use(calendarRoutes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
res.status(404).render('404');
});
app.listen(NODE_PORT, function(){
console.log("PERSMAN: ONLINE") ;
});
}