Sort buttons
This commit is contained in:
parent
5c98d46129
commit
3cc3bf27ed
10 changed files with 264 additions and 10 deletions
|
|
@ -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()
|
||||
}
|
||||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,4 @@
|
|||
{{ define "footer"}}
|
||||
<div class="navbar fixed-bottom">
|
||||
<div class="container">
|
||||
<a href="https://github.com/aceberg/WatchYourLAN">Github</a>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
{{ end }}
|
||||
|
|
@ -5,5 +5,62 @@
|
|||
<meta charset="utf-8">
|
||||
<!--Favicon-->
|
||||
<link href="data:image/x-icon;base64,AAABAAEAEBAQAAEABAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAA/6k7AP+uSQD/z50A7urjAD85MgAAOO8AWlpaAADIawAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADEREQAAAAAzEREREQAAAzEREREREAAzFxEREVERADN3d3VVVREDM3d3dYVlERMzd3d1VVUREzM3ERESERETMzcRESERERMzNxESMREREzM3ESNEQREwMzcSMxEREwAzNyM0REQzAAMzMzMzMzAAADMzMzMzAAAAADMzMwAAD4HwAA4AcAAMADAACAAQAAgAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIABAACAAQAAwAMAAOAHAAD4HwAA" rel="icon" type="image/x-icon" />
|
||||
|
||||
<!-- Bootstrap and theme -->
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootswatch@5.2.0/dist/{{ .Config.Theme }}/bootstrap.min.css" rel="stylesheet">
|
||||
<!-- Font for icons -->
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.9.1/font/bootstrap-icons.css">
|
||||
<!-- JavaScript Bundle with Popper -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script>
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-sm navbar-dark bg-primary">
|
||||
<div class="container">
|
||||
<ul class="navbar-nav">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" href="/">All</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" href="/online/">Online</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" href="/offline/">Offline</a>
|
||||
</li>
|
||||
<li class="nav-item dropdown">
|
||||
<a class="nav-link active dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown">Theme</a>
|
||||
<ul class="dropdown-menu">
|
||||
<li><a class="dropdown-item" href="/theme/cerulean">Cerulean</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/cosmo">Cosmo</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/cyborg">Cyborg</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/darkly">Darkly</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/flatly">Flatly</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/journal">Journal</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/litera">Litera</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/lumen">Lumen</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/lux">Lux</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/materia">Materia</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/minty">Minty</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/morph">Morph</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/pulse">Pulse</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/quartz">Quartz</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/sandstone">Sandstone</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/simplex">Simplex</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/sketchy">Sketchy</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/slate">Slate</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/solar">Solar</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/spacelab">Spacelab</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/superhero">Superhero</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/united">United</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/vapor">Vapor</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/yeti">Yeti</a></li>
|
||||
<li><a class="dropdown-item" href="/theme/zephyr">Zephyr</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
<li class="nav-item"><a class="nav-link active" href="https://github.com/aceberg/WatchYourLAN">
|
||||
<h3> <i class="bi bi-github"></i></h3>
|
||||
</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</nav>
|
||||
{{ end }}
|
||||
|
|
@ -22,7 +22,7 @@
|
|||
{{ if eq .Now 1 }}
|
||||
<tr>
|
||||
<td>{{ .Name }}</td>
|
||||
<td>{{ .Ip }}</td>
|
||||
<td><a href="http://{{ .Ip }}" target="_blank">{{ .Ip }}</a></td>
|
||||
<td>{{ .Mac }}</td>
|
||||
<td>{{ .Hw }}</td>
|
||||
<td>
|
||||
|
|
|
|||
74
src/templates/offline.html
Normal file
74
src/templates/offline.html
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
{{ define "offline"}}
|
||||
|
||||
{{ template "header" }}
|
||||
<!--Auto refresh (seconds)-->
|
||||
<!-- <meta http-equiv="refresh" content="{{ .Config.Timeout }}"> -->
|
||||
<!--Bootstrap theme-->
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootswatch@5.2.0/dist/{{ .Config.Theme }}/bootstrap.min.css" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container mt-3">
|
||||
<h2>Offline</h2>
|
||||
<table class="table table-striped">
|
||||
<tr>
|
||||
<th>Name<br>
|
||||
<button type="submit" name="sort_method" value="date-up" class="btn btn-primary btn-sm">
|
||||
<i class="bi bi-caret-up"></i>
|
||||
</button>
|
||||
<button type="submit" name="sort_method" value="date-down" class="btn btn-primary btn-sm">
|
||||
<i class="bi bi-caret-down"></i>
|
||||
</button>
|
||||
</th>
|
||||
<th>Ip<br>
|
||||
<button type="submit" name="sort_method" value="ip-up" class="btn btn-primary btn-sm">
|
||||
<i class="bi bi-caret-up"></i>
|
||||
</button>
|
||||
<button type="submit" name="sort_method" value="ip-down" class="btn btn-primary btn-sm">
|
||||
<i class="bi bi-caret-down"></i>
|
||||
</button>
|
||||
</th>
|
||||
<th>Mac</th>
|
||||
<th>Hardware</th>
|
||||
<th>Last seen<br>
|
||||
<button type="submit" name="sort_method" value="date-up" class="btn btn-primary btn-sm">
|
||||
<i class="bi bi-caret-up"></i>
|
||||
</button>
|
||||
<button type="submit" name="sort_method" value="date-down" class="btn btn-primary btn-sm">
|
||||
<i class="bi bi-caret-down"></i>
|
||||
</button>
|
||||
</th>
|
||||
<th>Known<br>
|
||||
<button type="submit" name="sort_method" value="known-up" class="btn btn-primary btn-sm">
|
||||
<i class="bi bi-caret-up"></i>
|
||||
</button>
|
||||
<button type="submit" name="sort_method" value="known-down" class="btn btn-primary btn-sm">
|
||||
<i class="bi bi-caret-down"></i>
|
||||
</button>
|
||||
</th>
|
||||
</tr>
|
||||
{{ range .Hosts }}
|
||||
<form action="/update_host/" method="post">
|
||||
<input name="id" type="hidden" class="form-control" value="{{ .Id }}">
|
||||
<tr>
|
||||
<td>
|
||||
<input name="name" type="text" class="form-control text-dark" value="{{ .Name }}">
|
||||
</td>
|
||||
<td>{{ .Ip }}</td>
|
||||
<td>{{ .Mac }}</td>
|
||||
<td>{{ .Hw }}</td>
|
||||
<td>{{ .Date }}</td>
|
||||
<td>
|
||||
{{ if eq .Known 1 }}
|
||||
<button value="off" name="known" type="submit" class="btn btn-success">Yes</button>
|
||||
{{ else }}
|
||||
<button value="on" name="known" type="submit" class="btn btn-warning">No</button>
|
||||
{{ end }}
|
||||
</td>
|
||||
</tr>
|
||||
</form>
|
||||
{{ end }}
|
||||
</table>
|
||||
</div>
|
||||
|
||||
{{ template "footer" }}
|
||||
{{ end }}
|
||||
|
|
@ -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)
|
||||
}
|
||||
44
src/web-onoffline.go
Normal file
44
src/web-onoffline.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
50
src/web-sort.go
Normal file
50
src/web-sort.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
21
src/web-theme.go
Normal file
21
src/web-theme.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
Loading…
Reference in a new issue