routes: serve static content from /static
Anirudh Oppiliappan x@icyphox.sh
Sat, 17 Dec 2022 12:45:21 +0530
5 files changed, 24 insertions(+), 13 deletions(-)
M config.yaml → config.yaml
@@ -8,8 +8,9 @@ - README.md mainBranch: - master - main -template: - dir: ./templates +dirs: + templates: ./templates + static: ./static meta: title: git good description: i think it's a skill issue
M config/config.go → config/config.go
@@ -13,9 +13,10 @@ ScanPath string `yaml:"scanPath"` Readme []string `yaml:"readme"` MainBranch []string `yaml:"mainBranch"` } `yaml:"repo"` - Template struct { - Dir string `yaml:"dir"` - } `yaml:"template"` + Dirs struct { + Templates string `yaml:"templates"` + Static string `yaml:"static"` + } `yaml:"dirs"` Meta struct { Title string `yaml:"title"` Description string `yaml:"description"`
M routes/handler.go → routes/handler.go
@@ -56,6 +56,7 @@ d.Write404(w) }) mux.HandleFunc("/", d.Index, "GET") + mux.HandleFunc("/static/:file", d.ServeStatic, "GET") mux.HandleFunc("/:name", dw.Multiplex, "GET", "POST") mux.HandleFunc("/:name/tree/:ref/...", d.RepoTree, "GET") mux.HandleFunc("/:name/blob/:ref/...", d.FileContent, "GET")
M routes/routes.go → routes/routes.go
@@ -42,6 +42,7 @@ c, err := gr.LastCommit() if err != nil { d.Write500(w) log.Println(err) + return } var desc string @@ -59,7 +60,7 @@ Idle: humanize.Time(c.Author.When), }) } - tpath := filepath.Join(d.c.Template.Dir, "*") + tpath := filepath.Join(d.c.Dirs.Templates, "*") t := template.Must(template.ParseGlob(tpath)) data := make(map[string]interface{}) @@ -186,7 +187,7 @@ log.Println(err) return } - tpath := filepath.Join(d.c.Template.Dir, "*") + tpath := filepath.Join(d.c.Dirs.Templates, "*") t := template.Must(template.ParseGlob(tpath)) data := make(map[string]interface{}) @@ -219,7 +220,7 @@ log.Println(err) return } - tpath := filepath.Join(d.c.Template.Dir, "*") + tpath := filepath.Join(d.c.Dirs.Templates, "*") t := template.Must(template.ParseGlob(tpath)) data := make(map[string]interface{}) @@ -260,7 +261,7 @@ d.Write500(w) return } - tpath := filepath.Join(d.c.Template.Dir, "*") + tpath := filepath.Join(d.c.Dirs.Templates, "*") t := template.Must(template.ParseGlob(tpath)) data := make(map[string]interface{}) @@ -275,3 +276,10 @@ log.Println(err) return } } + +func (d *deps) ServeStatic(w http.ResponseWriter, r *http.Request) { + f := flow.Param(r.Context(), "file") + f = filepath.Clean(filepath.Join(d.c.Dirs.Static, f)) + + http.ServeFile(w, r, f) +}
M routes/template.go → routes/template.go
@@ -13,7 +13,7 @@ "icyphox.sh/legit/git" ) func (d *deps) Write404(w http.ResponseWriter) { - tpath := filepath.Join(d.c.Template.Dir, "*") + tpath := filepath.Join(d.c.Dirs.Templates, "*") t := template.Must(template.ParseGlob(tpath)) w.WriteHeader(404) if err := t.ExecuteTemplate(w, "404", nil); err != nil { @@ -22,7 +22,7 @@ } } func (d *deps) Write500(w http.ResponseWriter) { - tpath := filepath.Join(d.c.Template.Dir, "*") + tpath := filepath.Join(d.c.Dirs.Templates, "*") t := template.Must(template.ParseGlob(tpath)) w.WriteHeader(500) if err := t.ExecuteTemplate(w, "500", nil); err != nil { @@ -31,7 +31,7 @@ } } func (d *deps) listFiles(files []git.NiceTree, data map[string]any, w http.ResponseWriter) { - tpath := filepath.Join(d.c.Template.Dir, "*") + tpath := filepath.Join(d.c.Dirs.Templates, "*") t := template.Must(template.ParseGlob(tpath)) data["files"] = files @@ -62,7 +62,7 @@ } } func (d *deps) showFile(content string, data map[string]any, w http.ResponseWriter) { - tpath := filepath.Join(d.c.Template.Dir, "*") + tpath := filepath.Join(d.c.Dirs.Templates, "*") t := template.Must(template.ParseGlob(tpath)) lc, err := countLines(strings.NewReader(content))