From d083d5d72e4bf496b8152d14986818a5b63fe301 Mon Sep 17 00:00:00 2001 From: Anirudh Oppiliappan Date: Sat, 17 Dec 2022 12:45:21 +0530 Subject: [PATCH] routes: serve static content from /static --- config.yaml | 5 +++-- config/config.go | 7 ++++--- routes/handler.go | 1 + routes/routes.go | 16 ++++++++++++---- routes/template.go | 8 ++++---- 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/config.yaml b/config.yaml index 59e903a..5dbe023 100644 --- a/config.yaml +++ b/config.yaml @@ -8,8 +8,9 @@ repo: mainBranch: - master - main -template: - dir: ./templates +dirs: + templates: ./templates + static: ./static meta: title: git good description: i think it's a skill issue diff --git a/config/config.go b/config/config.go index 278e17e..4b89dc1 100644 --- a/config/config.go +++ b/config/config.go @@ -13,9 +13,10 @@ type Config struct { 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"` diff --git a/routes/handler.go b/routes/handler.go index 7d9266b..d230773 100644 --- a/routes/handler.go +++ b/routes/handler.go @@ -56,6 +56,7 @@ func Handlers(c *config.Config) *flow.Mux { }) 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") diff --git a/routes/routes.go b/routes/routes.go index aab28f5..5a8d686 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -42,6 +42,7 @@ func (d *deps) Index(w http.ResponseWriter, r *http.Request) { if err != nil { d.Write500(w) log.Println(err) + return } var desc string @@ -59,7 +60,7 @@ func (d *deps) Index(w http.ResponseWriter, r *http.Request) { }) } - 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 @@ func (d *deps) Log(w http.ResponseWriter, r *http.Request) { 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 @@ func (d *deps) Diff(w http.ResponseWriter, r *http.Request) { 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 @@ func (d *deps) Refs(w http.ResponseWriter, r *http.Request) { 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 @@ func (d *deps) Refs(w http.ResponseWriter, r *http.Request) { 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) +} diff --git a/routes/template.go b/routes/template.go index 3d1e1cf..dc1ec35 100644 --- a/routes/template.go +++ b/routes/template.go @@ -13,7 +13,7 @@ import ( ) 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) Write404(w http.ResponseWriter) { } 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) Write500(w http.ResponseWriter) { } 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 countLines(r io.Reader) (int, error) { } 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))