execTemplate

This commit is contained in:
aceberg 2023-01-05 13:41:11 +07:00
parent e371e7bc48
commit 70cb8d0e6e
7 changed files with 30 additions and 43 deletions

View file

@ -6,12 +6,12 @@ import (
"time"
)
func isOpen(host string, port int) bool {
func isOpen(host, proto string, port int) bool {
timeout := 3 * time.Second
target := fmt.Sprintf("%s:%d", host, port)
conn, err := net.DialTimeout("tcp", target, timeout)
conn, err := net.DialTimeout(proto, target, timeout)
if err != nil {
return false
}
@ -25,14 +25,14 @@ func isOpen(host string, port int) bool {
}
// 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 ports []string
ports = []string{}
for i := begin; i < end; i++ {
if isOpen(host, i) {
for i := begin; i < end+1; i++ {
if isOpen(host, proto, i) {
onePort = fmt.Sprintf("%s:%d", host, i)
ports = append(ports, onePort)
}

View file

@ -1,7 +1,6 @@
package web
import (
"html/template"
"log"
"net/http"
"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"}
tmpl, err := template.ParseFS(TemplHTML, TemplPath+"config.html", TemplPath+"header.html", TemplPath+"footer.html")
check.IfError(err)
err = tmpl.ExecuteTemplate(w, "header", guiData)
check.IfError(err)
err = tmpl.ExecuteTemplate(w, "config", guiData)
check.IfError(err)
execTemplate(w, "config", guiData)
}
func saveConfigHandler(w http.ResponseWriter, r *http.Request) {

19
internal/web/exec-tpl.go Normal file
View 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)
}

View file

@ -1,7 +1,6 @@
package web
import (
"html/template"
"log"
"net"
"net/http"
@ -53,10 +52,5 @@ func hostHandler(w http.ResponseWriter, r *http.Request) {
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)
execTemplate(w, "host", guiData)
}

View file

@ -1,11 +1,8 @@
package web
import (
// "fmt"
"html/template"
"net/http"
"github.com/aceberg/WatchYourLAN/internal/check"
"github.com/aceberg/WatchYourLAN/internal/db"
"github.com/aceberg/WatchYourLAN/internal/models"
)
@ -17,12 +14,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
guiData.Hosts = AllHosts
guiData.Icon = Icon
tmpl, err := template.ParseFS(TemplHTML, TemplPath+"index.html", TemplPath+"header.html", TemplPath+"footer.html")
check.IfError(err)
err = tmpl.ExecuteTemplate(w, "header", guiData)
check.IfError(err)
err = tmpl.ExecuteTemplate(w, "index", guiData)
check.IfError(err)
execTemplate(w, "index", guiData)
}
func homeHandler(w http.ResponseWriter, r *http.Request) {

View file

@ -1,10 +1,8 @@
package web
import (
"html/template"
"net/http"
"github.com/aceberg/WatchYourLAN/internal/check"
"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")
err := tmpl.ExecuteTemplate(w, "header", guiData)
check.IfError(err)
err = tmpl.ExecuteTemplate(w, "line", guiData)
check.IfError(err)
execTemplate(w, "line", guiData)
}

View file

@ -1,7 +1,6 @@
package web
import (
"html/template"
"net/http"
"strconv"
@ -26,12 +25,7 @@ func portHandler(w http.ResponseWriter, r *http.Request) {
end, err := strconv.Atoi(endStr)
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")
check.IfError(err)
err = tmpl.ExecuteTemplate(w, "header", guiData)
check.IfError(err)
err = tmpl.ExecuteTemplate(w, "port", guiData)
check.IfError(err)
execTemplate(w, "port", guiData)
}