From 57ebd896dab7daae5f5227648387b3ac78ef816b Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Mon, 12 Apr 2021 13:55:00 +0200 Subject: [PATCH] use --- web/colormapping.go | 9 ++------- web/config.go | 14 +++++--------- web/mapblock.go | 9 ++------- web/mapobjects.go | 9 ++------- web/media.go | 9 ++------- web/minetest.go | 11 +++-------- web/serve.go | 19 ++++++++++--------- web/stats.go | 7 +------ web/viewblock.go | 9 ++------- 9 files changed, 29 insertions(+), 67 deletions(-) diff --git a/web/colormapping.go b/web/colormapping.go index 2a7ba09..476239c 100644 --- a/web/colormapping.go +++ b/web/colormapping.go @@ -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} } diff --git a/web/config.go b/web/config.go index 0e753f1..d4737e1 100644 --- a/web/config.go +++ b/web/config.go @@ -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) } diff --git a/web/mapblock.go b/web/mapblock.go index 152a29a..3d8decb 100644 --- a/web/mapblock.go +++ b/web/mapblock.go @@ -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) diff --git a/web/mapobjects.go b/web/mapobjects.go index 2164cc9..549932a 100644 --- a/web/mapobjects.go +++ b/web/mapobjects.go @@ -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())) diff --git a/web/media.go b/web/media.go index b1bdfae..9b8e647 100644 --- a/web/media.go +++ b/web/media.go @@ -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 diff --git a/web/minetest.go b/web/minetest.go index ee142bd..28f9d6b 100644 --- a/web/minetest.go +++ b/web/minetest.go @@ -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") } diff --git a/web/serve.go b/web/serve.go index 0463724..ed28ad4 100644 --- a/web/serve.go +++ b/web/serve.go @@ -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) diff --git a/web/stats.go b/web/stats.go index 13b88cf..e5f1ad3 100644 --- a/web/stats.go +++ b/web/stats.go @@ -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) } diff --git a/web/viewblock.go b/web/viewblock.go index 74256f2..ad998a8 100644 --- a/web/viewblock.go +++ b/web/viewblock.go @@ -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)