Compare commits

..

2 Commits

Author SHA1 Message Date
bel
8f0c62bd77 runs 2025-05-07 22:27:06 -06:00
bel
a0ddc7f25f embed fs 2025-05-07 22:15:01 -06:00

View File

@@ -2,15 +2,26 @@ package handler
import ( import (
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"os" "os"
"path" "path"
"show-rss/src/feeds" "show-rss/src/feeds"
"slices" "slices"
"strings"
"text/template" "text/template"
"time" "time"
"embed"
_ "embed"
) )
//go:embed testdata/index.tmpl
var embeddedIndexTMPL string
//go:embed testdata/*
var embeddedDir embed.FS
var dir = func() string { var dir = func() string {
if v := os.Getenv("UI_D"); v != "" { if v := os.Getenv("UI_D"); v != "" {
return v return v
@@ -19,11 +30,13 @@ var dir = func() string {
}() }()
func (h Handler) ui(w http.ResponseWriter, r *http.Request) error { func (h Handler) ui(w http.ResponseWriter, r *http.Request) error {
fs := http.FileServer(http.Dir(dir)) if path.Base(r.URL.Path) == "ui" || r.URL.Path == "/" {
if path.Base(r.URL.Path) == "ui" {
return h.uiIndex(w, r) return h.uiIndex(w, r)
} }
http.StripPrefix("/experimental/ui", fs).ServeHTTP(w, r)
fs := http.FileServer(http.FS(embeddedDir))
r.URL.Path = fmt.Sprintf("/testdata/%s", strings.TrimPrefix(r.URL.Path, "/experimental/ui"))
fs.ServeHTTP(w, r)
return nil return nil
} }
@@ -47,6 +60,9 @@ func (h Handler) uiIndex(w http.ResponseWriter, r *http.Request) error {
} }
b, _ := os.ReadFile(path.Join(dir, "index.tmpl")) b, _ := os.ReadFile(path.Join(dir, "index.tmpl"))
if len(b) == 0 {
b = []byte(embeddedIndexTMPL)
}
tmpl := template.New(r.URL.Path).Funcs(template.FuncMap{ tmpl := template.New(r.URL.Path).Funcs(template.FuncMap{
"feeds": func() []feeds.Feed { "feeds": func() []feeds.Feed {