From 4231be5e124fceb9cc27e8a6b21fd7351ebffc9a Mon Sep 17 00:00:00 2001 From: Thomas Rudin Date: Sun, 3 Feb 2019 18:04:01 +0100 Subject: [PATCH] route mt updates to frontend --- mapserver_mod/mapserver/bridge.lua | 2 -- server/static/js/WebSocketChannel.js | 2 +- server/static/js/main.js | 4 ++++ server/web/minetest.go | 14 +++++++++----- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/mapserver_mod/mapserver/bridge.lua b/mapserver_mod/mapserver/bridge.lua index 039b227..a66637c 100644 --- a/mapserver_mod/mapserver/bridge.lua +++ b/mapserver_mod/mapserver/bridge.lua @@ -39,8 +39,6 @@ function send_stats() table.insert(data.players, info) end - print(minetest.write_json(data)) --XXX - http.fetch({ url = url .. "/api/minetest", extra_headers = { "Content-Type: application/json", "Authorization: " .. key }, diff --git a/server/static/js/WebSocketChannel.js b/server/static/js/WebSocketChannel.js index 5d49d19..42adbec 100644 --- a/server/static/js/WebSocketChannel.js +++ b/server/static/js/WebSocketChannel.js @@ -25,7 +25,7 @@ WebSocketChannel.prototype.connect = function(){ var listeners = self.listenerMap[event.type]; if (listeners){ - self.listeners.forEach(function(listener){ + listeners.forEach(function(listener){ listener(event.data); }); } diff --git a/server/static/js/main.js b/server/static/js/main.js index 699c3a2..4dc35a7 100644 --- a/server/static/js/main.js +++ b/server/static/js/main.js @@ -5,6 +5,10 @@ api.getConfig().then(function(cfg){ var wsChannel = new WebSocketChannel(); wsChannel.connect(); + wsChannel.addListener("minetest-info", function(e){ + console.log(e); + }); + var rtTiles = new RealtimeTileLayer(wsChannel); var initialZoom = 11; diff --git a/server/web/minetest.go b/server/web/minetest.go index 5dd9b0f..ebf13cc 100644 --- a/server/web/minetest.go +++ b/server/web/minetest.go @@ -4,8 +4,6 @@ import ( "encoding/json" "mapserver/app" "net/http" - - "github.com/sirupsen/logrus" ) type PlayerPos struct { @@ -33,19 +31,25 @@ type Minetest struct { ctx *app.App } -func (t *Minetest) ServeHTTP(resp http.ResponseWriter, req *http.Request) { +func (this *Minetest) ServeHTTP(resp http.ResponseWriter, req *http.Request) { + if req.Header.Get("Authorization") != this.ctx.Config.WebApi.SecretKey { + resp.WriteHeader(403) + resp.Write([]byte("invalid key!")) + return + } + resp.Header().Add("content-type", "application/json") data := &MinetestInfo{} err := json.NewDecoder(req.Body).Decode(data) - logrus.Info(data) if err != nil { resp.WriteHeader(500) resp.Write([]byte(err.Error())) - logrus.Warn(err) return } + this.ctx.WebEventbus.Emit("minetest-info", data) + json.NewEncoder(resp).Encode("stub") }