diff --git a/src/getconfig.go b/src/getconfig.go index 24f8271..cb80354 100644 --- a/src/getconfig.go +++ b/src/getconfig.go @@ -4,7 +4,9 @@ import ( "github.com/spf13/viper" ) -func get_config(path string) (config Conf) { +const configPath = "/data/config" + +func get_config() (config Conf) { viper.SetDefault("IFACE", "enp1s0") viper.SetDefault("DBPATH", "/data/db.sqlite") viper.SetDefault("GUIIP", "localhost") @@ -13,7 +15,7 @@ func get_config(path string) (config Conf) { viper.SetDefault("SHOUTRRR_URL", "") viper.SetDefault("THEME", "solar") - viper.SetConfigFile(path) + viper.SetConfigFile(configPath) viper.SetConfigType("env") viper.ReadInConfig() @@ -28,4 +30,11 @@ func get_config(path string) (config Conf) { config.Theme = viper.Get("THEME").(string) return config +} + +func write_config() { + viper.SetConfigFile(configPath) + viper.SetConfigType("env") + viper.Set("THEME", AppConfig.Theme) + viper.WriteConfig() } \ No newline at end of file diff --git a/src/main.go b/src/main.go index be3c1ed..e64c974 100644 --- a/src/main.go +++ b/src/main.go @@ -44,7 +44,7 @@ func scan_and_compare() { func main() { AllHosts = []Host{} - AppConfig = get_config("/data/config") // Get config from Defaults, Config file, Env + AppConfig = get_config() // Get config from Defaults, Config file, Env db_create() // Check if DB exists. Create if not diff --git a/src/templates/footer.html b/src/templates/footer.html index 0032e6a..5dd8ca7 100644 --- a/src/templates/footer.html +++ b/src/templates/footer.html @@ -1,9 +1,4 @@ {{ define "footer"}} - {{ end }} \ No newline at end of file diff --git a/src/templates/header.html b/src/templates/header.html index 03471c2..393fb45 100644 --- a/src/templates/header.html +++ b/src/templates/header.html @@ -5,5 +5,62 @@ - + + + + + + + + + {{ end }} \ No newline at end of file diff --git a/src/templates/index.html b/src/templates/index.html index 36ecd0c..43d5abc 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -22,7 +22,7 @@ {{ if eq .Now 1 }} {{ .Name }} - {{ .Ip }} + {{ .Ip }} {{ .Mac }} {{ .Hw }} diff --git a/src/templates/offline.html b/src/templates/offline.html new file mode 100644 index 0000000..c3c3f68 --- /dev/null +++ b/src/templates/offline.html @@ -0,0 +1,74 @@ +{{ define "offline"}} + +{{ template "header" }} + + + + + + +
+

Offline

+ + + + + + + + + + {{ range .Hosts }} + + + + + + + + + + + + {{ end }} +
Name
+ + +
Ip
+ + +
MacHardwareLast seen
+ + +
Known
+ + +
+ + {{ .Ip }}{{ .Mac }}{{ .Hw }}{{ .Date }} + {{ if eq .Known 1 }} + + {{ else }} + + {{ end }} +
+
+ +{{ template "footer" }} +{{ end }} \ No newline at end of file diff --git a/src/index.go b/src/web-index.go similarity index 89% rename from src/index.go rename to src/web-index.go index fac63f5..cebbd0a 100644 --- a/src/index.go +++ b/src/web-index.go @@ -56,6 +56,10 @@ func webgui() { fmt.Println("=================================== \n") http.HandleFunc("/", index) + http.HandleFunc("/offline/", offline) + http.HandleFunc("/online/", online) + http.HandleFunc("/sort_hosts/", sort_hosts) + http.HandleFunc("/theme/", theme) http.HandleFunc("/update_host/", update_host) http.ListenAndServe(address, nil) } \ No newline at end of file diff --git a/src/web-onoffline.go b/src/web-onoffline.go new file mode 100644 index 0000000..a2ea686 --- /dev/null +++ b/src/web-onoffline.go @@ -0,0 +1,44 @@ +package main + +import ( + "net/http" + "html/template" +) + +func offline(w http.ResponseWriter, r *http.Request) { + type allData struct { + Config Conf + Hosts []Host + } + var guiData allData + guiData.Config = AppConfig + guiData.Hosts = []Host{} + + for _, oneHost := range AllHosts { + if oneHost.Now == 0 { + guiData.Hosts = append(guiData.Hosts, oneHost) + } + } + + tmpl, _ := template.ParseFiles("templates/offline.html", "templates/header.html", "templates/footer.html") + tmpl.ExecuteTemplate(w, "offline", guiData) +} + +func online(w http.ResponseWriter, r *http.Request) { + type allData struct { + Config Conf + Hosts []Host + } + var guiData allData + guiData.Config = AppConfig + guiData.Hosts = []Host{} + + for _, oneHost := range AllHosts { + if oneHost.Now == 1 { + guiData.Hosts = append(guiData.Hosts, oneHost) + } + } + + tmpl, _ := template.ParseFiles("templates/offline.html", "templates/header.html", "templates/footer.html") + tmpl.ExecuteTemplate(w, "offline", guiData) +} \ No newline at end of file diff --git a/src/web-sort.go b/src/web-sort.go new file mode 100644 index 0000000..f4aae27 --- /dev/null +++ b/src/web-sort.go @@ -0,0 +1,50 @@ +package main + +import ( + "net/http" + "sort" +) + +func sort_hosts(w http.ResponseWriter, r *http.Request) { + + sort_method := r.FormValue("sort_method") + + switch sort_method { + case "name-up": + sort.SliceStable(AllHosts, func(i, j int) bool { + return AllHosts[i].Name < AllHosts[j].Name + }) + case "name-down": + sort.SliceStable(AllHosts, func(i, j int) bool { + return AllHosts[i].Name > AllHosts[j].Name + }) + case "ip-up": + sort.SliceStable(AllHosts, func(i, j int) bool { + return AllHosts[i].Ip < AllHosts[j].Ip + }) + case "ip-down": + sort.SliceStable(AllHosts, func(i, j int) bool { + return AllHosts[i].Ip > AllHosts[j].Ip + }) + case "date-up": + sort.SliceStable(AllHosts, func(i, j int) bool { + return AllHosts[i].Date < AllHosts[j].Date + }) + case "date-down": + sort.SliceStable(AllHosts, func(i, j int) bool { + return AllHosts[i].Date > AllHosts[j].Date + }) + case "known-up": + sort.SliceStable(AllHosts, func(i, j int) bool { + return AllHosts[i].Known < AllHosts[j].Known + }) + case "known-down": + sort.SliceStable(AllHosts, func(i, j int) bool { + return AllHosts[i].Known > AllHosts[j].Known + }) + default: + AllHosts = db_select() + } + + http.Redirect(w, r, "/", http.StatusSeeOther) +} \ No newline at end of file diff --git a/src/web-theme.go b/src/web-theme.go new file mode 100644 index 0000000..a29ea65 --- /dev/null +++ b/src/web-theme.go @@ -0,0 +1,21 @@ +package main + +import ( + "net/http" + "html" + "strings" +) + +func theme(w http.ResponseWriter, r *http.Request) { + + if r.Method == "GET" { + urlString := html.EscapeString(r.URL.Path) + tags := strings.Split(urlString, "/") + oneTheme := tags[2] + + AppConfig.Theme = oneTheme + write_config() + } + + http.Redirect(w, r, "/", http.StatusSeeOther) +} \ No newline at end of file