diff --git a/cmd/substream/main.go b/cmd/substream/main.go
index f4f7fa8..c418fb4 100644
--- a/cmd/substream/main.go
+++ b/cmd/substream/main.go
@@ -15,6 +15,7 @@ import (
"github.com/ninedraft/substream/assets"
"github.com/ninedraft/substream/streamer"
+ "github.com/ninedraft/substream/views"
)
func main() {
@@ -93,8 +94,8 @@ func main() {
}
}()
- currentTrack := atomic.Pointer[trackData]{}
- currentTrack.Store(&trackData{
+ currentTrack := atomic.Pointer[views.TrackData]{}
+ currentTrack.Store(&views.TrackData{
Title: "Music for Programming",
Artist: "MFP",
Length: time.Minute,
@@ -133,7 +134,7 @@ func main() {
}
w.Header().Set("Content-Type", "text/html")
- errRender := view.Render(w)
+ errRender := views.Track.Render(w, view)
if errRender != nil {
log.Println("rendering track:", errRender)
}
diff --git a/cmd/substream/track.go b/cmd/substream/track.go
index 248b9fe..b643f04 100644
--- a/cmd/substream/track.go
+++ b/cmd/substream/track.go
@@ -1,8 +1,6 @@
package main
import (
- "html/template"
- "io"
"time"
)
@@ -11,21 +9,3 @@ type trackCover struct {
data []byte
updatedAt time.Time
}
-
-type trackData struct {
- Title string
- Artist string
- Length time.Duration
-}
-
-var trackView = template.Must(template.New("track").Parse(`
-
-
{{ .Title }}
-
{{ .Artist }}
-
{{ .Length }}
-
-`))
-
-func (t *trackData) Render(dst io.Writer) error {
- return trackView.Execute(dst, t)
-}
diff --git a/views/track.go b/views/track.go
new file mode 100644
index 0000000..81ad805
--- /dev/null
+++ b/views/track.go
@@ -0,0 +1,11 @@
+package views
+
+import "time"
+
+const Track View[TrackData] = "track.html"
+
+type TrackData struct {
+ Title string
+ Artist string
+ Length time.Duration
+}
diff --git a/views/track.html b/views/track.html
new file mode 100644
index 0000000..9295e52
--- /dev/null
+++ b/views/track.html
@@ -0,0 +1,5 @@
+
+
{{ .Title }}
+
{{ .Artist }}
+
{{ .Length }}
+
\ No newline at end of file
diff --git a/views/views.go b/views/views.go
new file mode 100644
index 0000000..539189e
--- /dev/null
+++ b/views/views.go
@@ -0,0 +1,18 @@
+package views
+
+import (
+ "embed"
+ "html/template"
+ "io"
+)
+
+type View[E any] string
+
+//go:embed *.html
+var assets embed.FS
+
+var tmpls = template.Must(template.ParseFS(assets, "*.html"))
+
+func (view View[E]) Render(dst io.Writer, args *E) error {
+ return tmpls.ExecuteTemplate(dst, string(view), args)
+}
diff --git a/views/views_test.go b/views/views_test.go
new file mode 100644
index 0000000..30d5df8
--- /dev/null
+++ b/views/views_test.go
@@ -0,0 +1,29 @@
+package views_test
+
+import (
+ "io"
+ "testing"
+
+ "github.com/ninedraft/substream/views"
+)
+
+func TestViews(t *testing.T) {
+ t.Parallel()
+ t.Log(
+ "Rendering views should not return an error.",
+ )
+
+ testView(t, views.Track)
+}
+
+func testView[E any](t *testing.T, view views.View[E]) {
+ t.Helper()
+
+ t.Run(string(view)+".Render", func(t *testing.T) {
+ t.Parallel()
+
+ if err := view.Render(io.Discard, new(E)); err != nil {
+ t.Errorf("render: %v", err)
+ }
+ })
+}