execTemplate
This commit is contained in:
parent
e371e7bc48
commit
70cb8d0e6e
7 changed files with 30 additions and 43 deletions
|
|
@ -6,12 +6,12 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func isOpen(host string, port int) bool {
|
func isOpen(host, proto string, port int) bool {
|
||||||
|
|
||||||
timeout := 3 * time.Second
|
timeout := 3 * time.Second
|
||||||
target := fmt.Sprintf("%s:%d", host, port)
|
target := fmt.Sprintf("%s:%d", host, port)
|
||||||
|
|
||||||
conn, err := net.DialTimeout("tcp", target, timeout)
|
conn, err := net.DialTimeout(proto, target, timeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
@ -25,14 +25,14 @@ func isOpen(host string, port int) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scan - scan all TCP ports of a host
|
// Scan - scan all TCP ports of a host
|
||||||
func Scan(host string, begin, end int) []string {
|
func Scan(host, proto string, begin, end int) []string {
|
||||||
var onePort string
|
var onePort string
|
||||||
var ports []string
|
var ports []string
|
||||||
|
|
||||||
ports = []string{}
|
ports = []string{}
|
||||||
|
|
||||||
for i := begin; i < end; i++ {
|
for i := begin; i < end+1; i++ {
|
||||||
if isOpen(host, i) {
|
if isOpen(host, proto, i) {
|
||||||
onePort = fmt.Sprintf("%s:%d", host, i)
|
onePort = fmt.Sprintf("%s:%d", host, i)
|
||||||
ports = append(ports, onePort)
|
ports = append(ports, onePort)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
@ -21,12 +20,7 @@ func configHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
guiData.Themes = []string{"cerulean", "cosmo", "cyborg", "darkly", "flatly", "journal", "litera", "lumen", "lux", "materia", "minty", "morph", "pulse", "quartz", "sandstone", "simplex", "sketchy", "slate", "solar", "spacelab", "superhero", "united", "vapor", "yeti", "zephyr"}
|
guiData.Themes = []string{"cerulean", "cosmo", "cyborg", "darkly", "flatly", "journal", "litera", "lumen", "lux", "materia", "minty", "morph", "pulse", "quartz", "sandstone", "simplex", "sketchy", "slate", "solar", "spacelab", "superhero", "united", "vapor", "yeti", "zephyr"}
|
||||||
|
|
||||||
tmpl, err := template.ParseFS(TemplHTML, TemplPath+"config.html", TemplPath+"header.html", TemplPath+"footer.html")
|
execTemplate(w, "config", guiData)
|
||||||
check.IfError(err)
|
|
||||||
err = tmpl.ExecuteTemplate(w, "header", guiData)
|
|
||||||
check.IfError(err)
|
|
||||||
err = tmpl.ExecuteTemplate(w, "config", guiData)
|
|
||||||
check.IfError(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveConfigHandler(w http.ResponseWriter, r *http.Request) {
|
func saveConfigHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
||||||
19
internal/web/exec-tpl.go
Normal file
19
internal/web/exec-tpl.go
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
package web
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/aceberg/WatchYourLAN/internal/check"
|
||||||
|
"github.com/aceberg/WatchYourLAN/internal/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func execTemplate(w http.ResponseWriter, tpl string, guiData models.GuiData) {
|
||||||
|
// tmpl, err := template.ParseFiles(TemplPath+tpl+".html", TemplPath+"header.html", TemplPath+"footer.html")
|
||||||
|
tmpl, err := template.ParseFS(TemplHTML, TemplPath+tpl+".html", TemplPath+"header.html", TemplPath+"footer.html")
|
||||||
|
check.IfError(err)
|
||||||
|
err = tmpl.ExecuteTemplate(w, "header", guiData)
|
||||||
|
check.IfError(err)
|
||||||
|
err = tmpl.ExecuteTemplate(w, tpl, guiData)
|
||||||
|
check.IfError(err)
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -53,10 +52,5 @@ func hostHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
guiData.Themes = addr
|
guiData.Themes = addr
|
||||||
|
|
||||||
tmpl, err := template.ParseFS(TemplHTML, TemplPath+"host.html", TemplPath+"header.html", TemplPath+"footer.html")
|
execTemplate(w, "host", guiData)
|
||||||
check.IfError(err)
|
|
||||||
err = tmpl.ExecuteTemplate(w, "header", guiData)
|
|
||||||
check.IfError(err)
|
|
||||||
err = tmpl.ExecuteTemplate(w, "host", guiData)
|
|
||||||
check.IfError(err)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,8 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// "fmt"
|
|
||||||
"html/template"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/aceberg/WatchYourLAN/internal/check"
|
|
||||||
"github.com/aceberg/WatchYourLAN/internal/db"
|
"github.com/aceberg/WatchYourLAN/internal/db"
|
||||||
"github.com/aceberg/WatchYourLAN/internal/models"
|
"github.com/aceberg/WatchYourLAN/internal/models"
|
||||||
)
|
)
|
||||||
|
|
@ -17,12 +14,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
guiData.Hosts = AllHosts
|
guiData.Hosts = AllHosts
|
||||||
guiData.Icon = Icon
|
guiData.Icon = Icon
|
||||||
|
|
||||||
tmpl, err := template.ParseFS(TemplHTML, TemplPath+"index.html", TemplPath+"header.html", TemplPath+"footer.html")
|
execTemplate(w, "index", guiData)
|
||||||
check.IfError(err)
|
|
||||||
err = tmpl.ExecuteTemplate(w, "header", guiData)
|
|
||||||
check.IfError(err)
|
|
||||||
err = tmpl.ExecuteTemplate(w, "index", guiData)
|
|
||||||
check.IfError(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func homeHandler(w http.ResponseWriter, r *http.Request) {
|
func homeHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,8 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/aceberg/WatchYourLAN/internal/check"
|
|
||||||
"github.com/aceberg/WatchYourLAN/internal/models"
|
"github.com/aceberg/WatchYourLAN/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -26,9 +24,5 @@ func lineHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl, _ := template.ParseFS(TemplHTML, TemplPath+"line.html", TemplPath+"header.html", TemplPath+"footer.html")
|
execTemplate(w, "line", guiData)
|
||||||
err := tmpl.ExecuteTemplate(w, "header", guiData)
|
|
||||||
check.IfError(err)
|
|
||||||
err = tmpl.ExecuteTemplate(w, "line", guiData)
|
|
||||||
check.IfError(err)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
|
@ -26,12 +25,7 @@ func portHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
end, err := strconv.Atoi(endStr)
|
end, err := strconv.Atoi(endStr)
|
||||||
check.IfError(err)
|
check.IfError(err)
|
||||||
|
|
||||||
guiData.Themes = port.Scan(ip, begin, end)
|
guiData.Themes = port.Scan(ip, "tcp", begin, end)
|
||||||
|
|
||||||
tmpl, err := template.ParseFS(TemplHTML, TemplPath+"port.html", TemplPath+"header.html", TemplPath+"footer.html")
|
execTemplate(w, "port", guiData)
|
||||||
check.IfError(err)
|
|
||||||
err = tmpl.ExecuteTemplate(w, "header", guiData)
|
|
||||||
check.IfError(err)
|
|
||||||
err = tmpl.ExecuteTemplate(w, "port", guiData)
|
|
||||||
check.IfError(err)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue