-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
executable file
·111 lines (106 loc) · 4.49 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
#!/usr/bin/env node
require('dotenv').config();
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const errorHandler = require('errorhandler');
const createError = require('http-errors');
const db = require('./lib/db');
const uploadHandler = require('./lib/upload-handler');
const env = process.env.NODE_ENV || 'development';
const staticDir = 'development' === env ? 'src' : 'public';
const apiUrlBase = '/api';
// Override unused remote-user
morgan.token('remote-user', req => (req.user ? req.user.username : ''));
const app = express();
app.set('port', process.env.PORT || 3000);
app.set('trust proxy', 'loopback');
app.use(morgan('combined'));
app.use(express.static(path.join(__dirname, staticDir), {maxAge: '15m'}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
if ('development' === env) {
app.use(errorHandler());
}
const routes = require('./routes')(app);
const apiApp = express();
routes.authentication(app, apiApp);
app.use(apiUrlBase, apiApp);
app.use(setProject);
app.get(
'/page-images/*',
function (req, res, next) {
req.url = req.url.replace('/page-images', '/page-images/' + req.project.code);
next();
},
express.static(path.join(__dirname, staticDir), {maxAge: '15m'})
);
apiApp.use(setProject);
apiApp.get('/project', routes.getProject);
apiApp.get('/search', routes.search);
apiApp.get('/line/:page/:line', routes.getLine);
apiApp.get('/line/:id', routes.getLine);
apiApp.put('/line/:id', routes.updateLine);
apiApp.get('/status', routes.status);
apiApp.post('/mark-blank/:page/:line', routes.markLineBlank);
apiApp.get('/completed.tsv', routes.completedTsv);
apiApp.get('/address-changes.tsv', routes.completedTsv);
apiApp.get('/response-voters.tsv', routes.responseVotersTsv);
apiApp.get('/response-address-changes.tsv', routes.responseAddressChangesTsv);
apiApp.get('/invoices.tsv', routes.invoicesTsv);
apiApp.get('/dt-line/:checker', routes.dtLine);
apiApp.get('/dt-line', routes.dtLine);
apiApp.get('/circulators', routes.getCirculators);
apiApp.get('/circulators/next', routes.getNextCirculatorNumber);
apiApp.post('/circulators', routes.createOrUpdateCirculator);
apiApp.get('/circulators/:id', routes.getCirculator);
apiApp.put('/circulators/:id', routes.createOrUpdateCirculator);
apiApp.delete('/circulators/:id', routes.deleteCirculator);
apiApp.get('/circulators/:id/ward-counts', routes.getLineCountsForCirculatorByWard);
apiApp.get('/circulators/:id/unpaid-pages', routes.getUnpaidPagesForCirculator);
apiApp.post('/notes', routes.createOrUpdateNote);
apiApp.get('/notes/:id', routes.getNote);
apiApp.get('/notes/:voter_id/:user_id', routes.getNote);
apiApp.put('/notes/:id', routes.createOrUpdateNote);
apiApp.delete('/notes/:id', routes.deleteNote);
apiApp.get('/pages/dt', routes.dtPages);
apiApp.post('/pages/images', uploadHandler.array('images'), routes.uploadPageImages);
apiApp.get('/pages', routes.getPages);
apiApp.post('/pages', routes.createOrUpdatePage);
apiApp.get('/pages/:number', routes.getPage);
apiApp.put('/pages/:number', routes.createOrUpdatePage);
apiApp.get('/invoices', routes.getInvoices);
apiApp.post('/invoices', routes.createOrUpdateInvoice);
apiApp.delete('/invoices/:number', routes.deleteInvoice);
apiApp.get('/invoices/invoice-:number.html', routes.htmlInvoices);
apiApp.get('/invoices/:number', routes.getInvoice);
apiApp.put('/invoices/:number', routes.createOrUpdateInvoice);
apiApp.get('/users', routes.getUsers);
apiApp.post('/users', routes.createOrUpdateUser);
apiApp.get('/users/usernames', routes.getUsernames);
apiApp.get('/users/:id', routes.getUser);
apiApp.put('/users/:id', routes.createOrUpdateUser);
apiApp.post('/users/:username/pages', routes.assignPages);
apiApp.get('/totals', routes.getTotals);
apiApp.get('/challenge.html', routes.challenge);
app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
function setProject(req, res, next) {
const m = req.url.match(/^\/([\w-]+)\//);
const projectCode = m && m[1];
if (!projectCode) {
return next();
}
return db.getProjectByCode(projectCode)
.then(function (project) {
if (project && req.user.projects.find(p => p.code === project.code)) {
req.project = project;
req.url = req.url.replace('/' + projectCode, '');
return next();
}
return next(createError(404, 'No such project'));
})
.catch(next);
}