1
0
forked from MTSR/mapserver
This commit is contained in:
BuckarooBanzay 2021-04-12 13:55:00 +02:00
parent a4577c41a9
commit 57ebd896da
9 changed files with 29 additions and 67 deletions

View File

@ -2,25 +2,20 @@ package web
import (
"encoding/json"
"mapserver/app"
"net/http"
)
type ColorMappingHandler struct {
ctx *app.App
}
type Color struct {
R uint8 `json:"r"`
G uint8 `json:"g"`
B uint8 `json:"b"`
}
func (h *ColorMappingHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
func (api *Api) GetColorMapping(resp http.ResponseWriter, req *http.Request) {
cm := make(map[string]Color)
for k, v := range h.ctx.Colormapping.GetColors() {
for k, v := range api.Context.Colormapping.GetColors() {
cm[k] = Color{R: v.R, G: v.G, B: v.B}
}

View File

@ -16,19 +16,15 @@ type PublicConfig struct {
EnableSearch bool `json:"enablesearch"`
}
type ConfigHandler struct {
ctx *app.App
}
func (h *ConfigHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
func (api *Api) GetConfig(resp http.ResponseWriter, req *http.Request) {
resp.Header().Add("content-type", "application/json")
webcfg := PublicConfig{}
webcfg.Layers = h.ctx.Config.Layers
webcfg.MapObjects = h.ctx.Config.MapObjects
webcfg.Layers = api.Context.Config.Layers
webcfg.MapObjects = api.Context.Config.MapObjects
webcfg.Version = app.Version
webcfg.DefaultOverlays = h.ctx.Config.DefaultOverlays
webcfg.EnableSearch = h.ctx.Config.EnableSearch
webcfg.DefaultOverlays = api.Context.Config.DefaultOverlays
webcfg.EnableSearch = api.Context.Config.EnableSearch
json.NewEncoder(resp).Encode(webcfg)
}

View File

@ -2,18 +2,13 @@ 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) {
func (api *Api) GetMapBlockData(resp http.ResponseWriter, req *http.Request) {
str := strings.TrimPrefix(req.URL.Path, "/api/mapblock/")
parts := strings.Split(str, "/")
if len(parts) != 3 {
@ -27,7 +22,7 @@ func (h *MapblockHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request)
z, _ := strconv.Atoi(parts[2])
c := coords.NewMapBlockCoords(x, y, z)
mb, err := h.ctx.MapBlockAccessor.GetMapBlock(c)
mb, err := api.Context.MapBlockAccessor.GetMapBlock(c)
if err != nil {
resp.WriteHeader(500)

View File

@ -2,18 +2,13 @@ package web
import (
"encoding/json"
"mapserver/app"
"mapserver/mapobjectdb"
"net/http"
"github.com/prometheus/client_golang/prometheus"
)
type MapObjects struct {
ctx *app.App
}
func (t *MapObjects) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
func (api *Api) QueryMapobjects(resp http.ResponseWriter, req *http.Request) {
timer := prometheus.NewTimer(mapobjectServeDuration)
defer timer.ObserveDuration()
@ -28,7 +23,7 @@ func (t *MapObjects) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
return
}
objects, err := t.ctx.Objectdb.GetMapData(&q)
objects, err := api.Context.Objectdb.GetMapData(&q)
if err != nil {
resp.WriteHeader(500)
resp.Write([]byte(err.Error()))

View File

@ -1,17 +1,12 @@
package web
import (
"mapserver/app"
"mapserver/public"
"net/http"
"strings"
)
type MediaHandler struct {
ctx *app.App
}
func (h *MediaHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
func (api *Api) GetMedia(resp http.ResponseWriter, req *http.Request) {
str := strings.TrimPrefix(req.URL.Path, "/api/media/")
parts := strings.Split(str, "/")
if len(parts) != 1 {
@ -23,7 +18,7 @@ func (h *MediaHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
filename := parts[0]
fallback, hasfallback := req.URL.Query()["fallback"]
content := h.ctx.MediaRepo[filename]
content := api.Context.MediaRepo[filename]
if content == nil && hasfallback && len(fallback) > 0 {
var err error

View File

@ -2,7 +2,6 @@ package web
import (
"encoding/json"
"mapserver/app"
"net/http"
"github.com/sirupsen/logrus"
@ -64,14 +63,10 @@ type MinetestInfo struct {
Uptime float64 `json:"uptime"`
}
type Minetest struct {
ctx *app.App
}
var LastStats *MinetestInfo
func (this *Minetest) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
if req.Header.Get("Authorization") != this.ctx.Config.WebApi.SecretKey {
func (api *Api) PostMinetestData(resp http.ResponseWriter, req *http.Request) {
if req.Header.Get("Authorization") != api.Context.Config.WebApi.SecretKey {
resp.WriteHeader(403)
resp.Write([]byte("invalid key!"))
return
@ -94,7 +89,7 @@ func (this *Minetest) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
mintestMaxLag.Set(data.MaxLag)
LastStats = data
this.ctx.WebEventbus.Emit("minetest-info", data)
api.Context.WebEventbus.Emit("minetest-info", data)
json.NewEncoder(resp).Encode("stub")
}

View File

@ -19,6 +19,7 @@ func Serve(ctx *app.App) {
}
logrus.WithFields(fields).Info("Starting http server")
api := NewApi(ctx)
mux := http.NewServeMux()
// static files
@ -27,13 +28,13 @@ func Serve(ctx *app.App) {
tiles := &Tiles{ctx: ctx}
tiles.Init()
mux.Handle("/api/tile/", tiles)
mux.Handle("/api/config", &ConfigHandler{ctx: ctx})
mux.Handle("/api/stats", &StatsHandler{ctx: ctx})
mux.Handle("/api/media/", &MediaHandler{ctx: ctx})
mux.Handle("/api/minetest", &Minetest{ctx: ctx})
mux.Handle("/api/mapobjects/", &MapObjects{ctx: ctx})
mux.Handle("/api/colormapping", &ColorMappingHandler{ctx: ctx})
mux.Handle("/api/viewblock/", &ViewMapblockHandler{ctx: ctx})
mux.HandleFunc("/api/config", api.GetConfig)
mux.HandleFunc("/api/stats", api.GetStats)
mux.HandleFunc("/api/media/", api.GetMedia)
mux.HandleFunc("/api/minetest", api.PostMinetestData)
mux.HandleFunc("/api/mapobjects/", api.QueryMapobjects)
mux.HandleFunc("/api/colormapping", api.GetColorMapping)
mux.HandleFunc("/api/viewblock/", api.GetBlockData)
if ctx.Config.MapObjects.Areas {
mux.Handle("/api/areas", &AreasHandler{ctx: ctx})
@ -51,11 +52,11 @@ func Serve(ctx *app.App) {
if ctx.Config.WebApi.EnableMapblock {
//mapblock endpoint
mux.Handle("/api/mapblock/", &MapblockHandler{ctx: ctx})
mux.HandleFunc("/api/mapblock/", api.GetMapBlockData)
}
// main entry point
http.HandleFunc("/", mux.ServeHTTP)
err := http.ListenAndServe(":"+strconv.Itoa(ctx.Config.Port), nil)
if err != nil {
panic(err)

View File

@ -2,15 +2,10 @@ package web
import (
"encoding/json"
"mapserver/app"
"net/http"
)
type StatsHandler struct {
ctx *app.App
}
func (h *StatsHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
func (api *Api) GetStats(resp http.ResponseWriter, req *http.Request) {
resp.Header().Add("content-type", "application/json")
json.NewEncoder(resp).Encode(LastStats)
}

View File

@ -2,7 +2,6 @@ package web
import (
"encoding/json"
"mapserver/app"
"mapserver/coords"
"net/http"
"strconv"
@ -14,11 +13,7 @@ type ViewBlock struct {
ContentId []int `json:"contentid"`
}
type ViewMapblockHandler struct {
ctx *app.App
}
func (h *ViewMapblockHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
func (api *Api) GetBlockData(resp http.ResponseWriter, req *http.Request) {
str := strings.TrimPrefix(req.URL.Path, "/api/viewblock/")
parts := strings.Split(str, "/")
if len(parts) != 3 {
@ -32,7 +27,7 @@ func (h *ViewMapblockHandler) ServeHTTP(resp http.ResponseWriter, req *http.Requ
z, _ := strconv.Atoi(parts[2])
c := coords.NewMapBlockCoords(x, y, z)
mb, err := h.ctx.MapBlockAccessor.GetMapBlock(c)
mb, err := api.Context.MapBlockAccessor.GetMapBlock(c)
if err != nil {
resp.WriteHeader(500)