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

View file

@ -17,7 +17,8 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
guiData.Hosts = AllHosts guiData.Hosts = AllHosts
guiData.Icon = Icon 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) err := tmpl.ExecuteTemplate(w, "header", guiData)
check.IfError(err) check.IfError(err)
err = tmpl.ExecuteTemplate(w, "index", guiData) 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) err := tmpl.ExecuteTemplate(w, "header", guiData)
check.IfError(err) check.IfError(err)
err = tmpl.ExecuteTemplate(w, "offline", guiData) 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) err := tmpl.ExecuteTemplate(w, "header", guiData)
check.IfError(err) check.IfError(err)
err = tmpl.ExecuteTemplate(w, "offline", guiData) err = tmpl.ExecuteTemplate(w, "offline", guiData)

View file

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