use embedded filesystem for server template access
Alan Pearce alan@alanpearce.eu
Mon, 24 Jun 2024 23:28:17 +0200
4 files changed, 20 insertions(+), 2 deletions(-)
M default.nix → default.nix
@@ -116,6 +116,7 @@ patchPhase = '' ${pkgs.templ}/bin/templ generate ''; subPackages = [ "cmd/server" ]; + tags = [ "embed" ]; ldflags = [ "-s" "-w"
A templates/dev.go
@@ -0,0 +1,9 @@+//go:build !embed + +package templates + +import ( + "os" +) + +var Files = os.DirFS("templates/")
A templates/embed.go
@@ -0,0 +1,8 @@+//go:build embed + +package templates + +import "embed" + +//go:embed * +var Files embed.FS
M templates/page.templ → templates/page.templ
@@ -1,7 +1,7 @@ package templates import ( - "os" + "io/fs" "net/url" "website/internal/config" @@ -12,7 +12,7 @@ css string ) func init() { - bytes, err := os.ReadFile("templates/style.css") + bytes, err := fs.ReadFile(Files, "style.css") if err != nil { panic(err) }