Skip to content

Commit

Permalink
move views as separate components
Browse files Browse the repository at this point in the history
  • Loading branch information
ninedraft committed Nov 4, 2023
1 parent a252d7b commit 1675303
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 23 deletions.
7 changes: 4 additions & 3 deletions cmd/substream/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/ninedraft/substream/assets"
"github.com/ninedraft/substream/streamer"
"github.com/ninedraft/substream/views"
)

func main() {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
20 changes: 0 additions & 20 deletions cmd/substream/track.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
"html/template"
"io"
"time"
)

Expand All @@ -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(`
<div class="track">
<div class="track__title">{{ .Title }}</div>
<div class="track__artist">{{ .Artist }}</div>
<div class="track__length">{{ .Length }}</div>
</div>
`))

func (t *trackData) Render(dst io.Writer) error {
return trackView.Execute(dst, t)
}
11 changes: 11 additions & 0 deletions views/track.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package views

import "time"

const Track View[TrackData] = "track.html"

type TrackData struct {
Title string
Artist string
Length time.Duration
}
5 changes: 5 additions & 0 deletions views/track.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="track">
<div class="track__title">{{ .Title }}</div>
<div class="track__artist">{{ .Artist }}</div>
<div class="track__length">{{ .Length }}</div>
</div>
18 changes: 18 additions & 0 deletions views/views.go
Original file line number Diff line number Diff line change
@@ -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)
}
29 changes: 29 additions & 0 deletions views/views_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}

0 comments on commit 1675303

Please sign in to comment.