-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
91 lines (76 loc) · 2.39 KB
/
server.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
// Daisy Central server code
var express = require('express'),
ss = require('socketstream'),
conf = require('./server/app/conf'),
daisyServer = require('./server/app/daisy-server.js').init(ss).start(),
mongoose = require('mongoose'),
mongooseAuth = require('mongoose-auth'),
security = require('./server/middleware/security'),
UserModel = require('./server/models/user').User;
// connect to mongodb
mongoose.connect(conf.db.url);
mongoose.connection.on('open', function () {
console.log("mongodb connection established");
});
/* _________________ VIEWS ________________ */
// the main webapp
ss.client.define('main', {
view: 'app.jade',
css: ['libs', 'app.styl'],
code: ['libs', 'app', 'shared', 'system'],
tmpl: '*'
});
// the login page
ss.client.define('login', {
view: 'login.jade',
css: ['libs', 'app.styl', 'login.styl'],
code: ['libs', 'app', 'system', 'shared'],
tmpl: '*'
});
// the admin page
ss.client.define('admin', {
view: 'admin.jade',
css: ['libs', 'app.styl'],
code: ['libs', 'admin', 'shared', 'system'],
tmpl: '*'
});
ss.client.formatters.add(require('ss-stylus'));
ss.client.formatters.add(require('ss-jade'));
ss.client.templateEngine.use(require('ss-hogan'));
// use redis for sticky sessions
ss.session.store.use('redis');
ss.publish.transport.use('redis');
// minimize and pack assets if you type: SS_ENV=production node app.js
if (ss.env == 'production') { ss.client.packAssets(); }
// express cookieParser + session added by ss.http.middleware
var app = express.createServer(
express.bodyParser(),
ss.http.middleware,
express.staticCache(),
express.static(__dirname + "/client/static"),
mongooseAuth.middleware()
);
/* _________________ ROUTES _________________ */
app.get('/', security.authenticated, function (req, res) {
res.serveClient('main');
});
app.get('/login', security.validCookie, function (req, res) {
res.serveClient('login');
});
app.get('/logout', function (req, res) {
req.session.userId = undefined;
req.session.destroy();
req.logout();
res.redirect('/login');
});
app.get('/admin', security.isAdmin, function (req, res, next) {
res.serveClient("admin");
});
// start socketstream webapp
app.listen(conf.webserver.port, function () {
console.log("daisycentral listening on port %d in %s mode", app.address().port, app.settings.env);
});
ss.start(app);
process.on('uncaughtException', function (err) {
console.log(err);
});