digidem / osm-p2p-server

Peer-to-peer OpenStreetMap API v0.6 Server for osm-p2p-db
BSD 2-Clause "Simplified" License
87 stars 10 forks source link

Restructuring code for easier testing and re-usability #21

Closed gmaclennan closed 7 years ago

gmaclennan commented 7 years ago

This restructure attempts to make testing and debugging easier by separating out the osm-db related logic from the server logic. Separating out this logic will also allow us to export and use the higher-level methods such as get_map() without a server. Finally, this restructure opens up the possibility of returning OSM JSON from the API as well as OSM XML.

All the methods under actions/ should return valid OSM JSON, either as an object stream or an object. The server routes should worry about converting this to XML or a JSON string based on headers.

I am also replacing the custom xml builder lib/h.js with xml2js, which uses xml-builder and has good test coverage. This reduces the chances of weird edge-case errors with xml building.

Currently I'm also replacing osm-p2p-xml with lib/json_to_xml which works better with the new code structure. This could maybe move to its own module (back into osm-p2p-xml) in the future if necessary.

gmaclennan commented 7 years ago

Update on how this is now organized:

api is a higher-level wrapper around osm-p2p-db. All API methods are created by composition to ease unit-testing. Some/all of these methods should maybe end up in osm-p2p-db itself.

Route handlers are individual files for each route, under handlers/. They expect to be called with parameters (req, res, api, params, next). This remotes any dependency on the routes module and makes it easy to swap out with a different router (see express.js example).

Route handlers only deal with logic related http and xml. Api methods speak OSM JSON.