finale Anpassung

This commit is contained in:
2025-05-04 17:10:38 +02:00
parent eb2d05f082
commit a1471fc310
9 changed files with 386 additions and 193 deletions

View File

@@ -46,19 +46,22 @@ func main() {
}
// Basislayout zuerst parsen
base := template.Must(
layout := template.Must(
template.New("base").Funcs(funcs).
ParseFiles("internal/web/templates/base.html"),
)
// LISTSeite: base + list.html
tplList = template.Must(base.Clone())
tplList = template.Must(layout.Clone())
template.Must(tplList.Funcs(funcs).ParseFiles("internal/web/templates/list.html"))
// ARTICLEInstanz
tplArticle = template.Must(base.Clone())
tplArticle = template.Must(layout.Clone())
template.Must(tplArticle.Funcs(funcs).ParseFiles("internal/web/templates/article.html"))
tplPage := template.Must(layout.Clone())
template.Must(tplPage.ParseFiles("internal/web/templates/page.html"))
mux := http.NewServeMux()
staticDir := "internal/web/static"
@@ -68,6 +71,13 @@ func main() {
fmt.Println(err)
}
staticPages, err := article.LoadStatic("pages")
if err != nil {
fmt.Println(err)
} else {
fmt.Println(staticPages)
}
// Handler für /
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
@@ -92,6 +102,19 @@ func main() {
http.NotFound(w, r)
})
mux.HandleFunc("/page/", func(w http.ResponseWriter, r *http.Request) {
slug := strings.TrimPrefix(r.URL.Path, "/page/")
p, ok := staticPages[slug]
if !ok {
http.NotFound(w, r)
return
}
// "layout" kommt aus deinem TemplatePool (list/article nutzen es ja auch)
if err := tplPage.ExecuteTemplate(w, "page", p); err != nil {
http.Error(w, err.Error(), 500)
}
})
mux.Handle("/static/",
cacheControl(http.StripPrefix("/static/",
http.FileServer(http.Dir(staticDir)))))