-
Notifications
You must be signed in to change notification settings - Fork 72
/
server.js
116 lines (85 loc) · 3.47 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
const service = require('express')();
const bodyParser = require('body-parser');
const morgan = require('morgan');
const { ok, notFound, badRequest, dbErrorHandler } = require('./util');
const { connect } = require('./model/database');
//const { submissionsToZipStream } = require('./xml');
////////////////////////////////////////////////////////////////////////////////
// DATABASE SETUP
// initialize our top-level static database instance.
const db = connect();
// initialize our model objects.
const Form = require('./model/form')(db);
//const Submission = require('./model/submission')(db);
////////////////////////////////////////////////////////////////////////////////
// SERVICE SETUP
// for now, just take in plain-text bodies. easy to augment with other formats.
service.use(bodyParser.text({ type: '*/*' }));
// apache request commonlog.
service.use(morgan('common'));
////////////////////////////////////////////////////////////////////////////////
// SUBMISSIONS (nonstandard ODK API)
/*
// combined endpoint for POSTing any form submission.
service.post('/submissions', (request, response) => {
const submission = Submission.fromXml(request.body);
if (submission instanceof Error)
badRequest(response, submission);
else
submission.save().then(ok(response)).catch(dbErrorHandler(response));
});
*/
////////////////////////////////////////////////////////////////////////////////
// FORMS (via REST)
// saves a new form definition.
service.post('/forms', (request, response) => {
const [ form, error ] = Form.fromXml(request.body);
if (error != null)
badRequest(response, error);
else
form.create().then(ok(response)).catch(dbErrorHandler(response));
});
// returns a form definition.
service.get('/forms/:xmlFormId', (request, response) => {
Form.getByXmlFormId(request.params.xmlFormId).then((form) => {
if (form == null)
notFound(response);
else
ok(response, form);
}).catch(dbErrorHandler(response));
});
/*
////////////////////////////////////////////////////////////////////////////////
// SUBMISSIONS (via REST, subresource of forms)
// get all submissions for any form.
service.get('/forms/:formId/submissions', (request, response) => {
Submission.listByFormId(request.params.formId)
.then(ok(response)).catch(dbErrorHandler(response));
});
// get a single submission for a single form.
service.get('/forms/:formId/submissions/:instanceId', (request, response) => {
Submission.getSingle(request.params.formId, request.params.instanceId)
.then(ok(response)).catch(dbErrorHandler(response));
});
// get all submissions for any form in ZIP format containing joinable CSVs.
service.get('/forms/:formId/submissions.csv.zip', async (request, response) => {
const formId = request.params.formId;
const template = await Form.getByXmlFormId(formId);
if (template == null) return notFound(response);
Submission.queryByFormId(formId).stream((stream) => {
response.append('Content-Disposition', `attachment; filename="${formId}.csv.zip"`);
submissionsToZipStream(formId, stream, template).pipe(response);
});
});
*/
////////////////////////////////////////////////////////////////////////////////
// PROCESS SETUP
// start the service.
service.listen(8383, () => {
// notify parent process we are alive if applicable.
if (process.send != null) process.send('online');
});
process.on('message', (message) => { // parent process.
if (message === 'shutdown') process.exit(0);
// TODO: do we need to cleanup database?
});