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 ( import (
"encoding/json" "encoding/json"
"mapserver/app"
"net/http" "net/http"
) )
type ColorMappingHandler struct {
ctx *app.App
}
type Color struct { type Color struct {
R uint8 `json:"r"` R uint8 `json:"r"`
G uint8 `json:"g"` G uint8 `json:"g"`
B uint8 `json:"b"` 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) 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} 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"` EnableSearch bool `json:"enablesearch"`
} }
type ConfigHandler struct { func (api *Api) GetConfig(resp http.ResponseWriter, req *http.Request) {
ctx *app.App
}
func (h *ConfigHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
resp.Header().Add("content-type", "application/json") resp.Header().Add("content-type", "application/json")
webcfg := PublicConfig{} webcfg := PublicConfig{}
webcfg.Layers = h.ctx.Config.Layers webcfg.Layers = api.Context.Config.Layers
webcfg.MapObjects = h.ctx.Config.MapObjects webcfg.MapObjects = api.Context.Config.MapObjects
webcfg.Version = app.Version webcfg.Version = app.Version
webcfg.DefaultOverlays = h.ctx.Config.DefaultOverlays webcfg.DefaultOverlays = api.Context.Config.DefaultOverlays
webcfg.EnableSearch = h.ctx.Config.EnableSearch webcfg.EnableSearch = api.Context.Config.EnableSearch
json.NewEncoder(resp).Encode(webcfg) json.NewEncoder(resp).Encode(webcfg)
} }

View File

@ -2,18 +2,13 @@ package web
import ( import (
"encoding/json" "encoding/json"
"mapserver/app"
"mapserver/coords" "mapserver/coords"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
) )
type MapblockHandler struct { func (api *Api) GetMapBlockData(resp http.ResponseWriter, req *http.Request) {
ctx *app.App
}
func (h *MapblockHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
str := strings.TrimPrefix(req.URL.Path, "/api/mapblock/") str := strings.TrimPrefix(req.URL.Path, "/api/mapblock/")
parts := strings.Split(str, "/") parts := strings.Split(str, "/")
if len(parts) != 3 { if len(parts) != 3 {
@ -27,7 +22,7 @@ func (h *MapblockHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request)
z, _ := strconv.Atoi(parts[2]) z, _ := strconv.Atoi(parts[2])
c := coords.NewMapBlockCoords(x, y, z) c := coords.NewMapBlockCoords(x, y, z)
mb, err := h.ctx.MapBlockAccessor.GetMapBlock(c) mb, err := api.Context.MapBlockAccessor.GetMapBlock(c)
if err != nil { if err != nil {
resp.WriteHeader(500) resp.WriteHeader(500)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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