forked from posm/OpenMapKitServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (72 loc) · 2.37 KB
/
index.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
'use strict';
var settings;
try {
settings = require('./settings');
} catch (e) {
console.error("You must have a settings.js file. Take a look at settings.js.example. https://github.com/AmericanRedCross/OpenMapKitServer/blob/master/settings.js.example");
process.exit();
}
var express = require('express');
var bodyParser = require('body-parser');
var directory = require('serve-index');
var cors = require('cors');
var odkOpenRosa = require('./api/odk/odk-openrosa-routes');
var odkAggregate = require('./api/odk/odk-aggregate-routes');
var deployments = require('./api/deployments/deployment-routes');
var error = require('./api/odk/controllers/error-handler');
var auth = require('./util/auth');
var pkg = require('./package');
var app = express();
// Enable CORS always.
app.use(cors());
// Body parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Basic Info
app.get('/', redirectToForms);
app.get('/omk', redirectToForms);
app.get('/omk/info', info);
// Open Data Kit OpenRosa
// It's better to stay on top level of routes to
// prevent the user from having to add a prefix in ODK Collect
// server path.
app.use('/', odkOpenRosa);
/**
* Authentication routes.
*
* Note that OpenRosa routes pass through without auth.
* We can't lock down /omk/data/forms route, because that
* breaks /formList
*/
app.use('/omk/odk', auth);
app.use('/omk/data/submissions', auth);
app.use('/omk/pages', auth);
// Open Data Kit Aggregate
// These are endpoints that are used by iD and other pages.
// They are used to aggregate ODK and OSM data, and they
// do not need to be OpenRosa spec'ed like the endpoints
// interacted with in ODK Collect.
app.use('/omk/odk', odkAggregate);
// Deployments
app.use('/omk/deployments', deployments);
// Public Data & Static Assets
app.use('/omk/data', express.static(settings.dataDir));
app.use('/omk/data', directory(settings.dataDir));
app.use('/omk/pages', express.static(settings.pagesDir));
app.use('/omk/pages', directory(settings.pagesDir));
// Handle errors
app.use(error);
module.exports = app;
function info(req, res) {
res.status(200).json({
name: settings.name,
description: settings.description,
status: 200,
service: 'omk-server',
npm: pkg.name,
version: pkg.version
});
}
function redirectToForms(req, res, next) {
res.redirect('/omk/pages/forms');
}