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

56 lines
1.2 KiB
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"
2019-02-15 11:08:22 +03:00
"github.com/prometheus/client_golang/prometheus"
2019-01-28 12:42:07 +03:00
"mapserver/app"
"mapserver/coords"
"mapserver/mapobjectdb"
"net/http"
"strconv"
"strings"
)
type MapObjects struct {
ctx *app.App
}
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
}
2019-02-15 10:36:48 +03:00
timer := prometheus.NewTimer(mapobjectServeDuration)
defer timer.ObserveDuration()
2019-01-28 12:42:07 +03:00
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)
if err != nil {
resp.WriteHeader(500)
resp.Write([]byte(err.Error()))
} else {
resp.Header().Add("content-type", "application/json")
json.NewEncoder(resp).Encode(objects)
}
}