From 0d270e8e23a4aaa92db0d7005a4c422196b8cfb2 Mon Sep 17 00:00:00 2001 From: aceberg <1502200+aceberg@users.noreply.github.com> Date: Wed, 19 Jul 2023 17:40:44 +0700 Subject: [PATCH] Release 1.0.5 --- CHANGELOG.md | 6 ++++- internal/web/file.go | 40 ++++++++++++++++++++++++++++++ internal/web/scan-uptime.go | 8 +++++- internal/web/scan.go | 19 +++++++++----- internal/web/templates/file.html | 17 +++++++++++++ internal/web/templates/header.html | 1 + internal/web/templates/version | 2 +- internal/web/webgui.go | 1 + 8 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 internal/web/file.go create mode 100644 internal/web/templates/file.html diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b11399..17dff0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,14 +2,18 @@ # Change Log All notable changes to this project will be documented in this file. -## [1.0.5] - 2023-07- +## [1.0.5] - 2023-07-19 ### Added - Uptime: split date and time - Uptime: filter by date, address, port, notification +- Edit board file online ### Changed - Connection timeout to 3 seconds +### Fixed +- Error: concurrent map read and map write + ## [1.0.4] - 2023-07-14 ### Added - Uptime: color Date diff --git a/internal/web/file.go b/internal/web/file.go new file mode 100644 index 0000000..2c0bbf4 --- /dev/null +++ b/internal/web/file.go @@ -0,0 +1,40 @@ +package web + +import ( + "net/http" + "os" + + "github.com/aceberg/miniboard/internal/check" + "github.com/aceberg/miniboard/internal/models" + "github.com/aceberg/miniboard/internal/yaml" +) + +func fileHandler(w http.ResponseWriter, r *http.Request) { + var guiData models.GuiData + guiData.Config = AppConfig + guiData.CurrentTab = "Edit board file" + guiData.Links = AllLinks + + text := r.FormValue("text") + + file, err := os.ReadFile(AppConfig.YamlPath) + check.IfError(err) + guiData.Version = string(file) + + if text != "" { + err := os.WriteFile(AppConfig.YamlPath, []byte(text), 0644) + check.IfError(err) + + AllLinks = yaml.Read(AppConfig.YamlPath) + assignAllIDs() // assign-IDs.go + + close(AppConfig.Quit) + AppConfig.Quit = make(chan bool) + + go scanPorts(AppConfig.Quit) // scan.go + + http.Redirect(w, r, r.Header.Get("Referer"), 302) + } + + execTemplate(w, "file", guiData) +} diff --git a/internal/web/scan-uptime.go b/internal/web/scan-uptime.go index f945238..be67629 100644 --- a/internal/web/scan-uptime.go +++ b/internal/web/scan-uptime.go @@ -14,6 +14,8 @@ import ( var ( // RetriesSyncMap - count retries to send notifications RetriesSyncMap sync.Map + // MuUptime - mutex for AllLinks.Uptime + MuUptime sync.Mutex ) func appendUptimeMon(panelName string, host models.Host, notif bool) { @@ -28,7 +30,9 @@ func appendUptimeMon(panelName string, host models.Host, notif bool) { mon.Color = check.Color(mon.Date) mon.State = host.State if notif { + MuUptime.Lock() mon.Notify = AllLinks.Uptime.Panels[panelName].Notify + MuUptime.Unlock() } UptimeMon = append(UptimeMon, mon) } @@ -37,9 +41,11 @@ func scanUptime(panelName string, host models.Host, oldState bool) { var retries int var notifEnabled, notif bool + MuUptime.Lock() panel, exists := AllLinks.Uptime.Panels[panelName] + MuUptime.Unlock() - if AllLinks.Uptime.Enabled && exists { + if exists { if len(panel.Notify) > 0 { retriesAny, ok := RetriesSyncMap.LoadOrStore(panelName+host.Name, 0) if ok { diff --git a/internal/web/scan.go b/internal/web/scan.go index bb5dd03..707c7ae 100644 --- a/internal/web/scan.go +++ b/internal/web/scan.go @@ -11,8 +11,8 @@ import ( ) var ( - // Mu - mutex to prevent concurrent map writes - Mu sync.Mutex + // MuScan - mutex for AllLinks.Panels + MuScan sync.Mutex ) func scanPorts(quit chan bool) { @@ -23,7 +23,10 @@ func scanPorts(quit chan bool) { case <-quit: return default: - for name := range AllLinks.Panels { + MuScan.Lock() + panels := AllLinks.Panels + MuScan.Unlock() + for name := range panels { _, exists := alreadyScanning[name] if !exists { go scanPanel(name, quit) @@ -42,7 +45,9 @@ func scanPanel(panelName string, quit chan bool) { case <-quit: return default: + MuScan.Lock() panel, exists := AllLinks.Panels[panelName] + MuScan.Unlock() if !exists { return } @@ -56,12 +61,14 @@ func scanPanel(panelName string, quit chan bool) { host.State = check.State(host) hosts[key] = host - scanUptime(panelName, host, oldState) // scan-uptime.go + if AllLinks.Uptime.Enabled { + scanUptime(panelName, host, oldState) // scan-uptime.go + } } panel.Hosts = hosts - Mu.Lock() + MuScan.Lock() AllLinks.Panels[panelName] = panel - Mu.Unlock() + MuScan.Unlock() } timeout, err := strconv.Atoi(panel.Timeout) diff --git a/internal/web/templates/file.html b/internal/web/templates/file.html new file mode 100644 index 0000000..2d324ba --- /dev/null +++ b/internal/web/templates/file.html @@ -0,0 +1,17 @@ +{{ define "file" }} + + +
+
+
+
+
+ + +
+
+
+
+ +{{ template "footer" }} +{{ end }} \ No newline at end of file diff --git a/internal/web/templates/header.html b/internal/web/templates/header.html index aeb723e..55b21f3 100644 --- a/internal/web/templates/header.html +++ b/internal/web/templates/header.html @@ -54,6 +54,7 @@
  • Edit tabs
  • Edit uptime
  • +
  • Edit board file
  • Reload
  • About
  • diff --git a/internal/web/templates/version b/internal/web/templates/version index 6b82dc6..73b0cec 100644 --- a/internal/web/templates/version +++ b/internal/web/templates/version @@ -1 +1 @@ -VERSION=1.0.4 \ No newline at end of file +VERSION=1.0.5 \ No newline at end of file diff --git a/internal/web/webgui.go b/internal/web/webgui.go index 5f6c52f..5a23750 100644 --- a/internal/web/webgui.go +++ b/internal/web/webgui.go @@ -35,6 +35,7 @@ func Gui(confPath, yamlPath, nodePath string) { http.HandleFunc("/", indexHandler) // index.go http.HandleFunc("/config/", configHandler) // config.go http.HandleFunc("/config_save/", saveConfigHandler) // config.go + http.HandleFunc("/file/", fileHandler) // file.go http.HandleFunc("/host/", hostHandler) // host.go http.HandleFunc("/panels/", panelsHandler) // panels.go http.HandleFunc("/panel_edit/", panelEditHandler) // panel-edit.go