-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
221 lines (202 loc) · 5.32 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
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* Base application
*/
var express = require('express'),
fs = require('fs'),
querystring = require('querystring'),
mongoose = require('mongoose'),
conf = require('node-config'),
crypto = require('crypto'),
cron = require('cron').CronJob;
var SessionMongoose = require('session-mongoose');
var mongooseSessionStore;
var app = module.exports = express.createServer();
// Asynchronously load config settings then finish booting app
conf.initConfig(function(err) {
if (err) throw err;
mongooseSessionStore = new SessionMongoose({
url: conf.db
});
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.cookieParser());
app.use(express.session({
cookie: {maxAge: 1000 * 86400 * 365 * 5},
secret: conf.secret,
store: mongooseSessionStore // Or Redis?
}));
// flash messages
app.use(function (req, res, next) {
req.flash = function (message) {
if (!req.session.messages || !(req.session.messages.length > 0)) {
req.session.messages = [];
}
req.session.messages.push(message);
}
req.message_text = "";
if (req.session.messages && req.session.messages.length > 0) {
req.messages = req.session.messages;
req.session.messages = [];
} else {
req.messages = [];
}
if (req.messages && req.messages.length > 0) {
res.partial("app/message", {messages: req.messages}, function (err, str) {
if (err) throw err;
req.message_text = str;
next();
});
} else {
next();
}
});
// dynamic nav
app.use(function (req, res, next) {
req.nav_group = "default";
next();
});
// dynamic helper functions accessible from views
app.dynamicHelpers({
flash_message: function (req, res) {
return function () {
return req.message_text;
}
},
get_nav_items: function (req, res) {
return function () {
var g = app.nav_groups[req.nav_group];
var n = g.length > 0 ? g : app.nav_groups.default;
app.nav_top_items.forEach(function (ni) {
var found = false;
n.forEach(function (existing_item) {
if (existing_item.url == ni.url) {
found = true;
}
});
if (!found) {
n.push(ni);
}
});
app.nav_bottom_items.forEach(function (ni) {
var found = false;
n.forEach(function (existing_item) {
if (existing_item.url == ni.url) {
found = true;
}
});
if (!found) {
n.push(ni);
}
});
return n;
}
},
get_url: function (req, res) {
return function () {
return req.originalUrl;
}
}
});
app.use(express.bodyParser());
app.use(express.methodOverride());
// auth
app.use(function(req, res, next){
req.is_user = false;
req.require_authentication = function () {
if (req.is_user == true) {
return true;
}
res.redirect('/admin/login?'+querystring.stringify({redirect_url: req.url}));
return false;
}
if (req.session.session_key) {
Settings = mongoose.model('Settings');
Settings.findOne({option: "app"}, function (err, s) {
if (!err && s) {
if (s.value.session_key && s.value.session_key == req.session.session_key) {
var correct_token = crypto.createHash('sha1').update(req.session.user_name+"|"+req.session.session_key).digest('hex');
if (req.session.session_token == correct_token) {
req.is_user = true;
s.value.last_activity = new Date();
s.commit("value");
s.save(function (err) {
// err?
});
}
}
}
next();
});
} else {
next();
}
});
// Example 500 page
app.error(function(err, req, res){
res.render('500',{error:err});
});
app.nav_groups = {
default: [
{url: "/dashboard", text: "Recent Posts"}
]
};
app.nav_top_items = [
{url: "/dashboard", text: "Dashboard"}
];
app.nav_bottom_items = [
{url: "/admin", text: "Admin"}
];
app.add_nav_item = function (g, nav_item) {
var found = false;
if (!app.nav_groups[g]) {
app.nav_groups[g] = [];
}
app.nav_groups[g].forEach(function (existing_item) {
if (existing_item.url == nav_item.url) {
found = true;
}
});
if (!found) {
app.nav_groups[g].push(nav_item);
}
}
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Models
mongoose.connect(conf.db);
fs.readdir(__dirname + '/models', function(err, files){
if (err) throw err;
files.forEach(function(file){
require(__dirname + '/models/'+ file)
});
// Routes
require('./controllers/index.js').init(app, function () {
// Example 404 page via simple Connect middleware
//app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.use(function(req, res){
res.render('404');
});
});
app.listen(conf.port, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
// every 5 seconds
cron('*/5 * * * * *', function () {
try {
var controller = require('./controllers/index');
controller._tick(app);
} catch (ex) {
console.log("Something really bad happened... ");
console.log(ex);
}
});
});
}, 'conf');