Add delete functionality for logged hosts

This commit is contained in:
bugbounce 2022-10-21 21:44:08 +05:30
parent 0c49949a5d
commit d67af210da
4 changed files with 33 additions and 5 deletions

View file

@ -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)
}
}

View file

@ -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
}
}

View file

@ -45,6 +45,7 @@
<th>Hardware</th>
<th>Last seen</th>
<th>Known</th>
<th>Delete</th>
</tr>
{{ range .Hosts }}
<form action="/update_host/" method="post">
@ -63,6 +64,7 @@
{{ else }}
<button value="on" name="known" type="submit" class="btn btn-warning">No</button>
{{ end }}
<td> <button name="delete" type="submit" class="btn btn-warning" formaction="/delete_host/" method="post">Delete</button>
</td>
</tr>
</form>
@ -71,4 +73,4 @@
</div>
{{ template "footer" }}
{{ end }}
{{ end }}

View file

@ -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)
}
}