-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
199 lines (169 loc) · 6.94 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// disable eslint es6 specific rules because we're using Node@6.x
/* eslint-disable no-var */
/* eslint-disable prefer-arrow-callback */
/* eslint-disable func-names */
/* eslint-disable prefer-template */
/* eslint-disable no-console */
var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');
var request = require('superagent');
var morgan = require('morgan');
var path = require('path');
var enforce = require('express-sslify');
var workerFarm = require('worker-farm');
// mailchimp API settings are stored in the .env file
var mailchimpAPIKey = process.env.MAILCHIMP_API_KEY;
var mailchimpServerInstance = mailchimpAPIKey.split('-')[1];
var mailchimpListID = process.env.MAILCHIMP_LIST_ID;
// mailchimp API url
var url = `https://${mailchimpServerInstance}.api.mailchimp.com/3.0/lists/${mailchimpListID}/members/`;
var app = express();
// load ecg-path-routing module as a worker, exposing its two methods
var workers = workerFarm(require.resolve('./ecg-path-routing'), ['findNearest', 'findRoute']);
// enforces HTTPS connections on any incoming GET and HEAD requests
if (process.env.NODE_ENV === 'production') {
app.use(enforce.HTTPS({ trustProtoHeader: true }));
}
// setup logging
app.use(morgan(':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] :response-time ms'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
function makePostRequest(strEmail, res) {
request.post(url)
.set('Content-Type', 'application/json;charset=utf-8')
.set('Authorization', 'Basic ' + new Buffer('any:' + mailchimpAPIKey).toString('base64'))
.send({
email_address: strEmail,
status: 'subscribed',
})
.end(function(error, response) {
if (response.status < 300 || (response.status === 400 && response.body.title === 'Member Exists')) {
res.send('Signed Up!');
} else {
res.status(500).send('Sign Up Failed :(', error);
}
});
}
// Serve static assets
app.use(express.static(path.resolve(__dirname, 'client/dist')));
// routing: given a latlng, find the neraest section of the path to that latlng and return details about it
// params:
// lat Latitude of the desired location.
// lng Longitude of the desired location.
// returns:
// JSON-encoded object of the closest trail segment.
// Attributes:
// id The ID# of that segment of the trail, e.g. 12345
// title The display name of that segment of the trail, e.g. "Smith Loop Road"
// wanted_lat The requested latlng location; latitude ordinate.
// wanted_lng The requested latlng location; longitude ordinate.
// closest_lat The closest point on the trail to the desired latlng; latitude ordinate.
// closest_lng The closest point on the trail to the desired latlng; longitude ordinate.
// closest_distance Distance in meters between the wanted latlng and the closest latlng.
// e The bounding box of that segment; east ordinate.
// w The bounding box of that segment; west ordinate.
// s The bounding box of that segment; south ordinate.
// n The bounding box of that segment; north ordinate.
// examples:
// http://localhost:5001/route/nearestpoint/?lat=42.3601&lng=-71.0589
// http://localhost:5001/route/nearestpoint/?lat=41.8240&lng=-71.4128
app.get('/route/nearestpoint/', (req, res) => {
const lat = parseFloat(req.query.lat);
const lng = parseFloat(req.query.lng);
const options = {
trailonly: true
};
if ( isNaN(lat) || isNaN(lng) ) {
res.json({ message: "Missing required params" });
return;
}
function success (segment) {
res
.set('Content-Type', 'application/json')
.json(segment);
}
function failure (errmsg) {
res
.set('Content-Type', 'text/plain')
.send(errmsg);
}
function callback (error, data) {
if (error) return failure(error);
return success(data);
}
workers.findNearest(lat, lng, options, callback);
});
// routing: given 2 latlngs, find the nearest point-on-route same as nearestpoint, then find a route between them
// params:
// slat Latitude of the desired starting location.
// slng Longitude of the desired starting location.
// tlat Latitude of the desired ending/target location.
// tlng Longitude of the desired ending/target location.
// returns:
// JSON-encoded object of the closest trail segment.
// Attributes:
// GDA TODO
// examples:
// http://localhost:5001/route/directions/?slat=42.3601&slng=-71.0589&tlat=41.8240&tlng=-71.4128
// http://localhost:5001/route/directions/?slat=24.5646034&slng=-81.8152815&tlat=45.1783131&tlng=-67.2807404
// http://localhost:5001/route/directions/?slat=24.5646034&slng=-81.8152815&tlat=31.3702&tlng=-81.4340
// http://localhost:5001/route/directions/?slat=32.0835&slng=-81.0998&tlat=45.1783&tlng=-67.2807
app.get('/route/directions/', (req, res) => {
const start_lat = parseFloat(req.query.slat);
const start_lng = parseFloat(req.query.slng);
const target_lat = parseFloat(req.query.tlat);
const target_lng = parseFloat(req.query.tlng);
let interval = 2000;
let intervalId;
if ( isNaN(start_lat) || isNaN(start_lng) || isNaN(target_lat) || isNaN(target_lng) ) {
res.json({ message: "Missing required params" });
return;
}
const options = {
debug: process.env.NODE_ENV === 'development'
};
function success (route) {
console.log('found route');
clearInterval(intervalId);
res.write(JSON.stringify(route));
res.end();
}
function failure (errmsg) {
clearInterval(intervalId);
res.write(JSON.stringify({ error: errmsg }));
res.end();
}
function callback (error, data) {
if (error) return failure(error);
return success(data);
}
function keepAlive () {
res.write(' ');
}
res.writeHead(200, { 'Content-Type': 'application/json' });
// send a single byte to keep the request open to prevent Heroku from shutting it down if it takes longer than 30s
intervalId = setInterval(keepAlive, interval);
workers.findRoute(start_lat, start_lng, target_lat, target_lng, options, callback);
});
// Handle Mailchimp API calls from the client
app.post('/signup', function(req, res) {
// validate body content
if (!req.body || !req.body.email_address) {
return res.status(500).send('Invalid mailchimp post');
}
// validate email address
if (/^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/.test(req.body.email_address)) {
makePostRequest(req.body.email_address, res);
} else {
return res.status(500).send('Invalid email format');
}
});
// Always return the main index.html, so react-router render the route in the client
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client/dist', 'index.html'));
});
// necessary to use http.createServer for enforcing HTTPS
http.createServer(app).listen(process.env.PORT || 5001, function() {
console.log('Listening on http://%s:%d/', this.address().address, this.address().port);
});