forked from softwerkskammer/Agora
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
133 lines (116 loc) · 4.46 KB
/
app.js
File metadata and controls
133 lines (116 loc) · 4.46 KB
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
"use strict";
var express = require('express');
var http = require('http');
var path = require('path');
var passport = require('passport');
var MongoStore = require('connect-mongo')(express);
function useApp(parent, url, factory) {
function ensureRequestedUrlEndsWithSlash(req, res, next) {
if (!(/\/$/).test(req.url)) { return res.redirect(req.url + '/'); }
next();
}
var child = factory(express());
child.locals({pretty: true});
parent.get('/' + url, ensureRequestedUrlEndsWithSlash);
parent.use('/' + url + '/', child);
return child;
}
var conf = require('nconf');
// initialize winston and two concrete loggers
var winston = require('winston-config').fromFileSync(path.join(__dirname, 'config/winston-config.json'));
var appLogger = winston.loggers.get('application');
var httpLogger = winston.loggers.get('http');
var sessionStore = new MongoStore({
db: 'swk',
host: conf.get('mongoHost'),
port: parseInt(conf.get('mongoPort'), 10),
username: conf.get('mongoUser'),
password: conf.get('mongoPass')
});
// stream the log messages of express to winston, remove line breaks on message
var winstonStream = {
write: function (message) {
httpLogger.info(message.replace(/(\r\n|\n|\r)/gm, ""));
}
};
module.exports = {
create: function () {
var app = express();
app.configure(function () {
app.set('view engine', 'jade');
app.set('views', path.join(__dirname, 'views'));
app.use(express.favicon(path.join(__dirname, 'public/img/Softwerkskammer16x16.ico')));
app.use(express.logger({stream: winstonStream}));
app.use(express.cookieParser());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.compress());
app.use(express.static(path.join(__dirname, 'public')));
var sevenDays = 86400 * 1000 * 7;
if (conf.get('dontUsePersistentSessions')) {
// TODO: Umbau als CoolBean mit SessionStore als InMemoryStore von Express statt if Konstrukt (leider)
app.use(express.session({secret: conf.get('secret'), cookie: {maxAge: sevenDays}, store: null}));
} else {
app.use(express.session({secret: conf.get('secret'), cookie: {maxAge: sevenDays}, store: sessionStore}));
}
app.use(passport.initialize());
app.use(passport.session());
app.use(conf.get('beans').get('secureByLogin'));
app.use(conf.get('beans').get('secureAdminOnly'));
app.use(conf.get('beans').get('expressViewHelper'));
app.use(conf.get('beans').get('redirectRuleForNewUser'));
app.use(conf.get('beans').get('announcementsInSidebar'));
app.use(conf.get('beans').get('wikiSubdirs'));
app.use(app.router);
});
app.use('/', conf.get('beans').get('siteApp'));
useApp(app, 'administration', conf.get('beans').get('administrationApp'));
useApp(app, 'activities', conf.get('beans').get('activitiesApp'));
useApp(app, 'members', conf.get('beans').get('membersApp'));
useApp(app, 'groups', conf.get('beans').get('groupsApp'));
useApp(app, 'announcements', conf.get('beans').get('announcementsApp'));
useApp(app, 'mailsender', conf.get('beans').get('mailsenderApp'));
useApp(app, 'auth', conf.get('beans').get('authenticationApp'));
useApp(app, 'mailarchive', conf.get('beans').get('mailarchiveApp'));
useApp(app, 'wiki', conf.get('beans').get('wikiApp'));
app.configure('development', function () {
// Handle 404
app.use(function (req, res) {
appLogger.error('404 - requested url was ' + req.url);
res.render('errorPages/404.jade');
});
// Handle 500
app.use(function (error, req, res, next) {
appLogger.error(error.stack);
if (/InternalOpenIDError|BadRequestError|InternalOAuthError/.test(error.name)) {
return res.render('errorPages/authenticationError.jade', {error: error});
}
res.render('errorPages/500.jade', {error: error});
next; // needed for jshint
});
});
app.configure('production', function () {
//app.use(express.errorHandler());
});
return app;
},
start: function (done) {
var port = conf.get('port');
var app = this.create();
this.server = http.createServer(app);
this.server.listen(port, function () {
console.log('Server running at port ' + port);
if (done) {
done();
}
});
},
stop: function (done) {
this.server.close(function () {
console.log('Server stopped');
if (done) {
done();
}
});
}
};