forked from codeforgermany/click_that_hood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
112 lines (88 loc) · 3.25 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
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
var express = require('express'),
lessMiddleware = require('less-middleware'),
fs = require('fs'),
fsTools = require('fs-tools'),
config = require('config');
var startApp = function() {
var app = express();
app.use(express.compress());
app.use(lessMiddleware({
src: __dirname + '/public',
compress: (process.env.NODE_ENV == 'production'),
once: (process.env.NODE_ENV == 'production')
}));
// Redirect to environment-appropriate domain, if necessary
app.all('*', function(req, res, next) {
if (config.app_host_port != req.headers.host) {
var redirectUrl = 'http://' + config.app_host_port + req.url;
console.log('Redirecting to ' + redirectUrl + '...');
res.redirect(301, redirectUrl);
} else {
next('route');
}
});
app.use(express.static(__dirname + '/public'));
var port = process.env.PORT || 8000;
app.listen(port, null, null, function() {
console.log('Listening on port ' + port);
});
}
function normalizeCountryName(str) {
if (str.substr(0, 4) == 'The ') {
str = str.substr(4);
}
return str;
}
// Write combined metadata file from individual location metadata files
fsTools.findSorted('public/data', /[^.]+\.metadata.json/, function(err, files) {
var metadata = {};
var countryNames = ['U.S.'];
for (index in files) {
var metadataFilePath = files[index];
var locationName = metadataFilePath.match(/([^\/.]+)\.metadata.json/)[1];
// Exclude template file
if (locationName != "_TEMPLATE") {
// Flag error and exit if metadata is not found
if (!fs.existsSync(metadataFilePath)) {
console.error("Metadata file not found for '" + locationName +
"'. Aborting server start.");
process.exit(1);
}
metadata[locationName] =
JSON.parse(fs.readFileSync(metadataFilePath, 'utf8'));
// Combine a list of country names.
var countryName = metadata[locationName].countryName;
if (countryName && countryNames.indexOf(countryName) == -1) {
countryNames.push(countryName);
}
// Parse GeoJSON file, find the first available latitude/longitude,
// and add them to the metadata.
geoJsonFilePath = 'public/data/' + locationName + '.geojson';
if (!fs.existsSync(geoJsonFilePath)) {
console.error("GeoJSON file not found for '" + locationName +
"'. Aborting server start.");
process.exit(1);
}
var geoJsonData = JSON.parse(fs.readFileSync(geoJsonFilePath, 'utf8'));
var latLon = geoJsonData.features[0].geometry.coordinates[0][0];
if (latLon[0][0]) {
var lat = latLon[0][0];
var lon = latLon[0][1];
} else {
var lat = latLon[0];
var lon = latLon[1];
}
metadata[locationName].sampleLatLon = [lat, lon];
}
}
countryNames.sort(function(a, b) {
return (normalizeCountryName(a) > normalizeCountryName(b)) ? 1 : -1;
});
var metadataFileContents =
"//\n// This file is AUTO-GENERATED each time the " +
"application is restarted.\n//\n\n" +
"var CITY_DATA = " + JSON.stringify(metadata) + ";\n" +
"var COUNTRY_NAMES = " + JSON.stringify(countryNames) + ";\n";
fs.writeFileSync("public/js/data.js", metadataFileContents);
startApp();
});