Skip to content

Commit b511ec9

Browse files
save
1 parent a35b592 commit b511ec9

10 files changed

+143
-142
lines changed

routed-js/MatchServiceHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ServiceHandler } from "./ServiceHandler";
1+
import { ServiceHandler } from './ServiceHandler';
22

33
export class MatchServiceHandler extends ServiceHandler {
44
protected buildServiceOptions(options: any, query: any): any {

routed-js/NearestServiceHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ServiceHandler } from "./ServiceHandler";
1+
import { ServiceHandler } from './ServiceHandler';
22

33
export class NearestServiceHandler extends ServiceHandler {
44
protected buildServiceOptions(options: any, query: any): any {

routed-js/OSRMWrapper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const util = require('util');
2-
const OSRM = require('../lib/index.js')
1+
import util from 'util';
2+
const OSRM = require('../lib/index.js');
33

44
export class OSRMWrapper {
55
private readonly osrm: typeof OSRM;

routed-js/RouteServiceHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import { ServiceHandler } from "./ServiceHandler";
2+
import { ServiceHandler } from './ServiceHandler';
33

44
export class RouteServiceHandler extends ServiceHandler {
55
protected buildServiceOptions(options: any, query: any): any {

routed-js/ServiceHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
import { Format } from "./Format";
3-
import { OSRMWrapper } from "./OSRMWrapper";
2+
import { Format } from './Format';
3+
import { OSRMWrapper } from './OSRMWrapper';
44

55

66

routed-js/TableServiceHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ServiceHandler } from "./ServiceHandler";
1+
import { ServiceHandler } from './ServiceHandler';
22

33
export class TableServiceHandler extends ServiceHandler {
44
protected buildServiceOptions(options: any, query: any): any {

routed-js/TripServiceHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ServiceHandler } from "./ServiceHandler";
1+
import { ServiceHandler } from './ServiceHandler';
22

33
export class TripServiceHandler extends ServiceHandler {
44
protected buildServiceOptions(options: any, query: any): any {

routed-js/routed.ts

Lines changed: 115 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,140 @@
11
#!/usr/bin/env node
2-
import Fastify, { FastifyReply, FastifyRequest } from 'fastify'
2+
import Fastify, { FastifyReply, FastifyRequest } from 'fastify';
33
import { OSRMWrapper } from './OSRMWrapper';
44
import yargs from 'yargs/yargs';
55
import { routeSchema, nearestSchema, tableSchema, tripSchema, matchSchema, tileSchema, parseQueryString, parseCoordinatesAndFormat } from './schema';
66
import { ServiceHandler } from './ServiceHandler';
7-
import { MatchServiceHandler } from "./MatchServiceHandler";
8-
import { NearestServiceHandler } from "./NearestServiceHandler";
9-
import { RouteServiceHandler } from "./RouteServiceHandler";
10-
import { TableServiceHandler } from "./TableServiceHandler";
11-
import { TripServiceHandler } from "./TripServiceHandler";
12-
import { Format } from "./Format";
7+
import { MatchServiceHandler } from './MatchServiceHandler';
8+
import { NearestServiceHandler } from './NearestServiceHandler';
9+
import { RouteServiceHandler } from './RouteServiceHandler';
10+
import { TableServiceHandler } from './TableServiceHandler';
11+
import { TripServiceHandler } from './TripServiceHandler';
12+
import { Format } from './Format';
1313

1414

1515
async function main() {
16-
const argv = await yargs(process.argv.slice(2)).options({
17-
ip: { type: 'string', default: '0.0.0.0', alias: 'i' },
18-
port: { type: 'number', default: 5000, alias: 'p' },
19-
threads: { type: 'number', alias: 't' },
20-
shared_memory: { type: 'boolean', alias: ['shared-memory', 's'] },
21-
algorithm: { choices: ['CH', 'CoreCH', 'MLD'], default: 'CH', alias: 'a' },
22-
dataset_name: { type: 'string', alias: 'dataset-name' },
23-
max_viaroute_size: { type: 'number', alias: 'max-viaroute-size', default: 500 },
24-
max_trip_size: { type: 'number', alias: 'max-trip-size', default: 100 },
25-
max_table_size: { type: 'number', alias: 'max-table-size', default: 100 },
26-
max_matching_size: { type: 'number', alias: 'max-matching-size', default: 100 },
27-
max_nearest_size: { type: 'number', alias: 'max-nearest-size', default: 100 },
28-
max_alternatives: { type: 'number', alias: 'max-alternatives', default: 3 },
29-
max_matching_radius: { type: 'number', alias: 'max-matching-radius', default: -1 },
30-
version: { alias: 'v' }
31-
})
32-
.help('h')
33-
.alias('h', 'help')
34-
.strict()
35-
.argv;
36-
37-
if (argv.version) {
16+
const argv = await yargs(process.argv.slice(2)).options({
17+
ip: { type: 'string', default: '0.0.0.0', alias: 'i' },
18+
port: { type: 'number', default: 5000, alias: 'p' },
19+
threads: { type: 'number', alias: 't' },
20+
shared_memory: { type: 'boolean', alias: ['shared-memory', 's'] },
21+
algorithm: { choices: ['CH', 'CoreCH', 'MLD'], default: 'CH', alias: 'a' },
22+
dataset_name: { type: 'string', alias: 'dataset-name' },
23+
max_viaroute_size: { type: 'number', alias: 'max-viaroute-size', default: 500 },
24+
max_trip_size: { type: 'number', alias: 'max-trip-size', default: 100 },
25+
max_table_size: { type: 'number', alias: 'max-table-size', default: 100 },
26+
max_matching_size: { type: 'number', alias: 'max-matching-size', default: 100 },
27+
max_nearest_size: { type: 'number', alias: 'max-nearest-size', default: 100 },
28+
max_alternatives: { type: 'number', alias: 'max-alternatives', default: 3 },
29+
max_matching_radius: { type: 'number', alias: 'max-matching-radius', default: -1 },
30+
version: { alias: 'v' }
31+
})
32+
.help('h')
33+
.alias('h', 'help')
34+
.strict()
35+
.argv;
36+
37+
if (argv.version) {
3838
// TODO: print real version
39-
process.stdout.write('v5.27.0\n');
40-
return;
41-
}
39+
process.stdout.write('v5.27.0\n');
40+
return;
41+
}
4242

43-
if (argv._.length == 0 && !argv.shared_memory) {
43+
if (argv._.length == 0 && !argv.shared_memory) {
4444
// TODO: show usage
45-
return;
46-
}
47-
48-
const osrm = new OSRMWrapper({
49-
path: argv._[0],
50-
dataset_name: argv.dataset_name,
51-
algorithm: argv.algorithm,
52-
shared_memory: argv.shared_memory,
53-
max_viaroute_size: argv.max_viaroute_size,
54-
max_trip_size: argv.max_trip_size,
55-
max_table_size: argv.max_table_size,
56-
max_matching_size: argv.max_matching_size,
57-
max_nearest_size: argv.max_nearest_size,
58-
max_alternatives: argv.max_alternatives,
59-
max_matching_radius: argv.max_matching_size
60-
});
61-
62-
63-
const fastify = Fastify({
64-
logger: true,
65-
maxParamLength: Number.MAX_SAFE_INTEGER,
66-
rewriteUrl: (req) => {
67-
// https://github.com/fastify/fastify/issues/2487
68-
return req.url!.replace(/;/g, '%3B');
69-
},
70-
querystringParser: parseQueryString
71-
})
72-
73-
74-
75-
76-
async function processRequest(handler: ServiceHandler, request: FastifyRequest, reply: FastifyReply) {
77-
const { coordinatesAndFormat } = request.params as any;
78-
const query = request.query as any;
79-
80-
try {
81-
const { format, coordinates } = parseCoordinatesAndFormat(coordinatesAndFormat);
82-
83-
switch (format) {
84-
case Format.Json:
85-
reply.type('application/json').code(200);
86-
break;
87-
case Format.Flatbuffers:
88-
reply.type('application/x-flatbuffers;schema=osrm.engine.api.fbresult').code(200);
89-
break;
90-
}
91-
92-
const result = await handler.handle(coordinates, query, format);
93-
result['code'] = 'Ok';
94-
return result;
95-
} catch (e: any) {
96-
reply.code(400);
97-
98-
return {
99-
code: e.code,
100-
message: e.message
101-
}
45+
return;
46+
}
47+
48+
const osrm = new OSRMWrapper({
49+
path: argv._[0],
50+
dataset_name: argv.dataset_name,
51+
algorithm: argv.algorithm,
52+
shared_memory: argv.shared_memory,
53+
max_viaroute_size: argv.max_viaroute_size,
54+
max_trip_size: argv.max_trip_size,
55+
max_table_size: argv.max_table_size,
56+
max_matching_size: argv.max_matching_size,
57+
max_nearest_size: argv.max_nearest_size,
58+
max_alternatives: argv.max_alternatives,
59+
max_matching_radius: argv.max_matching_size
60+
});
61+
62+
63+
const fastify = Fastify({
64+
logger: true,
65+
maxParamLength: Number.MAX_SAFE_INTEGER,
66+
rewriteUrl: (req) => {
67+
// https://github.com/fastify/fastify/issues/2487
68+
return req.url!.replace(/;/g, '%3B');
69+
},
70+
querystringParser: parseQueryString
71+
});
72+
73+
74+
75+
76+
async function processRequest(handler: ServiceHandler, request: FastifyRequest, reply: FastifyReply) {
77+
const { coordinatesAndFormat } = request.params as any;
78+
const query = request.query as any;
79+
80+
try {
81+
const { format, coordinates } = parseCoordinatesAndFormat(coordinatesAndFormat);
82+
83+
switch (format) {
84+
case Format.Json:
85+
reply.type('application/json').code(200);
86+
break;
87+
case Format.Flatbuffers:
88+
reply.type('application/x-flatbuffers;schema=osrm.engine.api.fbresult').code(200);
89+
break;
90+
}
91+
92+
const result = await handler.handle(coordinates, query, format);
93+
result['code'] = 'Ok';
94+
return result;
95+
} catch (e: any) {
96+
reply.code(400);
97+
98+
return {
99+
code: e.code,
100+
message: e.message
101+
};
102+
}
102103
}
103-
}
104104

105-
fastify.get('/route/v1/:profile/:coordinatesAndFormat', { schema: routeSchema }, async (request, reply) => {
106-
return processRequest(new RouteServiceHandler(osrm), request, reply);
107-
});
105+
fastify.get('/route/v1/:profile/:coordinatesAndFormat', { schema: routeSchema }, async (request, reply) => {
106+
return processRequest(new RouteServiceHandler(osrm), request, reply);
107+
});
108108

109-
fastify.get('/nearest/v1/:profile/:coordinatesAndFormat', { schema: nearestSchema }, async (request, reply) => {
110-
return processRequest(new NearestServiceHandler(osrm), request, reply);
111-
});
109+
fastify.get('/nearest/v1/:profile/:coordinatesAndFormat', { schema: nearestSchema }, async (request, reply) => {
110+
return processRequest(new NearestServiceHandler(osrm), request, reply);
111+
});
112112

113-
fastify.get('/table/v1/:profile/:coordinatesAndFormat', { schema: tableSchema }, async (request, reply) => {
114-
return processRequest(new TableServiceHandler(osrm), request, reply);
115-
});
113+
fastify.get('/table/v1/:profile/:coordinatesAndFormat', { schema: tableSchema }, async (request, reply) => {
114+
return processRequest(new TableServiceHandler(osrm), request, reply);
115+
});
116116

117-
fastify.get('/match/v1/:profile/:coordinatesAndFormat', { schema: matchSchema }, async (request, reply) => {
118-
return processRequest(new MatchServiceHandler(osrm), request, reply);
119-
});
117+
fastify.get('/match/v1/:profile/:coordinatesAndFormat', { schema: matchSchema }, async (request, reply) => {
118+
return processRequest(new MatchServiceHandler(osrm), request, reply);
119+
});
120120

121-
fastify.get('/trip/v1/:profile/:coordinatesAndFormat', { schema: tripSchema }, async (request, reply) => {
122-
return processRequest(new TripServiceHandler(osrm), request, reply);
123-
});
121+
fastify.get('/trip/v1/:profile/:coordinatesAndFormat', { schema: tripSchema }, async (request, reply) => {
122+
return processRequest(new TripServiceHandler(osrm), request, reply);
123+
});
124124

125-
fastify.get('/tile/v1/:profile/tile(:x,:y,:zoom).mvt', { schema: tileSchema }, async (request, reply) => {
126-
const { x, y, zoom } = request.params as any;
125+
fastify.get('/tile/v1/:profile/tile(:x,:y,:zoom).mvt', { schema: tileSchema }, async (request, reply) => {
126+
const { x, y, zoom } = request.params as any;
127127

128-
reply.type('application/x-protobuf').code(200);
129-
return osrm.tile([zoom, x, y]);
130-
});
128+
reply.type('application/x-protobuf').code(200);
129+
return osrm.tile([zoom, x, y]);
130+
});
131131

132132

133-
fastify.listen({ port: argv.port, host: argv.ip }, (err, address) => {
134-
if (err) { throw err }
133+
fastify.listen({ port: argv.port, host: argv.ip }, (err, address) => {
134+
if (err) { throw err; }
135135

136-
process.stdout.write('running and waiting for requests\n');
137-
})
136+
process.stdout.write('running and waiting for requests\n');
137+
});
138138
}
139139

140140
main();

0 commit comments

Comments
 (0)