1
0
forked from MTSR/mapserver
mapserver/web/mapobjects.go

51 lines
990 B
Go
Raw Normal View History

2019-01-28 12:42:07 +03:00
package web
import (
2019-01-28 13:45:23 +03:00
"encoding/json"
"mapserver/coords"
2019-01-28 12:42:07 +03:00
"mapserver/mapobjectdb"
"net/http"
2019-02-22 20:30:38 +03:00
"github.com/prometheus/client_golang/prometheus"
2019-01-28 12:42:07 +03:00
)
2021-04-12 14:55:00 +03:00
func (api *Api) QueryMapobjects(resp http.ResponseWriter, req *http.Request) {
2019-01-28 12:42:07 +03:00
2019-02-15 10:36:48 +03:00
timer := prometheus.NewTimer(mapobjectServeDuration)
defer timer.ObserveDuration()
2019-02-22 16:57:40 +03:00
decoder := json.NewDecoder(req.Body)
2019-02-22 20:30:38 +03:00
q := mapobjectdb.SearchQuery{}
2019-01-28 12:42:07 +03:00
2019-02-22 16:57:40 +03:00
err := decoder.Decode(&q)
2019-01-28 12:42:07 +03:00
if err != nil {
resp.WriteHeader(500)
resp.Write([]byte(err.Error()))
2019-02-22 16:57:40 +03:00
return
}
2019-01-28 12:42:07 +03:00
// apply defaults
limit := 1000
if q.Limit != nil {
q.Limit = &limit
}
if q.Pos1 == nil {
q.Pos1 = &coords.MapBlockCoords{X: -2048, Y: -2048, Z: -2048}
}
if q.Pos2 == nil {
q.Pos2 = &coords.MapBlockCoords{X: 2048, Y: 2048, Z: 2048}
}
2021-04-12 14:55:00 +03:00
objects, err := api.Context.Objectdb.GetMapData(&q)
2019-02-22 16:57:40 +03:00
if err != nil {
resp.WriteHeader(500)
resp.Write([]byte(err.Error()))
return
2019-01-28 12:42:07 +03:00
}
2019-02-22 16:57:40 +03:00
resp.Header().Add("content-type", "application/json")
json.NewEncoder(resp).Encode(objects)
2019-01-28 12:42:07 +03:00
}