all repos — website @ 6b5a99d1de4420a9334ed9b2119b56b138518c1c

My website

use embedded filesystem for server template access
Alan Pearce alan@alanpearce.eu
Mon, 24 Jun 2024 23:28:17 +0200
commit

6b5a99d1de4420a9334ed9b2119b56b138518c1c

parent

49b4d4d4e2bfcb5152b574100755bfbd39ede602

4 files changed, 20 insertions(+), 2 deletions(-)

jump to
M default.nixdefault.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.templtemplates/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)
 	}