-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublisher.js
More file actions
95 lines (75 loc) · 2.3 KB
/
publisher.js
File metadata and controls
95 lines (75 loc) · 2.3 KB
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
var express = require("express");
var logfmt = require("logfmt");
var pg = require('pg');
var connString = process.env.DATABASE_URL || 'postgres://localhost:5432/cmpd_feed';
var app = express();
app.use(logfmt.requestLogger());
// Basic JSON
app.get('/', function(req, res) {
var data = {
"publisher": "CMPD",
"events": []
};
var client = new pg.Client(connString);
client.connect();
client.query("select message, lat, lon from events where creation > now() - interval '24' hour", function(err, result) {
// Loop through different accidents/obstructions in CMPD's georss feed.
result.rows.forEach(function(item) {
var event = {};
event.message = item.message;
event.latitude = parseFloat(item.lat);
event.longitude = parseFloat(item.lon);
data.events.push(event);
});
// Build feed!
res.set('Content-Type', 'application/json');
res.send(data);
client.end();
});
});
// GeoJSON
app.get('/geojson', function(req, res) {
var data = {
"type": "FeatureCollection",
"features": []
};
/* Single feature example
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [102.0, 0.5]
},
"properties": {
"prop0": "value0"
}
}
*/
var client = new pg.Client(connString);
client.connect();
client.query("select * from events where creation > now() - interval '24' hour", function(err, result) {
result.rows.forEach(function(item) {
var feature = {
"type": "Feature",
"id": item.event_no,
"geometry": {
"type": "Point",
"coordinates": [parseFloat(item.lon), parseFloat(item.lat)]
},
"properties": {
"title": item.message,
"timestamp": item.creation
}
};
data.features.push(feature);
});
// Build feed!
res.set('Content-Type', 'application/json');
res.send(data);
client.end();
});
});
var port = Number(process.env.PORT || 5000);
app.listen(port, function() {
console.log("Listening on " + port);
});