1
0
forked from MTSR/mapserver
mapserver/web/mapobjects.go
2023-12-29 16:00:11 +01:00

51 lines
987 B
Go

package web
import (
"encoding/json"
"mapserver/mapobjectdb"
"mapserver/types"
"net/http"
"github.com/prometheus/client_golang/prometheus"
)
func (api *Api) QueryMapobjects(resp http.ResponseWriter, req *http.Request) {
timer := prometheus.NewTimer(mapobjectServeDuration)
defer timer.ObserveDuration()
decoder := json.NewDecoder(req.Body)
q := mapobjectdb.SearchQuery{}
err := decoder.Decode(&q)
if err != nil {
resp.WriteHeader(500)
resp.Write([]byte(err.Error()))
return
}
// apply defaults
limit := 1000
if q.Limit == nil {
q.Limit = &limit
}
if q.Pos1 == nil {
q.Pos1 = &types.MapBlockCoords{X: -2048, Y: -2048, Z: -2048}
}
if q.Pos2 == nil {
q.Pos2 = &types.MapBlockCoords{X: 2048, Y: 2048, Z: 2048}
}
objects, err := api.Context.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)
}