forked from mozilla/openbadges-backpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
196 lines (171 loc) · 6.5 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
// Newrelic *must* be the first module loaded. Do not move this require module!
// docs, https://npmjs.org/package/newrelic
if ( process.env.NEW_RELIC_HOME ) {
require( 'newrelic' );
}
// Configure & start express.
var express = require('express');
var http = require('http');
var fs = require('fs');
var path = require('path');
var middleware = require('./middleware');
var logger = require('./lib/logger');
var browserid = require('./lib/browserid');
var configuration = require('./lib/configuration');
var flash = require('connect-flash');
var nunjucks = require('nunjucks');
var _ = require('underscore');
var app = express();
app.logger = logger;
app.config = configuration;
/* Default values for template variables */
app.locals({
error: [],
success: [],
getBrowserIdScriptUrl: function() {
return browserid.getIncludeScriptUrl();
}
});
app.set('useCompiledTemplates', configuration.get('nunjucks_precompiled'));
// default view engine
var env = new nunjucks.Environment(new nunjucks.FileSystemLoader(__dirname + '/views'));
env.express(app);
env.addFilter('formatdate', function (rawDate) {
if (parseInt(rawDate, 10) == rawDate) {
var date = new Date(rawDate * 1000);
return date.toString();
}
return rawDate;
});
// Middleware. Also see `middleware.js`
// ------------------------------------
app.use(middleware.less(app.get('env')));
app.use(express.static(path.join(__dirname, "static")));
app.use("/views", express.static(path.join(__dirname, "views")));
app.use(middleware.staticTemplateViews(env, 'static/'));
app.use(middleware.noFrame({ whitelist: [ '/issuer/frame.*', '/', '/share/.*' ] }));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.methodOverride());
app.use(middleware.logRequests());
app.use(middleware.cookieSessions());
app.use(middleware.userFromSession());
app.use(flash());
app.use(middleware.csrf({
whitelist: [
'/backpack/authenticate',
'/displayer/convert/.+',
'/issuer/frameless.*',
'/api/.+'
]
}));
app.use(middleware.cors({ whitelist: ['/_badges.*', '/issuer.*', '/baker', '/displayer/.+/group.*'] }));
app.use(middleware.statsdRequests());
app.use(app.router);
app.use(middleware.notFound());
app.configure('development', function () {
var gitUtil = require('./lib/git-util');
try {
var sha = gitUtil.findSHA();
app.set('sha', sha);
}
catch (ex) {
logger.warn(ex);
}
browserid.configure({testUser: process.env['BROWSERID_TEST_USER']});
});
app.use(express.errorHandler());
// Routes
// ------
const baker = require('./controllers/baker');
const badge = require('./controllers/badge');
const issuer = require('./controllers/issuer');
const displayer = require('./controllers/displayer');
const demo = require('./controllers/demo');
const backpack = require('./controllers/backpack');
const group = require('./controllers/group');
const share = require('./controllers/share');
const BackpackConnect = require('./controllers/backpack-connect');
const backpackConnect = new BackpackConnect({
apiRoot: '/api',
realm: 'openbadges'
});
// Parameter handlers
app.param('badgeId', badge.findById);
app.param('apiUserId', displayer.findUserById);
app.param('apiGroupId', displayer.findGroupById);
app.param('groupId', group.findById);
app.param('groupUrl', share.findGroupByUrl);
app.param('badgeUrl', badge.findByUrl);
app.param('badgeHash', badge.findByHash);
app.get('/baker', baker.baker);
app.get('/issuer.js', issuer.generateScript);
app.get('/issuer/frame', issuer.frame);
app.post('/issuer/frameless', issuer.frameless);
app.get('/issuer/assertion', issuer.issuerBadgeAddFromAssertion);
app.post('/issuer/assertion', issuer.issuerBadgeAddFromAssertion);
app.get('/issuer/welcome', issuer.welcome);
app.get('/displayer/convert/email', displayer.emailToUserIdView);
app.post('/displayer/convert/email', displayer.emailToUserId);
app.get('/displayer/:apiUserId/groups.:format?', displayer.userGroups);
app.get('/displayer/:apiUserId/group/:apiGroupId.:format?', displayer.userGroupBadges);
app.get('/demo', demo.issuer);
app.get('/demo/ballertime', demo.massAward);
app.get('/demo/badge.json', demo.demoBadge);
app.get('/demo/invalid.json', demo.badBadge);
app.post('/demo/award', demo.award);
app.get('/', backpack.recentBadges);
app.get('/backpack', backpack.manage);
app.get('/backpack/badges', backpack.allBadges);
app.get('/backpack/add', backpack.addBadge);
app.get('/backpack/login', backpack.login);
app.get('/backpack/signout', backpack.signout);
app.post('/backpack/badge', backpack.userBadgeUpload);
app.post('/backpack/authenticate', backpack.authenticate);
app.get('/backpack/settings', backpack.settings());
app.post('/backpack/settings/revoke-origin', backpackConnect.revokeOrigin());
app.get('/stats', backpack.stats);
app.get('/backpack/badge/:badgeId', backpack.details);
app.delete('/backpack/badge/:badgeId', backpack.deleteBadge);
app.delete('/badge/:badgeId', badge.destroy);
app.post('/group', group.create);
app.put('/group/:groupId', group.update);
app.delete('/group/:groupId', group.destroy);
app.get('/images/badge/:badgeHash.:badgeFileType', badge.image);
app.post('/share/badge/:badgeId', badge.share);
app.get('/share/badge/:badgeUrl', badge.show);
app.get('/share/:groupUrl/edit', share.editor);
app.post('/share/:groupUrl', share.createOrUpdate);
app.put('/share/:groupUrl', share.createOrUpdate);
app.get('/share/:groupUrl', share.show);
app.get('/access', backpackConnect.requestAccess());
app.post('/accept', backpackConnect.allowAccess());
app.all('/api/*', backpackConnect.allowCors());
app.post('/api/token', backpackConnect.refresh());
app.post('/api/issue', backpackConnect.authorize("issue"),
issuer.issuerBadgeAddFromAssertion);
app.get('/api/identity', backpackConnect.authorize("issue"),
backpackConnect.hashIdentity());
if (!module.parent) {
var start_server = function start_server(app) {
var port = app.config.get('port');
var pid = process.pid.toString();
var pidfile = path.join(app.config.get('var_path'), 'server.pid');
app.listen(port);
app.logger.info('environment: %s', process.env['NODE_ENV']);
app.logger.info('opening server on port: %s', port);
app.logger.info('READY PLAYER ONE');
fs.unlink(pidfile, function () {
fs.writeFile(pidfile, pid, function (err) {
if (err) throw Error('could not make pidfile: ' + err);
});
});
process.on('SIGTERM', function () {
app.logger.info('recieved SIGTERM, exiting');
process.exit();
});
};
start_server(app);
} else {
module.exports = http.createServer(app);
}