Skip to content

Commit

Permalink
Add support for geoJSON endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed Dec 13, 2019
1 parent 6f2a7cb commit 7f6f7c4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
42 changes: 42 additions & 0 deletions openfaas/services/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,48 @@
curl -s 127.0.0.1:8080/function/db-reader/positions
```

GET `/positions-geojson` - return geoJSON to be used with a feed or external API

```
curl -s 127.0.0.1:8080/function/db-reader/positions-geojson
```

```json
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-0.2392101,
52.5724835
]
},
"properties": {
"title": "Halo",
"icon": "airfield"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-0.2400627,
52.5736589
]
},
"properties": {
"title": "Market-watch",
"icon": "airfield"
}
},
]
}
```

* render-map

Static webpage assets generated from a webpack build. The webpage is written in React and renders locations from the `db-reader` endpoint using GeoJSON.
Expand Down
32 changes: 31 additions & 1 deletion openfaas/services/db-reader/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,39 @@ module.exports = async (event, context) => {
let {rows} = await selectEvents(client);
client.release()
return context.status(200).succeed({"status": "OK", "data": rows});
} else if(event.path && event.path.indexOf("positions")>-1) {
} else if(event.path && (event.path.indexOf("positions")>-1 || event.path.indexOf("positions-geojson")> -1)) {
let {rows} = await selectPositions(client);
client.release()

if(event.path.indexOf("positions-geojson") > -1) {
var features = [];

rows.forEach(l => {
features.push({
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [
l.location.x,
l.location.y
]
},
'properties': {
'title': l.name,
"icon": "airfield"
}
});
});
let payload = {
'type': 'FeatureCollection',
'features': features
};

return context
.status(200)
.succeed(payload);
}

return context.status(200).succeed({"status": "OK", "data": rows});
}
}
Expand Down
2 changes: 1 addition & 1 deletion openfaas/services/stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ functions:
db-reader:
lang: node12
handler: ./db-reader
image: ${DOCKER_USER:-alexellis2}/db-reader:0.1.4
image: ${DOCKER_USER:-alexellis2}/db-reader:0.1.6
environment:
db_port: 5432
db_name: postgres
Expand Down

0 comments on commit 7f6f7c4

Please sign in to comment.