From 46d18a018ef6358a30f9bb1c87f8cabf9581e518 Mon Sep 17 00:00:00 2001 From: Chandler Swift Date: Thu, 12 Oct 2023 22:02:59 -0500 Subject: [PATCH 1/3] Add 404 handler for not-index --- minecraft-site.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/minecraft-site.go b/minecraft-site.go index a653bee..b422606 100644 --- a/minecraft-site.go +++ b/minecraft-site.go @@ -73,6 +73,9 @@ func main() { } http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/" { + http.NotFound(w, r) + } // Update servers with current data for i, s := range config.Servers { From 8b0242419d79b78b2ab5e9eadefa189f41358200 Mon Sep 17 00:00:00 2001 From: Chandler Swift Date: Thu, 12 Oct 2023 22:04:18 -0500 Subject: [PATCH 2/3] Add 404 for nonexistent pages --- minecraft-site.go | 1 + 1 file changed, 1 insertion(+) diff --git a/minecraft-site.go b/minecraft-site.go index b422606..0d03fd8 100644 --- a/minecraft-site.go +++ b/minecraft-site.go @@ -75,6 +75,7 @@ func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { http.NotFound(w, r) + return } // Update servers with current data From 4761cfbd1cb0caa2374fb7d125c04de9c3c8433b Mon Sep 17 00:00:00 2001 From: Chandler Swift Date: Thu, 12 Oct 2023 22:04:26 -0500 Subject: [PATCH 3/3] Add basic JSON API --- minecraft-site.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/minecraft-site.go b/minecraft-site.go index 0d03fd8..2b4bd64 100644 --- a/minecraft-site.go +++ b/minecraft-site.go @@ -93,6 +93,23 @@ func main() { } }) + http.HandleFunc("/data.json", func(w http.ResponseWriter, r *http.Request) { + // Update servers with current data + for i, s := range config.Servers { + // TODO: Query instead (opportunistically?) to get mod lists, etc + config.Servers[i].Status, err = minequery.Ping17(s.Host, s.Port) + if err != nil { + log.Printf("Error querying server: %v", err) + } + } + msg, err := json.Marshal(config.Servers) + if err != nil { + http.Error(w, err.Error(), 500) + } + w.Header().Set("Content-Type", "application/json") + w.Write(msg) + }) + // Serve backup directory if serveBackups { // TODO: add HTML http.Handle("/backups/", http.StripPrefix("/backups/", http.FileServer(http.Dir(config.BackupDir))))