This repository has been archived by the owner on Feb 2, 2018. It is now read-only.
/
server.js
119 lines (101 loc) · 3.65 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
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
var errors = require('errors');
var express = require('express');
var i18n = require('i18n-abide');
var nunjucks = require('nunjucks');
var passport = require('passport');
var path = require('path');
var sessions = require("client-sessions");
require('./httperrors');
var auth = require('./auth');
var config = require('./config');
var errorHandler = require('./middleware/errorHandler');
var notices = require('./notices');
var payment = require('./payments');
var products = require('./products');
var sellers = require('./sellers');
var state = require('./state');
var styleguide = require('./styleguide');
var trans = require('./trans');
var z = require('./zutil');
var templatePaths = [
path.join(z.rootPath, 'templates/payments/'),
path.join(z.rootPath, 'templates/styleguide/'),
path.join(z.rootPath, 'templates/docs/'),
path.join(z.rootPath, 'templates/'),
];
function createApp(options) {
// Setup Config with overrides.
config.addOverrides(options.configEnv);
var app = express();
app.set('name', 'zippy');
if (!options.options || !options.options.noAuth) {
// OAuthentication using passport with the token strategy (a.k.a. 0-legged).
app.use(passport.initialize());
passport.use('token', auth.tokenStrategy());
app.use(function authenticate() {
return function (req, res, next) {
passport.authenticate('token', { session: false }, function(err, user, info) {
var urlPath = req.url.toString().split('?')[0];
for (var path in config.unprotectedUrls) {
if (config.unprotectedUrls[path].test(urlPath)) {
return next();
}
}
if (!user.authenticated) {
var errorMessage = 'Authentication failed: ' + JSON.stringify(info);
throw new errors.InvalidCredentialsError(errorMessage);
}
next();
})(req, res, next);
};
}());
}
var env = new nunjucks.Environment(new nunjucks.FileSystemLoader(templatePaths),
{ autoescape: true });
env.express(app);
app.use(i18n.abide({
/*jshint camelcase: false */
supported_languages: config.supportedLocales,
default_lang: 'en-US',
debug_lang: 'it-CH',
locale_on_url: true
}));
app.use(sessions({
cookieName: 'zippySession', // This becomes request.zippySession.
secret: config.sessionSecret,
duration: config.sessionDuration,
activeDuration: config.sessionActiveDuration,
}));
// Parse JSON or urlencoded POST data but not file uploads.
app.use(express.json());
app.use(express.urlencoded());
app.post('/notices', notices.verify);
app.get('/products/:uuid', products.retrieve);
app.post('/products', products.create);
app.post('/sellers', sellers.create);
app.get('/sellers/:uuid', sellers.retrieve);
app.put('/sellers/:uuid', sellers.update);
app.del('/sellers/:uuid', sellers.delete);
app.get('/terms/:uuid', sellers.terms);
app.get('/styleguide/:doc', styleguide.retrieve);
app.get('/styleguide', styleguide.retrieve);
app.post('/transactions', trans.create);
app.get('/status', state.retrieve);
// Payment screens.
app.get('/', payment.start);
app.post('/payment/process', payment.processPayment);
app.get('/payment/confirm', payment.confirmPayment);
app.get('/payment/card', payment.creditCard);
// Static Resources.
app.get(/\/(?:css|fonts|images|js)\/?.*/, express.static('./media'));
// 404 Catch-all.
app.use(function(req, res, next) {
return next(new errors.NotFoundError("Sorry! Zippy can't find this please check the URL"));
});
// Error handling middleware.
app.use(errorHandler());
return app;
}
module.exports = {
createApp: createApp
};