watchyourlan/internal/web/host.go
2023-01-02 23:36:09 +07:00

62 lines
1.2 KiB
Go

package web
import (
"html/template"
"log"
"net"
"net/http"
"strconv"
"github.com/aceberg/WatchYourLAN/internal/check"
"github.com/aceberg/WatchYourLAN/internal/db"
"github.com/aceberg/WatchYourLAN/internal/models"
)
func delHandler(w http.ResponseWriter, r *http.Request) {
idStr := r.FormValue("id")
id, err := strconv.Atoi(idStr)
check.IfError(err)
log.Println("INFO: delete host ID =", id)
db.Delete(AppConfig.DbPath, uint16(id))
http.Redirect(w, r, "/home/", 302)
}
func hostHandler(w http.ResponseWriter, r *http.Request) {
var guiData models.GuiData
var host models.Host
guiData.Config = AppConfig
guiData.Icon = Icon
idStr := r.URL.Query().Get("id")
id, err := strconv.Atoi(idStr)
check.IfError(err)
id16 := uint16(id)
for _, oneHost := range AllHosts {
if id16 == oneHost.ID {
host = oneHost
break
}
}
guiData.Hosts = append(guiData.Hosts, host)
addr, err := net.LookupAddr(host.IP)
check.IfError(err)
guiData.Themes = addr
tmpl, err := template.ParseFS(TemplHTML, TemplPath+"host.html", TemplPath+"header.html", TemplPath+"footer.html")
check.IfError(err)
err = tmpl.ExecuteTemplate(w, "header", guiData)
check.IfError(err)
err = tmpl.ExecuteTemplate(w, "host", guiData)
check.IfError(err)
}