watchyourlan/src/web-index.go
2022-09-05 23:12:04 +07:00

67 lines
No EOL
1.5 KiB
Go

package main
import (
"fmt"
"net/http"
"html/template"
"strconv"
)
func index(w http.ResponseWriter, r *http.Request) {
type allData struct {
Config Conf
Hosts []Host
}
var guiData allData
guiData.Config = AppConfig
guiData.Hosts = AllHosts
tmpl, _ := template.ParseFiles("templates/index.html", "templates/header.html", "templates/footer.html")
tmpl.ExecuteTemplate(w, "index", guiData)
}
func update_host(w http.ResponseWriter, r *http.Request) {
idStr := r.FormValue("id")
name := r.FormValue("name")
knownStr := r.FormValue("known")
if idStr == "" {
fmt.Fprintf(w, "No data!")
} else {
var known uint16
id, _ := strconv.Atoi(idStr)
known = 0
if knownStr == "on" {
known = 1
}
for i, oneHost := range AllHosts {
if oneHost.Id == uint16(id) {
AllHosts[i].Name = name
AllHosts[i].Known = known
db_update(AllHosts[i])
}
}
}
http.Redirect(w, r, r.Header.Get("Referer"), 302)
}
func webgui() {
// fmt.Println(FoundHosts)
address := AppConfig.GuiIP + ":" + AppConfig.GuiPort
fmt.Println("\n=================================== ")
fmt.Println(fmt.Sprintf("Web GUI at http://%s", address))
fmt.Println("=================================== \n")
http.HandleFunc("/", index)
http.HandleFunc("/home/", home)
http.HandleFunc("/offline/", offline)
http.HandleFunc("/online/", online)
http.HandleFunc("/search_hosts/", search_hosts)
http.HandleFunc("/sort_hosts/", sort_hosts)
http.HandleFunc("/theme/", theme)
http.HandleFunc("/update_host/", update_host)
http.ListenAndServe(address, nil)
}