commit 964b49d98af7652f37f0eb6fe170fdca5d444b35 from: Matt Streatfield via: Oliver Lowe date: Mon Apr 21 23:45:08 2025 UTC Rename some templates Make the channel template re-usable Add handler for channel page List episode titles commit - 5dfdd66b09fe47403bcc7678f431f06feba5cbb1 commit + 964b49d98af7652f37f0eb6fe170fdca5d444b35 blob - fe60e21b6bd1a72734687ab0197b479fd2332ae8 blob + 60238e8a438264893569d426e11a67367d81bc03 --- internal/service/handlers.go +++ internal/service/handlers.go @@ -2,11 +2,10 @@ package service import ( "html/template" + "log" "net/http" "net/url" "strings" - - "github.com/streatCodes/rss/rss" ) var templateFuncs = template.FuncMap{ @@ -14,14 +13,17 @@ var templateFuncs = template.FuncMap{ "pathEscape": url.PathEscape, } -func render(w http.ResponseWriter, name string, data any) { +func render(w http.ResponseWriter, name string, data any) error { tmpl := template.New("").Funcs(templateFuncs) tmpl = template.Must(tmpl.ParseGlob("internal/templates/*.tmpl")) - tmpl.ExecuteTemplate(w, name, data) + return tmpl.ExecuteTemplate(w, name, data) } func (service *Service) homeHandler(w http.ResponseWriter, r *http.Request) { - render(w, "home", nil) + err := render(w, "homePage", nil) + if err != nil { + log.Printf("Error executing template - %s", err) + } } func (service *Service) searchHandler(w http.ResponseWriter, r *http.Request) { @@ -37,22 +39,23 @@ func (service *Service) searchHandler(w http.ResponseW render(w, "results", results) return } - render(w, "home", results) + err = render(w, "homePage", results) + if err != nil { + log.Printf("Error executing template - %s", err) + } } -type ChannelPage struct { - ShowSubscribeButton bool - Channel *rss.Channel -} - func (service *Service) channelHandler(w http.ResponseWriter, r *http.Request) { - channelPage := ChannelPage{ShowSubscribeButton: true} + channelPage := ChannelResult{ShowSubscribeButton: true} channelUrl := strings.TrimPrefix(r.URL.Path, "/channel/") //Check to see if we have the feed in the database if channel, err := service.db.GetChannel(channelUrl); channel != nil && err == nil { - channelPage.Channel = channel + channelPage.Channel = *channel } - render(w, "channelPage", channelPage) + err := render(w, "channelPage", channelPage) + if err != nil { + log.Printf("Error executing template - %s", err) + } } blob - 7ed9210fb5ae6c58e79f7f2e061779b2107ecb8d blob + 0290a91232879dfc969ed229043dc4ca3c74c021 --- internal/service/search.go +++ internal/service/search.go @@ -9,8 +9,9 @@ import ( ) type ChannelResult struct { - ChannelUrl string - Channel rss.Channel + ShowSubscribeButton bool + ChannelUrl string + Channel rss.Channel } func (service *Service) findChannel(query string) ([]ChannelResult, error) { blob - ef0a5d66297f2c3b8c46267193799a428e007726 blob + a5ccabe58f529a1cd5b8be587e00bbb6cfc1fbdc --- internal/templates/channelPage.tmpl +++ internal/templates/channelPage.tmpl @@ -1,7 +1,11 @@ {{ define "channelPage" }} {{ template "header" }}
- Channels todo {{.Channel.Title}} + {{ template "channel" . }} + TODO Episodes: + {{range .Channel.Items}} +

{{.Title}}

+ {{end}}
{{ template "footer" }} {{ end }} blob - /dev/null blob + 877c7246e89f9cba24249441307f109be81eb6b2 (mode 644) --- /dev/null +++ internal/templates/channel.tmpl @@ -0,0 +1,36 @@ +{{ define "channel" }} +
+ +
+
+
+

{{ .Channel.Title }}

+ {{if .ChannelUrl}}Episodes{{end}} + {{if .ShowSubscribeButton}}Subscribe{{end}} +
+ +
+

{{ .Channel.Description }}

+
+ {{ if gt (len .Channel.Link) 0 }} + + + + + + {{ end }} + + + + + + + {{ range .Channel.Categories }} + #{{ .Text }} + {{ end }} +
+
+
+{{ end }} \ No newline at end of file blob - 23302b9aa8be8f918eb35fbd152ef550a6366a1c (mode 644) blob + /dev/null --- internal/templates/home.tmpl +++ /dev/null @@ -1,43 +0,0 @@ -{{ define "home" }} -{{ template "header" }} -
-
-

Subscribe to your favourite feed!

-
-

Your 

-
-

exciting calendar event

-

top blog

-

beloved podcast

-

number one forum thread

-

go-to YouTube channel

-
-
-
- -
- -
- -
- {{ template "results" . }} -
-
-{{ template "footer" }} -{{ end }} blob - 659b7e94cc258811911c19033cd2fa7efe7b88b6 blob + 2aab6a3d7f25203a20886b691538fd51768cd146 --- internal/templates/results.tmpl +++ internal/templates/results.tmpl @@ -1,39 +1,7 @@ {{ define "results" }}
{{ range . }} -
- -
-
-
-

{{ .Channel.Title }}

- Episodes -
- -
-

{{ .Channel.Description }}

-
- {{ if gt (len .Channel.Link) 0 }} - - - - - - {{ end }} - - - - - - - {{ range .Channel.Categories }} - #{{ .Text }} - {{ end }} -
-
-
+ {{ template "channel" . }} {{ end }}
{{ end }} \ No newline at end of file blob - /dev/null blob + d1df16020a04bda47bd95761a71fffe655c38a5c (mode 644) --- /dev/null +++ internal/templates/homePage.tmpl @@ -0,0 +1,43 @@ +{{ define "homePage" }} +{{ template "header" }} +
+
+

Subscribe to your favourite feed!

+
+

Your 

+
+

exciting calendar event

+

top blog

+

beloved podcast

+

number one forum thread

+

go-to YouTube channel

+
+
+
+ +
+ +
+ +
+ {{ template "results" . }} +
+
+{{ template "footer" }} +{{ end }} blob - 273cadda4dc38e2b12ef22db0e9ce52b16cc787d blob + 3e5a16ca63d50b96853d608997b5c9095754578a --- web/style.css +++ web/style.css @@ -223,13 +223,17 @@ main:has(.result)>hgroup.search-animation { } } -.results .result { +.result { background-color: var(--black); display: flex; border-radius: 2rem; padding: 1rem; box-shadow: var(--shadow); margin-bottom: 1rem; + + &>div { + width: 100%; + } img { border-radius: 2rem;