From d67af210dad10cfe9b9169efb0a4a920afa36ac3 Mon Sep 17 00:00:00 2001 From: bugbounce <35751394+bugbounce@users.noreply.github.com> Date: Fri, 21 Oct 2022 21:44:08 +0530 Subject: [PATCH] Add delete functionality for logged hosts --- src/db-edit.go | 10 +++++++++- src/main.go | 8 ++++++-- src/templates/index.html | 4 +++- src/web-index.go | 16 +++++++++++++++- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/db-edit.go b/src/db-edit.go index b7f811e..7bc7c7b 100644 --- a/src/db-edit.go +++ b/src/db-edit.go @@ -47,7 +47,15 @@ func db_update(oneHost Host) { db_exec(sqlStatement) } +func db_delete(hostId int) { + sqlStatement := `DELETE FROM "now" WHERE ID = '%d';` + sqlStatement = fmt.Sprintf(sqlStatement, hostId) + //fmt.Println("Delete statement:", sqlStatement) + db_exec(sqlStatement) +} + + func db_setnow() { sqlStatement := `UPDATE "now" set NOW = '0';` db_exec(sqlStatement) -} \ No newline at end of file +} diff --git a/src/main.go b/src/main.go index e64c974..8441584 100644 --- a/src/main.go +++ b/src/main.go @@ -37,11 +37,15 @@ func scan_and_compare() { db_setnow() // Mark hosts in DB as offline hosts_compare(foundHosts, dbHosts) // Compare hosts online and in DB // and add them to DB - AllHosts = db_select() + update_all_hosts() time.Sleep(time.Duration(AppConfig.Timeout) * time.Second) // Timeout } } +func update_all_hosts(){ + AllHosts = db_select() +} + func main() { AllHosts = []Host{} AppConfig = get_config() // Get config from Defaults, Config file, Env @@ -51,4 +55,4 @@ func main() { go scan_and_compare() webgui() // Start web GUI -} \ No newline at end of file +} diff --git a/src/templates/index.html b/src/templates/index.html index 43d5abc..6a48255 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -45,6 +45,7 @@ Hardware Last seen Known + Delete {{ range .Hosts }}
@@ -63,6 +64,7 @@ {{ else }} {{ end }} +
@@ -71,4 +73,4 @@ {{ template "footer" }} -{{ end }} \ No newline at end of file +{{ end }} diff --git a/src/web-index.go b/src/web-index.go index e026ada..a4fc460 100644 --- a/src/web-index.go +++ b/src/web-index.go @@ -48,6 +48,19 @@ func update_host(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, r.Header.Get("Referer"), 302) } +func delete_host( w http.ResponseWriter, r *http.Request) { + + idStr := r.FormValue("id") + if idStr == "" { + fmt.Fprintf(w, "No data!") + } else { + id, _ := strconv.Atoi(idStr) + db_delete(id) + update_all_hosts() + } + + http.Redirect(w, r, r.Header.Get("Referer"), 302) +} func webgui() { // fmt.Println(FoundHosts) address := AppConfig.GuiIP + ":" + AppConfig.GuiPort @@ -64,5 +77,6 @@ func webgui() { http.HandleFunc("/sort_hosts/", sort_hosts) http.HandleFunc("/theme/", theme) http.HandleFunc("/update_host/", update_host) + http.HandleFunc("/delete_host/", delete_host) http.ListenAndServe(address, nil) -} \ No newline at end of file +}