Skip to content

Commit

Permalink
Sort and search (#4,#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
aceberg committed Sep 5, 2022
1 parent 3cc3bf2 commit 567c756
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/templates/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<div class="container">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active" href="/">All</a>
<a class="nav-link active" href="/home/">Home</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="/online/">Online</a>
Expand Down Expand Up @@ -57,6 +57,12 @@
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<form action="/search_hosts/" method="post">
<li class="input-group nav-item">
<input name="search" type="text" class="form-control text-dark" placeholder="Search">
<button type="submit" class="btn btn-primary">Search</button>
</li>
</form>
<li class="nav-item"><a class="nav-link active" href="https://github.com/aceberg/WatchYourLAN">
<h3>&nbsp;&nbsp;&nbsp;<i class="bi bi-github"></i></h3>
</a></li>
Expand Down
9 changes: 5 additions & 4 deletions src/templates/offline.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
</head>
<body>
<div class="container mt-3">
<h2>Offline</h2>
<table class="table table-striped">
<tr>
<form action="/sort_hosts/" method="post">
<th>Name<br>
<button type="submit" name="sort_method" value="date-up" class="btn btn-primary btn-sm">
<button type="submit" name="sort_method" value="name-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">
<button type="submit" name="sort_method" value="name-down" class="btn btn-primary btn-sm">
<i class="bi bi-caret-down"></i>
</button>
</th>
Expand Down Expand Up @@ -45,6 +45,7 @@ <h2>Offline</h2>
<i class="bi bi-caret-down"></i>
</button>
</th>
</form>
</tr>
{{ range .Hosts }}
<form action="/update_host/" method="post">
Expand All @@ -53,7 +54,7 @@ <h2>Offline</h2>
<td>
<input name="name" type="text" class="form-control text-dark" value="{{ .Name }}">
</td>
<td>{{ .Ip }}</td>
<td><a href="http://{{ .Ip }}" target="_blank">{{ .Ip }}</a></td>
<td>{{ .Mac }}</td>
<td>{{ .Hw }}</td>
<td>{{ .Date }}</td>
Expand Down
4 changes: 3 additions & 1 deletion src/web-index.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func update_host(w http.ResponseWriter, r *http.Request) {
}
}

http.Redirect(w, r, "/", http.StatusSeeOther)
http.Redirect(w, r, r.Header.Get("Referer"), 302)
}

func webgui() {
Expand All @@ -56,8 +56,10 @@ func webgui() {
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)
Expand Down
35 changes: 35 additions & 0 deletions src/web-search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"net/http"
"strings"
)

func search_hosts(w http.ResponseWriter, r *http.Request) {
search := r.FormValue("search")

foundHosts := []Host{}
for _, oneHost := range AllHosts {
if in_string(oneHost.Name, search) {
foundHosts = append(foundHosts, oneHost)
} else if in_string(oneHost.Ip, search) {
foundHosts = append(foundHosts, oneHost)
} else if in_string(oneHost.Mac, search) {
foundHosts = append(foundHosts, oneHost)
} else if in_string(oneHost.Date, search) {
foundHosts = append(foundHosts, oneHost)
} else if in_string(oneHost.Hw, search) {
foundHosts = append(foundHosts, oneHost)
}
}
AllHosts = foundHosts

http.Redirect(w, r, r.Header.Get("Referer"), 302)
}

func in_string (str1 string, str2 string) (bool) {
return strings.Contains(
strings.ToLower(str1),
strings.ToLower(str2),
)
}
2 changes: 1 addition & 1 deletion src/web-sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ func sort_hosts(w http.ResponseWriter, r *http.Request) {
AllHosts = db_select()
}

http.Redirect(w, r, "/", http.StatusSeeOther)
http.Redirect(w, r, r.Header.Get("Referer"), 302)
}
5 changes: 5 additions & 0 deletions src/web-theme.go → src/web-theme-home.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ func theme(w http.ResponseWriter, r *http.Request) {
write_config()
}

http.Redirect(w, r, r.Header.Get("Referer"), 302)
}

func home(w http.ResponseWriter, r *http.Request) {
AllHosts = db_select()
http.Redirect(w, r, "/", http.StatusSeeOther)
}

0 comments on commit 567c756

Please sign in to comment.