-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
executable file
·83 lines (70 loc) · 2 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
var url = require('url');
var express = require('express');
var cors = require('cors');
var config = require('histograph-config');
var schemas = require('histograph-schemas');
var io = require('histograph-io');
var stats = require('histograph-stats');
var app = express();
var query = require('./lib/query');
var jsonld = require('./lib/jsonld');
var geojson = require('./lib/geojson');
var params = require('./lib/params');
var package = require('./package');
app.use(cors());
// Mount Histograph IO
app.use('/', io);
// Mount Histograph Stats
app.use('/stats', stats);
var ontology;
schemas.ontology(function(err, results) {
ontology = results;
});
var exampleUrls = config.api.exampleUrls || [];
function formatError(err) {
if (err && err.message && err.message.indexOf('IndexMissingException') === 0) {
var match = err.message.match(/\[\[(.*?)\]/);
if (match) {
return 'Dataset not found: ' + match[1];
}
}
return err;
}
app.get('/', function(req, res) {
res.send({
name: 'Histograph API',
version: package.version,
message: 'Histograph - Historical Geocoder',
docs: 'http://histograph.io/',
examples: exampleUrls.map(function(query) {
return url.resolve(config.api.baseUrl, query);
})
});
});
app.get('/ontology', function(req, res) {
res.set('Content-Type', 'text/turtle');
res.send(ontology);
});
app.get('/schemas/:schema(pits|relations)', function(req, res) {
res.send(schemas[req.params.schema]);
});
app.get('/search',
params.preprocess,
params.check,
function(req, res) {
query(req.processedQuery, function(err, results) {
if (err) {
res.status(err.status || 400).send({
message: formatError(err)
});
} else {
results = jsonld(geojson(results, req.processedQuery), req.processedQuery);
res.send(results);
}
});
}
);
app.listen(config.api.bindPort, function() {
console.log(config.logo.join('\n'));
console.log('Histograph API listening at port ' + config.api.bindPort);
});