Skip to content

Commit

Permalink
adds GET handling
Browse files Browse the repository at this point in the history
  • Loading branch information
normanrz committed Aug 15, 2018
1 parent 6d7acb0 commit 4f98004
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ jobs:
-H 'Content-Type: application/json' \
http://localhost:3000)
[ "$RESULT" = "[101,100]" ]
RESULT=$(curl http://localhost:3000/\?lat\=51.3\&lng\=13.4)
[ "$RESULT" = "101" ]
docker stop elevation
- run:
Expand All @@ -57,6 +59,8 @@ jobs:
-H 'Content-Type: application/json' \
http://localhost:3000)
[ "$RESULT" = "[101,100]" ]
RESULT=$(curl http://localhost:3000/\?lat\=51.3\&lng\=13.4)
[ "$RESULT" = "101" ]
docker stop elevation
- run:
Expand Down
44 changes: 38 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { json, send } = require("micro");
const limitedMap = require("limited-map");
const query = require("micro-query");
const { FileTileSet, S3TileSet } = require("./tileset");

const cacheSize = process.env.TILE_SET_CACHE || 128;
Expand All @@ -11,13 +12,13 @@ const tiles = tileFolder.startsWith("s3://")
? new S3TileSet({ cacheSize })
: new FileTileSet(tileFolder, { cacheSize });

module.exports = async (req, res) => {
if (req.method !== "POST") {
return send(res, 405, { error: "Only POST allowed" });
}

async function handlePOST(req, res) {
const payload = await json(req, { limit: maxPostSize });
if (!payload || Object.keys(payload).length === 0) {
if (
!payload ||
!Array.isArray(payload) ||
!payload.every(([lat, lng]) => Number.isFinite(lat) && Number.isFinite(lng))
) {
return send(res, 400, {
error:
"Invalid Payload. Expected a JSON array with latitude-longitude pairs: [[lat, lng], ...]"
Expand All @@ -30,4 +31,35 @@ module.exports = async (req, res) => {
maxParallelProcessing
);
return result;
}

async function handleGET(req, res) {
const reqQuery = query(req);
const lat = parseFloat(reqQuery.lat);
const lng = parseFloat(reqQuery.lng);
if (lat == null || !Number.isFinite(lat)) {
return send(res, 400, {
error:
"Invalid Latitude. Expected a float number as query parameter: ?lat=12.3&lng=45.6"
});
}
if (lng == null || !Number.isFinite(lng)) {
return send(res, 400, {
error:
"Invalid Longitude. Expected a float number as query parameter: ?lat=12.3&lng=45.6"
});
}
const result = await tiles.getElevation([lat, lng]);
return result;
}

module.exports = async (req, res) => {
switch (req.method) {
case "POST":
return handlePOST(req, res);
case "GET":
return handleGET(req, res);
default:
return send(res, 405, { error: "Only GET or POST allowed" });
}
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"dependencies": {
"limited-map": "^0.0.1",
"lru-memoize": "^1.0.2",
"micro": "^9.3.2"
"micro": "^9.3.2",
"micro-query": "^0.3.0"
},
"devDependencies": {
"prettier": "^1.14.0"
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ lru-memoize@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/lru-memoize/-/lru-memoize-1.0.2.tgz#f5ae84d288e7d55fec8388ec0bd725621bc815d0"

micro-query@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/micro-query/-/micro-query-0.3.0.tgz#10bd973d6248379ab5847ab92d3365fc344f309d"
dependencies:
qs "6.5.2"

micro@^9.3.2:
version "9.3.2"
resolved "https://registry.yarnpkg.com/micro/-/micro-9.3.2.tgz#41e249fe96a33db056f0a5dabb32c8fd4b69c5ca"
Expand All @@ -93,6 +99,10 @@ prettier@^1.14.0:
version "1.14.0"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.14.0.tgz#847c235522035fd988100f1f43cf20a7d24f9372"

qs@6.5.2:
version "6.5.2"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36"

raw-body@2.3.2:
version "2.3.2"
resolved "https://registry.yarnpkg.com/raw-body/-/raw-body-2.3.2.tgz#bcd60c77d3eb93cde0050295c3f379389bc88f89"
Expand Down

0 comments on commit 4f98004

Please sign in to comment.