Embed templates

This commit is contained in:
aceberg 2022-12-28 22:51:32 +07:00
parent 7e1d9d8826
commit a49a668d4d
4 changed files with 18 additions and 12 deletions

View file

@ -1,8 +1,8 @@
FROM golang:alpine AS builder
RUN apk add build-base
COPY src /src
RUN cd /src && go build .
COPY . /src
RUN cd /src/cmd/WatchYourLAN/ && CGO_ENABLED=0 go build -o /WatchYourLAN .
FROM alpine
@ -12,7 +12,6 @@ WORKDIR /app
RUN apk add --no-cache tzdata arp-scan \
&& mkdir /data
COPY src/templates /app/templates
COPY --from=builder /src/watchyourlan /app/
COPY --from=builder /WatchYourLAN /app/
ENTRYPOINT ["./watchyourlan"]
ENTRYPOINT ["./WatchYourLAN"]

View file

@ -17,7 +17,8 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
guiData.Hosts = AllHosts
guiData.Icon = Icon
tmpl, _ := template.ParseFiles(TemplPath+"index.html", TemplPath+"header.html", TemplPath+"footer.html")
// tmpl, _ := template.ParseFiles(TemplPath+"index.html", TemplPath+"header.html", TemplPath+"footer.html")
tmpl, _ := template.ParseFS(TemplHTML, TemplPath+"index.html", TemplPath+"header.html", TemplPath+"footer.html")
err := tmpl.ExecuteTemplate(w, "header", guiData)
check.IfError(err)
err = tmpl.ExecuteTemplate(w, "index", guiData)

View file

@ -21,7 +21,8 @@ func offlineHandler(w http.ResponseWriter, r *http.Request) {
}
}
tmpl, _ := template.ParseFiles(TemplPath+"offline.html", TemplPath+"header.html", TemplPath+"footer.html")
// tmpl, _ := template.ParseFiles(TemplPath+"offline.html", TemplPath+"header.html", TemplPath+"footer.html")
tmpl, _ := template.ParseFS(TemplHTML, TemplPath+"offline.html", TemplPath+"header.html", TemplPath+"footer.html")
err := tmpl.ExecuteTemplate(w, "header", guiData)
check.IfError(err)
err = tmpl.ExecuteTemplate(w, "offline", guiData)
@ -41,7 +42,8 @@ func onlineHandler(w http.ResponseWriter, r *http.Request) {
}
}
tmpl, _ := template.ParseFiles(TemplPath+"offline.html", TemplPath+"header.html", TemplPath+"footer.html")
// tmpl, _ := template.ParseFiles(TemplPath+"offline.html", TemplPath+"header.html", TemplPath+"footer.html")
tmpl, _ := template.ParseFS(TemplHTML, TemplPath+"offline.html", TemplPath+"header.html", TemplPath+"footer.html")
err := tmpl.ExecuteTemplate(w, "header", guiData)
check.IfError(err)
err = tmpl.ExecuteTemplate(w, "offline", guiData)

View file

@ -1,7 +1,7 @@
package web
import (
// "embed"
"embed"
"log"
"net/http"
@ -19,11 +19,15 @@ var (
AllHosts []models.Host
)
////go:embed templates/*
// var TemplHTML embed.FS
// TemplHTML - embed templates
//
//go:embed templates/*
var TemplHTML embed.FS
// TemplPath - path to html templates
const TemplPath = "../../internal/web/templates/"
const TemplPath = "templates/"
// const TemplPath = "../../internal/web/templates/"
// Gui - start web GUI
func Gui(appConfig models.Conf) {