a dummy readonly ui but no way to write and nothing written is boring

main
Bel LaPointe 2025-04-28 21:59:00 -06:00
parent ac642d0bdf
commit 4c9e6fbe35
3 changed files with 47 additions and 12 deletions

View File

@ -1,9 +0,0 @@
<html>
<header>
</header>
<body>
<h2>Hello World</h2>
</body>
<footer>
</footer>
</html>

View File

@ -0,0 +1,8 @@
<html>
<body>
<h2>Hello templated world b</h2>
{{ range feeds }}
feed = {{ . }}
{{ end }}
</body>
</html>

View File

@ -1,13 +1,49 @@
package handler
import (
"log"
"html/template"
"net/http"
"os"
"path"
"show-rss/src/feeds"
)
var dir = func() string {
if v := os.Getenv("UI_D"); v != "" {
return v
}
return "./src/cmd/server/handler/testdata"
}()
func (h Handler) ui(w http.ResponseWriter, r *http.Request) error {
log.Printf("ui(%s)", r.URL.String())
fs := http.FileServer(http.Dir("./src/cmd/server/handler/testdata"))
fs := http.FileServer(http.Dir(dir))
if path.Base(r.URL.Path) == "ui" {
return h.uiIndex(w, r)
}
http.StripPrefix("/experimental/ui", fs).ServeHTTP(w, r)
return nil
}
func (h Handler) uiIndex(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
b, _ := os.ReadFile(path.Join(dir, "index.tmpl"))
tmpl := template.New(r.URL.Path).Funcs(template.FuncMap{
"feeds": func() ([]feeds.Feed, error) {
all := []feeds.Feed{}
err := feeds.ForEach(ctx, func(f feeds.Feed) error {
all = append(all, f)
return ctx.Err()
})
return all, err
},
})
tmpl, err := tmpl.Parse(string(b))
if err != nil {
return err
}
return tmpl.Execute(w, nil)
}