-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
208 lines (170 loc) · 6.43 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
'use strict';
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var config = require('./config/config');
// ########################################
// ### Application Translation ###
// ########################################
var i18n = require("i18n");
i18n.configure({
locales : [ 'en', 'fr' ],
directory : __dirname + '/locales',
cookie : 'shuttersLocale',
objectNotation : true
});
// ########################################
// ### Application logging ###
// ########################################
var logger = require('./utils').logger.main;
// ########################################
// ### Scheduling ###
// ########################################
var schedule = require('node-schedule');
var Shutter = require("./services/shuttersCommand");
var shutterCmd = new Shutter();
// Every days at 1 AM recalculate the today's up and down time
var rule = new schedule.RecurrenceRule();
// hour is set to 3:30 to avoid problems with day saving light changes
// (remain in the same day and no minute overlaps)
rule.hour = config.daySetupScheduler.hour;
rule.minute = config.daySetupScheduler.minute;
logger.info("Everyday scheduler rule: ", rule);
schedule.scheduleJob(rule, shutterCmd.scheduleShutters);
// Call the function for the first time to set things up for the current day.
var newScheduleTime = new Date(new Date().getTime() + 5000);
schedule.scheduleJob(newScheduleTime, shutterCmd.scheduleShutters);
// ########################################
// ### Request logging ###
// ########################################
var morgan = require('morgan');
// ########################################
// ### Database Configuration ###
// ########################################
var db = require('./db');
// ########################################
// ### Passport Authentication ###
// ########################################
var passport = require('passport');
var Strategy = require('passport-local').Strategy;
var bcrypt = require('bcrypt');
// Configure the local strategy for use by Passport.
// The local strategy require a `verify` function which receives the credentials
// (`username` and `password`) submitted by the user. The function must verify
// that the password is correct and then invoke `cb` with a user object, which
// will be set at `req.user` in route handlers after authentication.
passport.use(new Strategy(function(username, password, cb) {
db.users.findByUsername(username, function(err, user) {
if (err) {
logger.error(err);
return cb(err);
}
if (!user) {
logger.info("Username: " + username + ", not right user.");
return cb(null, false);
}
logger.info("Username: " + username + ", password: " + password + ", hashed: " + user.password + ", compare: " + bcrypt.compareSync(password, user.password));
if (!bcrypt.compareSync(password, user.password)) {
logger.info("Username: " + username + ", not right password.");
return cb(null, false);
}
logger.debug("Username: " + username + ", everything is fine.");
return cb(null, user);
});
}));
// Configure Passport authenticated session persistence.
// In order to restore authentication state across HTTP requests, Passport needs
// to serialize users into and deserialize users out of the session. The
// typical implementation of this is as simple as supplying the user ID when
// serializing, and querying the user record by ID from the database when
// deserializing.
passport.serializeUser(function(user, cb) {
cb(null, user._id);
});
passport.deserializeUser(function(id, cb) {
db.users.findUserById(id, function(err, user) {
if (err) {
return cb(err);
}
cb(null, user);
});
});
// ########################################
// ### Express Configuration ###
// ########################################
var routes = require('./routes/index');
var users = require('./routes/users');
var shutters = require('./routes/shutters');
var monitoring = require('./routes/monitoring');
var app = express();
app.use(i18n.init);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// uncomment after placing your favicon in /public
app.use(favicon(path.join(__dirname, '/public/', 'favicon.ico')));
// set logging format
// app.use(morgan(':remote-addr :remote-user [:date[iso]] :status :response-time
// :method :url'));
app.use(morgan(':status :response-time :method :url'));
// Session settings
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended : true
}));
app.use(cookieParser('secret'));
app.use(express.static(path.join(__dirname, 'public')));
var session = require('express-session');
var sessionStore = new session.MemoryStore();
app.use(session({
secret : 'secret',
resave : true,
store : sessionStore,
saveUninitialized : true,
cookie : {
maxAge : 600000
}
}));
// Initialize Passport and restore authentication state, if any, from the
// session.
app.use(passport.initialize());
app.use(passport.session());
app.use('/', routes);
app.use('/users', users);
app.use('/shutters', shutters);
app.use('/monitoring', monitoring);
app.use(express.static('public'));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found [' + req.originalUrl + ']');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
var localConfig = require('./config/config');
res.status(err.status || 500);
res.render('error', {
message : err.message,
error : err,
contextPath : config.webApp.rootPath
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
var localConfig = require('./config/config');
res.status(err.status || 500);
res.render('error', {
message : err.message,
error : {},
contextPath : config.webApp.rootPath
});
});
module.exports = app;