-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
81 lines (61 loc) · 2.21 KB
/
app.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
/**
* Module dependencies.
*/
var express = require('express');
var http = require('http');
var path = require('path');
var timers = require("timers");
var gzippo = require("gzippo");
var crawler = require(path.join(__dirname, "crawler/vehicles/vehicle-crawler.js"));
var gasstation_crawler = require(path.join(__dirname, "crawler/gasstations/gasstations-crawler.js"));
var site = require(path.join(__dirname, "routes/site.js"));
var vehicles_api = require(path.join(__dirname, "routes/vehicles.js"));
var gasstations_api = require(path.join(__dirname, "routes/gasstations.js"));
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(gzippo.staticGzip(path.join(__dirname, 'public')));
app.use(gzippo.compress());
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
// development only
var crawler_interval = 3 * 60000;
if (typeof process.env.UPDATE_INTERVAL !== "undefined") {
crawler_interval = parseFloat(process.env.UPDATE_INTERVAL) * 60000;
}
console.info("Setting crawler interval to " + (crawler_interval / 1000) + " seconds");
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
// Crawwwwl.
var crawl_vehicles = function() {
crawler.crawl(function(new_vehicles) {
vehicles_api.vehicles(new_vehicles);
});
};
timers.setInterval(crawl_vehicles, crawler_interval);
crawl_vehicles();
var crawl_gasstations = function() {
gasstation_crawler.crawl(function(new_gasstations) {
gasstations_api.gasstations(new_gasstations);
});
};
timers.setInterval(crawl_gasstations, crawler_interval);
crawl_gasstations();
// API
app.get('/', site.index);
app.get('/vehicles', vehicles_api.all_vehicles);
app.get('/vehicles/:city', vehicles_api.vehicles_in_city);
app.get('/nearest', vehicles_api.nearest_vehicle);
app.get('/gasstations', gasstations_api.all_gasstations);
app.get('/gasstations/:city', gasstations_api.gasstations_in_city);
// --------
http.createServer(app).listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});