From 409274759f94b24d3cb10fd2626c3a055a78d9c7 Mon Sep 17 00:00:00 2001 From: aceberg <1502200+aceberg@users.noreply.github.com> Date: Fri, 23 Aug 2024 13:59:32 +0700 Subject: [PATCH] Host page --- internal/web/api.go | 11 ++++++++- internal/web/{helpers.go => functions.go} | 0 internal/web/host.go | 23 +++++++++++++++++++ internal/web/public/js/filter.js | 5 ++++ internal/web/public/js/index.js | 2 +- internal/web/public/js/sort.js | 28 ++++++++++++++++++----- internal/web/templates/host.html | 28 +++++++++++++++++++++++ internal/web/templates/index.html | 1 + internal/web/webgui.go | 2 ++ 9 files changed, 92 insertions(+), 8 deletions(-) rename internal/web/{helpers.go => functions.go} (100%) create mode 100644 internal/web/host.go create mode 100644 internal/web/templates/host.html diff --git a/internal/web/api.go b/internal/web/api.go index 623ea7e..a4ef539 100644 --- a/internal/web/api.go +++ b/internal/web/api.go @@ -22,6 +22,15 @@ func apiHistory(c *gin.Context) { c.IndentedJSON(http.StatusOK, histHosts) } +func apiHost(c *gin.Context) { + + idStr := c.Param("id") + id, _ := strconv.Atoi(idStr) + host := getHostByID(id, allHosts) // functions.go + + c.IndentedJSON(http.StatusOK, host) +} + func apiEdit(c *gin.Context) { idStr := c.Param("id") @@ -30,7 +39,7 @@ func apiEdit(c *gin.Context) { id, _ := strconv.Atoi(idStr) - host := getHostByID(id, allHosts) + host := getHostByID(id, allHosts) // functions.go if host.Date != "" { host.Name = name diff --git a/internal/web/helpers.go b/internal/web/functions.go similarity index 100% rename from internal/web/helpers.go rename to internal/web/functions.go diff --git a/internal/web/host.go b/internal/web/host.go new file mode 100644 index 0000000..95b8b43 --- /dev/null +++ b/internal/web/host.go @@ -0,0 +1,23 @@ +package web + +import ( + "log" + "net/http" + + "github.com/gin-gonic/gin" + + "github.com/aceberg/WatchYourLAN/internal/models" +) + +func hostHandler(c *gin.Context) { + var guiData models.GuiData + guiData.Config = appConfig + + idStr := c.Param("id") + log.Println("ID =", idStr) + + guiData.Version = idStr + + c.HTML(http.StatusOK, "header.html", guiData) + c.HTML(http.StatusOK, "host.html", guiData) +} diff --git a/internal/web/public/js/filter.js b/internal/web/public/js/filter.js index 6760111..f29aa7e 100644 --- a/internal/web/public/js/filter.js +++ b/internal/web/public/js/filter.js @@ -23,5 +23,10 @@ function filterFunc(field, value) { oldFilter = field; + displayArrayData(addrsArray); +} + +function resetFilter() { + addrsArray = bkpArray; displayArrayData(addrsArray); } \ No newline at end of file diff --git a/internal/web/public/js/index.js b/internal/web/public/js/index.js index 196a741..d48c548 100644 --- a/internal/web/public/js/index.js +++ b/internal/web/public/js/index.js @@ -28,7 +28,7 @@ function createHTML(addr, i) { ${addr.IP} - ${addr.Mac} + ${addr.Mac} ${addr.Hw} ${addr.Date} diff --git a/internal/web/public/js/sort.js b/internal/web/public/js/sort.js index 1f06271..8bdbd61 100644 --- a/internal/web/public/js/sort.js +++ b/internal/web/public/js/sort.js @@ -15,15 +15,31 @@ function sortByAny(someArray, field) { // console.log("Field =", field); if (field != oldField) { - someArray.sort(byFieldDown(field)); + + if (field == 'IP') { + someArray.sort((a, b) => { + const num1 = Number(a.IP.split(".").map((num) => (`000${num}`).slice(-3) ).join("")); + const num2 = Number(b.IP.split(".").map((num) => (`000${num}`).slice(-3) ).join("")); + return num1-num2; + }); + } else { + someArray.sort(byFieldDown(field)); + } + oldField = field; } else { - someArray.sort(byFieldUp(field)); - oldField = ''; - } + + if (field == 'IP') { + someArray.sort((a, b) => { + const num1 = Number(a.IP.split(".").map((num) => (`000${num}`).slice(-3) ).join("")); + const num2 = Number(b.IP.split(".").map((num) => (`000${num}`).slice(-3) ).join("")); + return num2-num1; + }); + } else { + someArray.sort(byFieldUp(field)); + } - if (field == 'State') { - someArray.sort(byFieldUp('Watch')); + oldField = ''; } displayArrayData(someArray); diff --git a/internal/web/templates/host.html b/internal/web/templates/host.html new file mode 100644 index 0000000..f0859fc --- /dev/null +++ b/internal/web/templates/host.html @@ -0,0 +1,28 @@ +{{ define "host.html" }} + + +
+
+
+
+
Host
+
+ + +
+
+
+
+ +
+
+
Port scan
+
+
+
+
+
+
+ +{{ template "footer.html" }} +{{ end }} \ No newline at end of file diff --git a/internal/web/templates/index.html b/internal/web/templates/index.html index dcfef84..9446de2 100644 --- a/internal/web/templates/index.html +++ b/internal/web/templates/index.html @@ -28,6 +28,7 @@
  • Online
  • Offline
  • + diff --git a/internal/web/webgui.go b/internal/web/webgui.go index e29a66d..36d01d4 100644 --- a/internal/web/webgui.go +++ b/internal/web/webgui.go @@ -44,10 +44,12 @@ func Gui(dirPath, nodePath string) { router.GET("/api/all", apiAll) // api.go router.GET("/api/history", apiHistory) // api.go + router.GET("/api/host", apiHost) // api.go router.GET("/api/edit/:id/:name/*known", apiEdit) // api.go router.GET("/", indexHandler) // index.go router.GET("/history/", historyHandler) // index.go + router.GET("/host/:id", hostHandler) // host.go router.GET("/config/", configHandler) // config.go router.POST("/config/", saveConfigHandler) // config.go