diff options
| author | Xe Iaso <me@xeiaso.net> | 2023-09-09 22:37:53 -0400 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2023-09-09 22:37:53 -0400 |
| commit | 8eb7eaf80084346f07dc573a641b077a000982f1 (patch) | |
| tree | 9947e6d886e200b7c24786f91ee9ad9159a774a7 /xesite/main.go | |
| parent | 3035f885636c72bd62329af3dc82efc74bc6ad09 (diff) | |
| download | xesite-8eb7eaf80084346f07dc573a641b077a000982f1.tar.xz xesite-8eb7eaf80084346f07dc573a641b077a000982f1.zip | |
fix HTML template rendering
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'xesite/main.go')
| -rw-r--r-- | xesite/main.go | 55 |
1 files changed, 39 insertions, 16 deletions
diff --git a/xesite/main.go b/xesite/main.go index dea694e..ef530e9 100644 --- a/xesite/main.go +++ b/xesite/main.go @@ -7,6 +7,7 @@ import ( "log/slog" "net/http" "os" + "strings" "sync" "time" @@ -42,13 +43,13 @@ func templateFor(name string) *template.Template { } type Site struct { - Config *config.Config - Posts []*internal.Post + Config *config.Config + Blog []*internal.Post + Talks []*internal.Post + Gallery []*internal.Post } -func (s *Site) HandleIndex(w http.ResponseWriter, r *http.Request) { - slog.Debug("request", "path", r.URL.Path, "method", r.Method, "remote", r.RemoteAddr) - +func (s *Site) Index(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { w.WriteHeader(http.StatusNotFound) if err := templateFor("404.html").Execute(w, map[string]string{ @@ -64,6 +65,12 @@ func (s *Site) HandleIndex(w http.ResponseWriter, r *http.Request) { } } +func (s *Site) BlogIndex(w http.ResponseWriter, r *http.Request) { + if err := templateFor("blogindex.html").Execute(w, s.Blog); err != nil { + slog.Error("can't render template", "err", err) + } +} + func main() { flag.Parse() internal.Slog() @@ -78,7 +85,31 @@ func main() { posts := embedded.Posts - tmpl := template.Must( + var blog []*internal.Post + var talks []*internal.Post + var gallery []*internal.Post + + for _, post := range posts { + switch strings.Split(post.Link, "/")[0] { + case "blog": + blog = append(blog, post) + case "talks": + talks = append(talks, post) + case "gallery": + gallery = append(gallery, post) + } + } + + mux.Handle("/static/", http.FileServer(http.FS(xeiaso.Static))) + + site := &Site{ + Config: config, + Blog: blog, + Talks: talks, + Gallery: gallery, + } + + baseTmpl = template.Must( template.New("xesite/v4").Funcs(template.FuncMap{ "getYear": func() string { return time.Now().Format("2006") @@ -95,15 +126,6 @@ func main() { }). ParseFS(xeiaso.Templates, "tmpl/base.html"), ) - baseTmpl = tmpl - tmpl = template.Must(tmpl.ParseFS(xeiaso.Templates, "tmpl/*.html")) - - mux.Handle("/static/", http.FileServer(http.FS(xeiaso.Static))) - - site := &Site{ - Config: config, - Posts: posts, - } for k, v := range config.Redirects { mux.HandleFunc(k, func(w http.ResponseWriter, r *http.Request) { @@ -111,7 +133,8 @@ func main() { }) } - mux.HandleFunc("/", site.HandleIndex) + mux.HandleFunc("/", site.Index) + mux.HandleFunc("/blog", site.BlogIndex) slog.Info("listening", "addr", *addr) |
