Permalink
Browse files

adds GET handling

  • Loading branch information...
normanrz committed Aug 15, 2018
1 parent 6d7acb0 commit 4f980043a5037a6bbdc67c68341367160dfff79f
Showing with 54 additions and 7 deletions.
  1. +4 −0 .circleci/config.yml
  2. +38 −6 index.js
  3. +2 −1 package.json
  4. +10 −0 yarn.lock
@@ -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:
@@ -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:
@@ -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;
@@ -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], ...]"
@@ -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" });
}
};
@@ -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"

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 4f98004

Please sign in to comment.