|
1 | 1 | #!/usr/bin/env node
|
2 |
| -import Fastify, { FastifyReply, FastifyRequest } from 'fastify' |
| 2 | +import Fastify, { FastifyReply, FastifyRequest } from 'fastify'; |
3 | 3 | import { OSRMWrapper } from './OSRMWrapper';
|
4 | 4 | import yargs from 'yargs/yargs';
|
5 | 5 | import { routeSchema, nearestSchema, tableSchema, tripSchema, matchSchema, tileSchema, parseQueryString, parseCoordinatesAndFormat } from './schema';
|
6 | 6 | 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'; |
13 | 13 |
|
14 | 14 |
|
15 | 15 | 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) { |
38 | 38 | // 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 | + } |
42 | 42 |
|
43 |
| - if (argv._.length == 0 && !argv.shared_memory) { |
| 43 | + if (argv._.length == 0 && !argv.shared_memory) { |
44 | 44 | // 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 | + } |
102 | 103 | }
|
103 |
| - } |
104 | 104 |
|
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 | + }); |
108 | 108 |
|
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 | + }); |
112 | 112 |
|
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 | + }); |
116 | 116 |
|
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 | + }); |
120 | 120 |
|
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 | + }); |
124 | 124 |
|
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; |
127 | 127 |
|
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 | + }); |
131 | 131 |
|
132 | 132 |
|
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; } |
135 | 135 |
|
136 |
| - process.stdout.write('running and waiting for requests\n'); |
137 |
| - }) |
| 136 | + process.stdout.write('running and waiting for requests\n'); |
| 137 | + }); |
138 | 138 | }
|
139 | 139 |
|
140 | 140 | main();
|
|
0 commit comments