-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
214 lines (186 loc) · 6.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
'use strict';
const OSRMObj = require('osrm');
const Crypt = require('crypto');
const text = require('./text');
const Cache = require('./cache');
const Config = require('./config');
const Response = require('./response');
const OSRM = new OSRMObj(Config.core.mapAddress);
/**
* Main Class.
*
* @type {App}
*/
module.exports = class App {
/**
* Application Constructor
* @param request
* @param server
*/
constructor(request, server) {
this._coordinate = [];
this._timestamp = [];
this._radius = [];
this.server = server;
this.token = App.generateToken(request.url);
}
/**
* Is variable set ?
* @param variable
* @returns {boolean}
*/
static is(variable) {
return ( variable !== undefined );
};
/**
* It makes error ready to be written in the response
*
* @param error
* @param code
*/
static error(error, code = undefined) {
if (error === null && code !== undefined && text.error.http[code] !== undefined)
error = text.error.http[code];
return Response.make({'status': error});
};
/**
* Generates A token for the application based on the request parameters.
*
* @param request
* @returns {*}
*/
static generateToken(request) {
return Crypt.createHash('sha1').update(request, 'binary').digest('hex');
}
/**
* It pushes Coordinates and other related stuff into their own variable.
*
* @param method
* @param data
* @returns {boolean}
*/
fetchPointsOptions(method, data) {
if (data.coordinates === undefined)
return false;
let coordinateArr = data.coordinates.split(Config.core.delimiter);
let timestampArr = ( data.timestamps !== undefined ) ? data.timestamps.split(Config.core.delimiter) : undefined;
let radiusArr = ( data.radiuses !== undefined ) ? data.radiuses.split(Config.core.delimiter) : undefined;
let length = coordinateArr.length;
for (let i = 0; length > i; i++) {
let latLng = coordinateArr[i].split(',');
if (method === 'match' || method === 'nearest')
this._coordinate.push([parseFloat(latLng[1]), parseFloat(latLng[0])]);
else if (method === 'route')
this._coordinate.push([parseFloat(latLng[0]), parseFloat(latLng[1])]);
if (timestampArr && timestampArr[i]) {
if (timestampArr.length === length) {
this._timestamp.push(parseInt(timestampArr[i]));
} else {
this._timestamp = -1;
}
}
if (radiusArr && radiusArr[i]) {
if (radiusArr.length === length) {
this._radius.push(parseFloat(radiusArr[i]));
} else {
this._radius = -1;
}
}
}
};
/**
* Makes options array ready to be sent to the API.
*
* @param query
* @returns {{}}
*/
setOptions(query) {
let options = {};
options.coordinates = this._coordinate;
if (this._timestamp.length > 0)
options.timestamps = this._timestamp;
if (this._radius.length > 0)
options.radiuses = this._radius;
if (App.is(query.overview))
options.overview = query.overview;
if (App.is(query.geometries))
options.geometries = query.geometries;
if (App.is(query.annotations))
options.annotations = query.annotations;
if (App.is(query.steps))
options.steps = Boolean(query.steps);
if (App.is(query.alternateRoute))
options.alternateRoute = query.alternateRoute;
if (App.is(query.number))
options.number = query.number;
return options;
};
/**
* Is caching enabled ?
*
* @returns {boolean}
*/
static isCache() {
return Config.core.cache === true;
}
/**
* Initialize a new OSRM session.
*
* @param method
* @param data
* @returns {boolean}
*/
initOSRM(method, data) {
let cacheServer = new Cache;
cacheServer.get(this.token, (result) => {
if (result) {
Response.write(this.server, result, undefined, {
'Content-Length': Buffer.byteLength(result)
});
} else {
//Shared Variables
this.fetchPointsOptions(method, data);
if (this._coordinate.length < 2) {
Response.write(this.server, App.error(text.error.http[400]), 400);
return false;
}
if (this._timestamp === -1 || this._radius === -1) {
Response.write(this.server, App.error(text.error.app.radius_timestamp), 400);
return false;
}
let options = this.setOptions(data);
//Shared Variables
//Callback
/**
* The callback to use in OSRM Api.
*
* @param err
* @param result
* @returns {boolean}
*/
let osrmCallback = (err, result) => {
if (err) {
Response.write(this.server, App.error(err.message));
return false;
}
/*if (data.process)
result = Response.manipulate(method, result);*/
let resp = Response.make(result);
cacheServer.set(this.token, resp);
Response.write(this.server, resp, undefined, {
'Content-Length': Buffer.byteLength(resp)
});
return true;
};
//Callback
if (method === 'match') {
OSRM.match(options, osrmCallback);
} else if (method === 'route') {
OSRM.route(options, osrmCallback);
} else if (method === 'nearest') {
OSRM.nearest(options, osrmCallback);
}
}
});
};
};