mapserver/web/mapblock.go

42 lines
816 B
Go
Raw Normal View History

2019-01-18 13:04:37 +03:00
package web
import (
"encoding/json"
"mapserver/app"
"mapserver/coords"
"net/http"
"strconv"
"strings"
)
type MapblockHandler struct {
ctx *app.App
}
func (h *MapblockHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
str := strings.TrimPrefix(req.URL.Path, "/api/mapblock/")
parts := strings.Split(str, "/")
if len(parts) != 3 {
resp.WriteHeader(500)
resp.Write([]byte("wrong number of arguments"))
return
}
x, _ := strconv.Atoi(parts[0])
y, _ := strconv.Atoi(parts[1])
z, _ := strconv.Atoi(parts[2])
c := coords.NewMapBlockCoords(x, y, z)
2019-07-31 14:41:35 +03:00
mb, err := h.ctx.MapBlockAccessor.GetMapBlock(c)
2019-01-18 13:04:37 +03:00
if err != nil {
resp.WriteHeader(500)
resp.Write([]byte(err.Error()))
} else {
resp.Header().Add("content-type", "application/json")
json.NewEncoder(resp).Encode(mb)
}
}