forked from datagouv/api-geo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
156 lines (133 loc) · 4.3 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
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
const express = require('express');
const cors = require('cors');
const morgan = require('morgan');
const { initCommuneFields, initCommuneFormat } = require('./lib/communeHelpers');
const { initDepartementFields } = require('./lib/departementHelpers');
const { initRegionFields } = require('./lib/regionHelpers');
const { formatOne } = require('./lib/helpers');
const dbCommunes = require('./lib/communes').getIndexedDb();
const dbDepartements = require('./lib/departements').getIndexedDb();
const dbRegions = require('./lib/regions').getIndexedDb();
const { pick } = require('lodash');
const app = express();
app.use(cors());
app.use(morgan('dev'));
// Inject databases references
app.use((req, res, next) => {
req.db = {
communes: dbCommunes,
departements: dbDepartements,
regions: dbRegions,
};
next();
});
/* Communes */
app.get('/communes', initCommuneFields, initCommuneFormat, function (req, res) {
let result;
const query = pick(req.query, 'code', 'codePostal', 'nom', 'codeDepartement', 'codeRegion', 'boost');
if (req.query.lat && req.query.lon) {
const lat = parseFloat(req.query.lat);
const lon = parseFloat(req.query.lon);
if (Number.isFinite(lat)
&& lat >= -90
&& lat <= 90
&& Number.isFinite(lon)
&& lon >= -180
&& lon <= 180
) {
query.pointInContour = [lon, lat];
}
}
if (query.nom) req.fields.add('_score');
if (Object.keys(query).length === 0) {
return res.sendStatus(400);
}
result = dbCommunes.search(query);
if (req.outputFormat === 'geojson') {
res.send({
type: 'FeatureCollection',
features: result.map(commune => formatOne(req, commune)),
});
} else {
res.send(result.map(commune => formatOne(req, commune)));
}
});
app.get('/communes/:code', initCommuneFields, initCommuneFormat, function (req, res) {
const communes = dbCommunes.search({ code: req.params.code });
if (communes.length === 0) {
res.sendStatus(404);
} else {
res.send(formatOne(req, communes[0]));
}
});
/* Départements */
app.get('/departements', initDepartementFields, function (req, res) {
const query = pick(req.query, 'code', 'nom', 'codeRegion');
if (query.nom) req.fields.add('_score');
res.send(
dbDepartements
.search(query)
.map(departement => formatOne(req, departement))
);
});
app.get('/departements/:code', initDepartementFields, function (req, res) {
const departements = dbDepartements.search({ code: req.params.code });
if (departements.length === 0) {
res.sendStatus(404);
} else {
res.send(formatOne(req, departements[0]));
}
});
app.get('/departements/:code/communes', initCommuneFields, initCommuneFormat, function (req, res) {
const departements = dbDepartements.search({ code: req.params.code });
if (departements.length === 0) {
res.sendStatus(404);
} else {
const communes = dbCommunes.search({ codeDepartement: req.params.code });
if (req.outputFormat === 'geojson') {
res.send({
type: 'FeatureCollection',
features: communes.map(commune => formatOne(req, commune)),
});
} else {
res.send(communes.map(commune => formatOne(req, commune)));
}
}
});
/* Régions */
app.get('/regions', initRegionFields, function (req, res) {
const query = pick(req.query, 'code', 'nom');
if (query.nom) req.fields.add('_score');
res.send(
dbRegions
.search(query)
.map(region => formatOne(req, region))
);
});
app.get('/regions/:code', initRegionFields, function (req, res) {
const regions = dbRegions.search({ code: req.params.code });
if (regions.length === 0) {
res.sendStatus(404);
} else {
res.send(formatOne(req, regions[0]));
}
});
app.get('/regions/:code/departements', initDepartementFields, function (req, res) {
const regions = dbRegions.search({ code: req.params.code });
if (regions.length === 0) {
res.sendStatus(404);
} else {
const departements = dbDepartements.search({ codeRegion: req.params.code });
res.send(departements.map(commune => formatOne(req, commune)));
}
});
/* Definition */
app.get('/definition.yml', function (req, res) {
res.sendFile(__dirname + '/definition.yml');
});
const port = process.env.PORT || 5000;
app.listen(port, () => {
/* eslint no-console: 0 */
console.log('Start listening on port %d', port);
});
module.exports = app;