From 186491250e773abb148c601ef0461469a6b4cf12 Mon Sep 17 00:00:00 2001 From: ange Date: Sat, 14 Dec 2024 09:19:13 +0000 Subject: [PATCH] feat: range over files for paths --- src/tmpl.go | 16 +++++++++------- src/views.go | 3 +-- tmpl/header.tmpl | 9 +++------ 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/tmpl.go b/src/tmpl.go index 0115cd8..21c57e3 100644 --- a/src/tmpl.go +++ b/src/tmpl.go @@ -9,21 +9,23 @@ import ( var TMPL map[string][]byte -// TODO not use a global -// TODO use range instead of if else in header.html func generateTmpl() { files, _ := filepath.Glob("html/*.html") - re := regexp.MustCompile("html(/.+).html") + re := regexp.MustCompile("html/(.+).html") + names := make([]string, len(files)) + for i, f := range files { + names[i] = re.FindStringSubmatch(f)[1] + } TMPL = make(map[string][]byte, len(files)) - for _, f := range files { + for i, f := range files { b := new(bytes.Buffer) - name := re.FindStringSubmatch(f)[1] t, _ := template.ParseFiles(f) t.ParseGlob("tmpl/*.tmpl") t.Execute(b, map[string]any{ - "name": name, + "name": names[i], + "names": names, }) - TMPL[name] = b.Bytes() + TMPL["/" + names[i]] = b.Bytes() } } diff --git a/src/views.go b/src/views.go index e37d897..1af527d 100644 --- a/src/views.go +++ b/src/views.go @@ -5,8 +5,7 @@ import ( ) func index(w http.ResponseWriter, r *http.Request) { - r.URL.Path = "/contact" - html(w, r) + http.Redirect(w, r, "/contact", http.StatusMovedPermanently) } func style(w http.ResponseWriter, r *http.Request) { diff --git a/tmpl/header.tmpl b/tmpl/header.tmpl index beba74f..7d7920b 100644 --- a/tmpl/header.tmpl +++ b/tmpl/header.tmpl @@ -4,12 +4,9 @@ Website heavily inspired from suckless.org