From cd54371361b580cae2144de766825febc428f1c4 Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Fri, 22 Feb 2019 14:57:40 +0100 Subject: [PATCH] POST mapobject query --- server/static/js/api.js | 12 ++--- .../js/overlays/AbstractGeoJsonOverlay.js | 9 ++-- .../static/js/overlays/AbstractIconOverlay.js | 9 ++-- server/web/mapobjects.go | 44 ++++++------------- 4 files changed, 30 insertions(+), 44 deletions(-) diff --git a/server/static/js/api.js b/server/static/js/api.js index 389836c..ba01bb6 100644 --- a/server/static/js/api.js +++ b/server/static/js/api.js @@ -1,11 +1,11 @@ var api = { - getMapObjects: function(x1,y1,z1,x2,y2,z2,type){ - return m.request("/api/mapobjects/" + - x1 + "/" + y1 + "/" + z1 + "/" + - x2 + "/" + y2 + "/" + z2 + "/" + - type - ); + getMapObjects: function(query){ + return m.request({ + method: "POST", + url: "api/mapobjects", + data: query + }); }, getConfig: function(){ diff --git a/server/static/js/overlays/AbstractGeoJsonOverlay.js b/server/static/js/overlays/AbstractGeoJsonOverlay.js index 78a4353..a650ef3 100644 --- a/server/static/js/overlays/AbstractGeoJsonOverlay.js +++ b/server/static/js/overlays/AbstractGeoJsonOverlay.js @@ -57,10 +57,11 @@ var AbstractGeoJsonOverlay = L.LayerGroup.extend({ var z1 = parseInt(min.lat); var z2 = parseInt(max.lat); - api.getMapObjects( - x1, y1, z1, - x2, y2, z2, - this.type) + api.getMapObjects({ + pos1: { x:x1, y:y1, z:z1 }, + pos2: { x:x2, y:y2, z:z2 }, + type: this.type + }) .then(function(objects){ self.clearLayers(); diff --git a/server/static/js/overlays/AbstractIconOverlay.js b/server/static/js/overlays/AbstractIconOverlay.js index 625d793..3cd767d 100644 --- a/server/static/js/overlays/AbstractIconOverlay.js +++ b/server/static/js/overlays/AbstractIconOverlay.js @@ -72,10 +72,11 @@ var AbstractIconOverlay = L.LayerGroup.extend({ var z1 = parseInt(min.lat); var z2 = parseInt(max.lat); - api.getMapObjects( - x1, y1, z1, - x2, y2, z2, - this.type) + api.getMapObjects({ + pos1: { x:x1, y:y1, z:z1 }, + pos2: { x:x2, y:y2, z:z2 }, + type: this.type + }) .then(function(objects){ //TODO: remove non-existing markers diff --git a/server/web/mapobjects.go b/server/web/mapobjects.go index a1d2091..4723928 100644 --- a/server/web/mapobjects.go +++ b/server/web/mapobjects.go @@ -4,11 +4,8 @@ import ( "encoding/json" "github.com/prometheus/client_golang/prometheus" "mapserver/app" - "mapserver/coords" "mapserver/mapobjectdb" "net/http" - "strconv" - "strings" ) type MapObjects struct { @@ -16,40 +13,27 @@ type MapObjects struct { } func (t *MapObjects) ServeHTTP(resp http.ResponseWriter, req *http.Request) { - str := strings.TrimPrefix(req.URL.Path, "/api/mapobjects/") - // x1/y1/z1/x2/y2/z2/type - parts := strings.Split(str, "/") - if len(parts) != 7 { - resp.WriteHeader(500) - resp.Write([]byte("wrong number of arguments")) - return - } timer := prometheus.NewTimer(mapobjectServeDuration) defer timer.ObserveDuration() - x1, _ := strconv.Atoi(parts[0]) - y1, _ := strconv.Atoi(parts[1]) - z1, _ := strconv.Atoi(parts[2]) - x2, _ := strconv.Atoi(parts[3]) - y2, _ := strconv.Atoi(parts[4]) - z2, _ := strconv.Atoi(parts[5]) - typeStr := parts[6] - - q := mapobjectdb.SearchQuery{ - Pos1: coords.NewMapBlockCoords(x1, y1, z1), - Pos2: coords.NewMapBlockCoords(x2, y2, z2), - Type: typeStr, - } - objects, err := t.ctx.Objectdb.GetMapData(q) + decoder := json.NewDecoder(req.Body) + var q mapobjectdb.SearchQuery + err := decoder.Decode(&q) if err != nil { resp.WriteHeader(500) resp.Write([]byte(err.Error())) - - } else { - resp.Header().Add("content-type", "application/json") - json.NewEncoder(resp).Encode(objects) - + return } + + objects, err := t.ctx.Objectdb.GetMapData(q) + if err != nil { + resp.WriteHeader(500) + resp.Write([]byte(err.Error())) + return + } + + resp.Header().Add("content-type", "application/json") + json.NewEncoder(resp).Encode(objects) }