Skip to content

Commit

Permalink
started web views
Browse files Browse the repository at this point in the history
  • Loading branch information
akhenakh committed Nov 21, 2019
1 parent 1241e0a commit 7442126
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 25 deletions.
1 change: 0 additions & 1 deletion cmd/geottncli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var (
lng = flag.Float64("lng", 2.2, "Lng")
radius = flag.Float64("radius", 1000, "Radius in meters")
key = flag.String("key", "", "ask for a key, if empty perform radius search")
topic = flag.String("topic", "metar", "topic")
)

func main() {
Expand Down
38 changes: 26 additions & 12 deletions cmd/geottnd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/dgraph-io/badger/v2/options"
log "github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/gobuffalo/packr/v2"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
grpc_middleware "github.com/mwitkow/go-grpc-middleware"
Expand All @@ -38,22 +40,24 @@ const appName = "geottnd"
var (
version = "no version from LDFLAGS"

appID = flag.String("appID", "akhtestapp", "The things network application ID")
appAccessKey = flag.String("appAccessKey", "", "The things network access key")
httpMetricsPort = flag.Int("httpMetricsPort", 8888, "http port")
httpAPIPort = flag.Int("httpAPIPort", 9201, "http API port")
grpcPort = flag.Int("grpcPort", 9200, "gRPC API port")
healthPort = flag.Int("healthPort", 6666, "grpc health port")
channel = flag.Int("channel", 1, "the Cayenne channel where to find gps messages")
dbPath = flag.String("dbPath", "geo.db", "DB path")
appID = flag.String("appID", "akhtestapp", "The things network application ID")
appAccessKey = flag.String("appAccessKey", "", "The things network access key")
channel = flag.Int("channel", 1, "the Cayenne channel where to find gps messages")

key = flag.String("key", "", "The key that will passed in the queries to the tiles server")
tilesKey = flag.String("tilesKey", "", "The key that will passed in the queries to the tiles server")
tilesURL = flag.String(
"tilesURL",
"http://127.0.0.1:8081",
"the URL where to point to get tiles",
)

dbPath = flag.String("dbPath", "geo.db", "DB path")

httpMetricsPort = flag.Int("httpMetricsPort", 8888, "http port")
httpAPIPort = flag.Int("httpAPIPort", 9201, "http API port")
grpcPort = flag.Int("grpcPort", 9200, "gRPC API port")
healthPort = flag.Int("healthPort", 6666, "grpc health port")

httpServer *http.Server
grpcHealthServer *grpc.Server
grpcServer *grpc.Server
Expand Down Expand Up @@ -132,9 +136,16 @@ func main() {

// geottn server
cfg := geottn.Config{
Channel: *channel,
Channel: *channel,
TilesURL: *tilesURL,
TilesKey: *tilesKey,
}

// box html templates
box := packr.New("Root box", "./htdocs")

s := geottn.NewServer(appName, logger, cfg)
s.FileHandler = http.FileServer(box)
s.GeoDB = idx

// gRPC Server
Expand Down Expand Up @@ -172,6 +183,11 @@ func main() {
r := mux.NewRouter()
r.HandleFunc("/api/data/{key}", s.DataQuery)
r.HandleFunc("/api/rect/{urlat}/{urlng}/{bllat}/{bllng}", s.RectQuery)
r.PathPrefix("/").Handler(
handlers.CompressHandler(
handlers.CORS(
handlers.AllowedOrigins([]string{"*"}))(s)))

httpServer = &http.Server{
Addr: fmt.Sprintf(":%d", *httpAPIPort),
ReadTimeout: 10 * time.Second,
Expand Down Expand Up @@ -246,8 +262,6 @@ func main() {
}

}

return nil
})

select {
Expand Down
101 changes: 101 additions & 0 deletions cmd/geottnd/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8' />
<title>Locator map {{ .Version }}</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='{{ .TilesURL }}/mapbox-gl.js'></script>
<link href='{{ .TilesURL }}/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>

<div id='map'></div>
<script>
const map = new mapboxgl.Map({
container: 'map',
style: '{{ .TilesURL }}/osm-liberty-gl.style',
center: [{{ .Lng }}, {{ .Lat }}],
zoom: 6,
maxZoom: 15,
minZoom: 6,
{{ if .TilesKey}}
transformRequest: (url, resourceType)=> {
if(resourceType === 'Tile') {
return {
url: url,
headers: { 'X-Key': '{{ .TilesKey }}'}
}
}
}
{{ end }}
});
// Add zoom and rotation controls to the map.
map.addControl(new mapboxgl.NavigationControl());
// Add geolocate control to the map.
map.addControl(new mapboxgl.GeolocateControl({
positionOptions: {
enableHighAccuracy: false
},
trackUserLocation: true
}));
function urlForBounds() {
const urlParams = new URLSearchParams(location.search);
let topic = 'metar';
if (urlParams.has('topic')) {
topic = urlParams.get('topic');
}
const mapBounds = map.getBounds();
return "/api/rect/" + "/" + mapBounds.getNorthEast().lat + "/" + mapBounds.getNorthEast().lng +
"/" + mapBounds.getSouthWest().lat + "/" + mapBounds.getSouthWest().lng;
}
map.on('load', function () {
map.addSource('points', { type: 'geojson', data: urlForBounds() });
map.addLayer({
"id": "points",
"type": "symbol",
"source": "points",
"layout": {
"icon-image": "airport_11",
"text-field": ["get", "key"],
"text-font": ["Roboto Regular"],
"text-variable-anchor": ["top"],
"text-justify": "auto",
"text-radial-offset": 0.7,
}
});
});
map.on('click', 'points', function (e) {
const key = e.features[0].properties.key;
const coordinates = e.features[0].geometry.coordinates.slice();

const xhr = new XMLHttpRequest();
xhr.open('GET', "/api/data/" + "/" + key, true);
xhr.onload = function() {
if (xhr.status !== 200) {
return;
}
let data = JSON.parse(xhr.responseText);
// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
new mapboxgl.Popup()
.setLngLat(coordinates)
.setHTML(data.properties.data)
.addTo(map);
};
xhr.send();
});
map.on('moveend', function () {
map.getSource('points').setData(urlForBounds());
});
</script>
</body>
</html>

10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ require (
github.com/armon/go-metrics v0.3.0 // indirect
github.com/dgraph-io/badger/v2 v2.0.0
github.com/go-kit/kit v0.9.0
github.com/gobuffalo/envy v1.8.1 // indirect
github.com/gobuffalo/logger v1.0.2 // indirect
github.com/gobuffalo/packr/v2 v2.7.1
github.com/golang/geo v0.0.0-20190916061304-5b978397cfec
github.com/golang/protobuf v1.3.2
github.com/gorilla/handlers v1.4.2
github.com/gorilla/mux v1.7.3
github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
Expand All @@ -23,10 +27,14 @@ require (
github.com/namsral/flag v1.7.4-pre
github.com/opentracing/opentracing-go v1.1.0
github.com/prometheus/client_golang v0.9.2
github.com/rogpeppe/go-internal v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.4.0
github.com/twpayne/go-geom v1.0.5
golang.org/x/net v0.0.0-20190620200207-3b0461eec859
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e // indirect
golang.org/x/tools v0.0.0-20191121214350-f51c1a7cd27a // indirect
google.golang.org/grpc v1.20.1
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
)
Loading

0 comments on commit 7442126

Please sign in to comment.