routes: serve static content from /static

This commit is contained in:
Anirudh Oppiliappan 2022-12-17 12:45:21 +05:30
parent b833d2f73d
commit d083d5d72e
No known key found for this signature in database
GPG Key ID: 8A93F96F78C5D4C4
5 changed files with 24 additions and 13 deletions

View File

@ -8,8 +8,9 @@ repo:
mainBranch: mainBranch:
- master - master
- main - main
template: dirs:
dir: ./templates templates: ./templates
static: ./static
meta: meta:
title: git good title: git good
description: i think it's a skill issue description: i think it's a skill issue

View File

@ -13,9 +13,10 @@ type Config struct {
Readme []string `yaml:"readme"` Readme []string `yaml:"readme"`
MainBranch []string `yaml:"mainBranch"` MainBranch []string `yaml:"mainBranch"`
} `yaml:"repo"` } `yaml:"repo"`
Template struct { Dirs struct {
Dir string `yaml:"dir"` Templates string `yaml:"templates"`
} `yaml:"template"` Static string `yaml:"static"`
} `yaml:"dirs"`
Meta struct { Meta struct {
Title string `yaml:"title"` Title string `yaml:"title"`
Description string `yaml:"description"` Description string `yaml:"description"`

View File

@ -56,6 +56,7 @@ func Handlers(c *config.Config) *flow.Mux {
}) })
mux.HandleFunc("/", d.Index, "GET") mux.HandleFunc("/", d.Index, "GET")
mux.HandleFunc("/static/:file", d.ServeStatic, "GET")
mux.HandleFunc("/:name", dw.Multiplex, "GET", "POST") mux.HandleFunc("/:name", dw.Multiplex, "GET", "POST")
mux.HandleFunc("/:name/tree/:ref/...", d.RepoTree, "GET") mux.HandleFunc("/:name/tree/:ref/...", d.RepoTree, "GET")
mux.HandleFunc("/:name/blob/:ref/...", d.FileContent, "GET") mux.HandleFunc("/:name/blob/:ref/...", d.FileContent, "GET")

View File

@ -42,6 +42,7 @@ func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
d.Write500(w) d.Write500(w)
log.Println(err) log.Println(err)
return
} }
var desc string 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)) t := template.Must(template.ParseGlob(tpath))
data := make(map[string]interface{}) data := make(map[string]interface{})
@ -186,7 +187,7 @@ func (d *deps) Log(w http.ResponseWriter, r *http.Request) {
return return
} }
tpath := filepath.Join(d.c.Template.Dir, "*") tpath := filepath.Join(d.c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath)) t := template.Must(template.ParseGlob(tpath))
data := make(map[string]interface{}) data := make(map[string]interface{})
@ -219,7 +220,7 @@ func (d *deps) Diff(w http.ResponseWriter, r *http.Request) {
return return
} }
tpath := filepath.Join(d.c.Template.Dir, "*") tpath := filepath.Join(d.c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath)) t := template.Must(template.ParseGlob(tpath))
data := make(map[string]interface{}) data := make(map[string]interface{})
@ -260,7 +261,7 @@ func (d *deps) Refs(w http.ResponseWriter, r *http.Request) {
return return
} }
tpath := filepath.Join(d.c.Template.Dir, "*") tpath := filepath.Join(d.c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath)) t := template.Must(template.ParseGlob(tpath))
data := make(map[string]interface{}) data := make(map[string]interface{})
@ -275,3 +276,10 @@ func (d *deps) Refs(w http.ResponseWriter, r *http.Request) {
return 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)
}

View File

@ -13,7 +13,7 @@ import (
) )
func (d *deps) Write404(w http.ResponseWriter) { 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)) t := template.Must(template.ParseGlob(tpath))
w.WriteHeader(404) w.WriteHeader(404)
if err := t.ExecuteTemplate(w, "404", nil); err != nil { 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) { 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)) t := template.Must(template.ParseGlob(tpath))
w.WriteHeader(500) w.WriteHeader(500)
if err := t.ExecuteTemplate(w, "500", nil); err != nil { 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) { 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)) t := template.Must(template.ParseGlob(tpath))
data["files"] = files 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) { 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)) t := template.Must(template.ParseGlob(tpath))
lc, err := countLines(strings.NewReader(content)) lc, err := countLines(strings.NewReader(content))