- commit
- d6f7f7e
- parent
- 7768f66
- author
- jolheiser
- date
- 2025-08-11 08:12:40 -0400 EDT
refactor: restructure web assets - Move component (non-page) assets into a components sub-directory - This makes parsing all of them simpler without needing to add to the list in-code - Move pages into their own subdirectory - Allows simplifying their name and distinguishes which files are the "actual" pages - Move parsing to vars - Cleans up some overhead by not having to parse the templates on *every* page load while also simplifying the callsite Signed-off-by: jolheiser <git@jolheiser.com>
12 files changed,
+27,
-31
M
web.go
R tmpl/patchset.html =>
tmpl/components/patchset.html
+0,
-0
R tmpl/pr-header.html =>
tmpl/components/pr-header.html
+0,
-0
R tmpl/pr-list-item.html =>
tmpl/components/pr-list-item.html
+0,
-0
R tmpl/pr-status.html =>
tmpl/components/pr-status.html
+0,
-0
R tmpl/pr-table.html =>
tmpl/components/pr-table.html
+0,
-0
R tmpl/range-diff.html =>
tmpl/components/range-diff.html
+0,
-0
R tmpl/user-pill.html =>
tmpl/components/user-pill.html
+0,
-0
R tmpl/index.html =>
tmpl/pages/index.html
+0,
-0
R tmpl/pr-detail.html =>
tmpl/pages/pr.html
+0,
-0
R tmpl/repo-detail.html =>
tmpl/pages/repo.html
+0,
-0
R tmpl/user-detail.html =>
tmpl/pages/user.html
+0,
-0
M
web.go
+27,
-31
1@@ -27,8 +27,29 @@ import (
2 "github.com/gorilla/feeds"
3 )
4
5-//go:embed tmpl/*
6-var tmplFS embed.FS
7+var (
8+ //go:embed tmpl/*
9+ tmplFS embed.FS
10+ indexTmpl = getTemplate("index.html")
11+ prTmpl = getTemplate("pr.html")
12+ userTmpl = getTemplate("user.html")
13+ repoTmpl = getTemplate("repo.html")
14+)
15+
16+func getTemplate(page string) *template.Template {
17+ tmpl, err := template.New("").Funcs(template.FuncMap{
18+ "sha": shaFn,
19+ }).ParseFS(
20+ tmplFS,
21+ filepath.Join("tmpl", "pages", page),
22+ filepath.Join("tmpl", "components", "*.html"),
23+ filepath.Join("tmpl", "base.html"),
24+ )
25+ if err != nil {
26+ panic(err)
27+ }
28+ return tmpl.Lookup(page)
29+}
30
31 //go:embed static/*
32 var embedStaticFS embed.FS
33@@ -83,27 +104,6 @@ func shaFn(sha string) string {
34 return truncateSha(sha)
35 }
36
37-func getTemplate(file string) *template.Template {
38- tmpl, err := template.New("").Funcs(template.FuncMap{
39- "sha": shaFn,
40- }).ParseFS(
41- tmplFS,
42- filepath.Join("tmpl", file),
43- filepath.Join("tmpl", "user-pill.html"),
44- filepath.Join("tmpl", "patchset.html"),
45- filepath.Join("tmpl", "range-diff.html"),
46- filepath.Join("tmpl", "pr-header.html"),
47- filepath.Join("tmpl", "pr-list-item.html"),
48- filepath.Join("tmpl", "pr-table.html"),
49- filepath.Join("tmpl", "pr-status.html"),
50- filepath.Join("tmpl", "base.html"),
51- )
52- if err != nil {
53- panic(err)
54- }
55- return tmpl
56-}
57-
58 type LinkData struct {
59 Url template.URL
60 Text string
61@@ -323,8 +323,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
62 }
63
64 w.Header().Set("content-type", "text/html")
65- tmpl := getTemplate("index.html")
66- err = tmpl.ExecuteTemplate(w, "index.html", PrTableData{
67+ err = indexTmpl.Execute(w, PrTableData{
68 NumOpen: numOpen,
69 NumAccepted: numAccepted,
70 NumClosed: numClosed,
71@@ -422,8 +421,7 @@ func userDetailHandler(w http.ResponseWriter, r *http.Request) {
72 }
73
74 w.Header().Set("content-type", "text/html")
75- tmpl := getTemplate("user-detail.html")
76- err = tmpl.ExecuteTemplate(w, "user-detail.html", UserDetailData{
77+ err = userTmpl.Execute(w, UserDetailData{
78 Prs: prdata,
79 NumOpen: numOpen,
80 NumAccepted: numAccepted,
81@@ -498,8 +496,7 @@ func repoDetailHandler(w http.ResponseWriter, r *http.Request) {
82 }
83
84 w.Header().Set("content-type", "text/html")
85- tmpl := getTemplate("repo-detail.html")
86- err = tmpl.ExecuteTemplate(w, "repo-detail.html", RepoDetailData{
87+ err = repoTmpl.Execute(w, RepoDetailData{
88 Name: repo.Name,
89 UserID: user.ID,
90 Username: userName,
91@@ -772,7 +769,6 @@ func createPrDetail(page string) http.HandlerFunc {
92 }
93
94 w.Header().Set("content-type", "text/html")
95- tmpl := getTemplate("pr-detail.html")
96 pk, err := web.Backend.PubkeyToPublicKey(user.Pubkey)
97 if err != nil {
98 web.Logger.Error("cannot parse pubkey for pr user", "err", err)
99@@ -840,7 +836,7 @@ func createPrDetail(page string) http.HandlerFunc {
100
101 repoNs := web.Backend.CreateRepoNs(repoOwner.Name, repo.Name)
102 url := fmt.Sprintf("/r/%s/%s", repoOwner.Name, repo.Name)
103- err = tmpl.ExecuteTemplate(w, "pr-detail.html", PrDetailData{
104+ err = prTmpl.Execute(w, PrDetailData{
105 Page: "pr",
106 Repo: LinkData{
107 Url: template.URL(url),