forked from ryanj/restify-postGIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
172 lines (144 loc) · 4.95 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
var config = require('config'),
restify = require('restify'),
fs = require('fs'),
moment = require('moment'),
db = require('./bin/db.js'),
scraper = require('./bin/scraper.js'),
pjson = require('./package.json'),
doT = require('dot');
// set Czech locale for Moment.js
moment.locale('cs');
var app = restify.createServer();
app.use(restify.queryParser());
app.use(restify.bodyParser());
app.use(restify.CORS());
app.use(restify.fullResponse());
app.use(restify.gzipResponse());
// evaluate templates
var
index = doT.template(fs.readFileSync(__dirname + '/templates/index.html').toString()),
scrape_status = doT.template(fs.readFileSync(__dirname + '/templates/scrape_status.html').toString());
// Routes
///--- API calls
app.get('/container', db.getContainers);
app.get('/container/update', function (req, res, next) {
// forcibly run scraper update
scraper.scrape();
res.send({status: 'ok'});
return next();
});
app.get('/place', db.getPlaces);
app.put('/place/:id', function (req, res, next) {
db.checkPlaceInDistrict(req.params.id, req.params.lat, req.params.lng, function(err) {
if (err) {
if (err.name == 'PlaceNotFound') {
return next(new restify.ResourceNotFoundError(err.message));
} else if (err.name == 'InvalidPlacement') {
return next(new restify.InvalidContentError(err.message));
} else {
return next(err);
}
}
// place is located in the district (or district is not set)
db.locatePlace(req.params.id, req.params.lat, req.params.lng, function(err) {
next.ifError(err);
res.send({id: req.params.id, lat: req.params.lat, lng: req.params.lng});
return next();
});
});
});
app.get('/status', function (req, res, next)
{
res.send({status: 'ok'});
return next();
});
app.get('/status/scrape', function (req, res, next) {
db.getScrapeStatus(function(err, rows) {
if (err) {
console.error('Cannot get scrape status from DB!', err);
return next(err);
}
// extract and format DB data
rows.forEach(function(record) {
var scrape_result;
record.suc_time_from_str = (record.suc_time_from) ? moment(record.suc_time_from).calendar() : '';
record.suc_duration_str = (record.suc_time_from && record.suc_time_to) ? moment(record.suc_time_to).from(record.suc_time_from, true) : '';
record.err_time_from_str = (record.err_time_from) ? moment(record.err_time_from).calendar() : '';
record.err_duration_str = (record.err_time_from && record.err_time_to) ? moment(record.err_time_to).from(record.err_time_from, true) : '';
// parse success message
if (record.suc_message) {
try {
scrape_result = JSON.parse(record.suc_message);
record.places_parsed = scrape_result.places.parsed;
record.places_inserted = scrape_result.places.inserted;
record.containers_parsed = scrape_result.containers.parsed;
record.containers_inserted = scrape_result.containers.inserted;
}
// ignore parse errors
catch(err) {}
}
});
res.status(200);
res.header('Content-Type', 'text/html');
res.end(scrape_status(rows));
});
});
app.get('/district/:id', function (req, res, next) {
db.findDistrict(req.params.id, function(err, district) {
if (err) {
return next(new Error(err.message));
}
if (!district) {
res.send(404, new Error('District with id '+req.params.id+' not found!'));
return next(false);
}
res.send(district.json);
return next();
});
});
///--- Static content
app.use(function(req, res, next) {
res.etag = pjson.version;
next();
});
app.use(restify.conditionalRequest());
app.get('/', function (req, res, next)
{
var debug = 'debug' in req.params;
res.status(200);
res.header('Content-Type', 'text/html');
res.header('Content-Language', 'cs');
res.end(index({version: pjson.version, debug: debug}));
return next();
});
app.get(/\/(css|js|img|lib|test)\/?.*/, restify.serveStatic({directory: './static/'}));
app.get('/favicon.ico', function(req, res, next) {
fs.readFile(__dirname + '/static/img/favicon.ico', function(err, file) {
if (err) {
res.send(500);
return next();
}
res.write(file);
res.end();
return next();
});
});
app.get('/robots.txt', function (req, res, next)
{
res.status(200);
res.header('Content-Type', 'text/plain');
res.end(doT.template(fs.readFileSync(__dirname + '/templates/robots.txt').toString())({siteUrl: config.siteUrl}));
return next();
});
app.get('/sitemap.xml', function (req, res, next)
{
res.status(200);
res.header('Content-Type', 'application/xml');
res.end(doT.template(fs.readFileSync(__dirname + '/templates/sitemap.xml').toString())({siteUrl: config.siteUrl}));
return next();
});
app.listen(config.port, config.ip, function () {
console.info( "Listening on " + config.ip + ", port " + config.port );
});
///--- Scrapers
scraper.init();