a dummy readonly ui but no way to write and nothing written is boring
parent
ac642d0bdf
commit
4c9e6fbe35
|
|
@ -1,9 +0,0 @@
|
|||
<html>
|
||||
<header>
|
||||
</header>
|
||||
<body>
|
||||
<h2>Hello World</h2>
|
||||
</body>
|
||||
<footer>
|
||||
</footer>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
<html>
|
||||
<body>
|
||||
<h2>Hello templated world b</h2>
|
||||
{{ range feeds }}
|
||||
feed = {{ . }}
|
||||
{{ end }}
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue