From 01f27147baf80e2222927ddca9369c7d99b4ff3c Mon Sep 17 00:00:00 2001 From: Anirudh Oppiliappan Date: Mon, 12 Dec 2022 21:33:09 +0530 Subject: [PATCH] routes: index view --- config.yaml | 2 +- git/git.go | 8 +++++++ routes/routes.go | 52 +++++++++++++++++++++++++++++++++++++------- templates/500.html | 2 +- templates/index.html | 27 +++++++++++++++++++++++ templates/nav.html | 4 ++++ templates/repo.html | 2 +- 7 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 templates/index.html diff --git a/config.yaml b/config.yaml index 25fc992..2381bf0 100644 --- a/config.yaml +++ b/config.yaml @@ -1,5 +1,5 @@ git: - scanPath: /home/icy/code/tmp + scanPath: /home/icy/code/tmp/testrepos readme: - readme - README diff --git a/git/git.go b/git/git.go index 1a7ade2..34a25e5 100644 --- a/git/git.go +++ b/git/git.go @@ -52,6 +52,14 @@ func (g *GitRepo) Commits() ([]*object.Commit, error) { return commits, nil } +func (g *GitRepo) LastCommit() (*object.Commit, error) { + c, err := g.r.CommitObject(g.h) + if err != nil { + return nil, fmt.Errorf("last commit: %w", err) + } + return c, nil +} + func (g *GitRepo) FileContent(path string) (string, error) { c, err := g.r.CommitObject(g.h) if err != nil { diff --git a/routes/routes.go b/routes/routes.go index dd7d46e..ebb1622 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -4,7 +4,9 @@ import ( "html/template" "log" "net/http" + "os" "path/filepath" + "time" "github.com/alexedwards/flow" "icyphox.sh/legit/config" @@ -16,14 +18,50 @@ type deps struct { } func (d *deps) Index(w http.ResponseWriter, r *http.Request) { + dirs, err := os.ReadDir(d.c.Git.ScanPath) + if err != nil { + d.Write500(w) + log.Printf("reading scan path: %s", err) + return + } + repoInfo := make(map[string]time.Time) + + for _, dir := range dirs { + path := filepath.Join(d.c.Git.ScanPath, dir.Name()) + gr, err := git.Open(path, "") + if err != nil { + d.Write500(w) + log.Printf("opening dir %s: %s", path, err) + return + } + + c, err := gr.LastCommit() + if err != nil { + d.Write500(w) + log.Println(err) + } + + repoInfo[dir.Name()] = c.Author.When + } + + tpath := filepath.Join(d.c.Template.Dir, "*") + t := template.Must(template.ParseGlob(tpath)) + + data := make(map[string]interface{}) + data["meta"] = d.c.Meta + data["info"] = repoInfo + + if err := t.ExecuteTemplate(w, "index", data); err != nil { + log.Println(err) + return + } } func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) { name := flow.Param(r.Context(), "name") name = filepath.Clean(name) - // TODO: remove .git - path := filepath.Join(d.c.Git.ScanPath, name+".git") + path := filepath.Join(d.c.Git.ScanPath, name) gr, err := git.Open(path, "") if err != nil { d.Write404(w) @@ -65,8 +103,7 @@ func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) { ref := flow.Param(r.Context(), "ref") name = filepath.Clean(name) - // TODO: remove .git - path := filepath.Join(d.c.Git.ScanPath, name+".git") + path := filepath.Join(d.c.Git.ScanPath, name) gr, err := git.Open(path, ref) if err != nil { d.Write404(w) @@ -95,8 +132,7 @@ func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) { ref := flow.Param(r.Context(), "ref") name = filepath.Clean(name) - // TODO: remove .git - path := filepath.Join(d.c.Git.ScanPath, name+".git") + path := filepath.Join(d.c.Git.ScanPath, name) gr, err := git.Open(path, ref) if err != nil { d.Write404(w) @@ -116,7 +152,7 @@ func (d *deps) Log(w http.ResponseWriter, r *http.Request) { name := flow.Param(r.Context(), "name") ref := flow.Param(r.Context(), "ref") - path := filepath.Join(d.c.Git.ScanPath, name+".git") + path := filepath.Join(d.c.Git.ScanPath, name) gr, err := git.Open(path, ref) if err != nil { d.Write404(w) @@ -149,7 +185,7 @@ func (d *deps) Diff(w http.ResponseWriter, r *http.Request) { name := flow.Param(r.Context(), "name") ref := flow.Param(r.Context(), "ref") - path := filepath.Join(d.c.Git.ScanPath, name+".git") + path := filepath.Join(d.c.Git.ScanPath, name) gr, err := git.Open(path, ref) if err != nil { d.Write404(w) diff --git a/templates/500.html b/templates/500.html index 5495d31..72e784c 100644 --- a/templates/500.html +++ b/templates/500.html @@ -1,7 +1,7 @@ {{ define "500" }} 500 -{{ template "header" . }} +{{ template "head" . }} 500 — something broke! diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..91f18b7 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,27 @@ +{{ define "index" }} + +{{ template "head" . }} + +
+

{{ .meta.Title }}

+

{{ .meta.Description }}

+
+ + {{ template "nav" . }} +
+ + + + + + {{ range $repo, $lc := .info }} + + + + + {{ end }} +
repositorylast active
{{ $repo }}{{ $lc }}
+
+ + +{{ end }} diff --git a/templates/nav.html b/templates/nav.html index 20dca50..a8fa84a 100644 --- a/templates/nav.html +++ b/templates/nav.html @@ -2,9 +2,13 @@ {{ end }} diff --git a/templates/repo.html b/templates/repo.html index a033f0d..ddc3f01 100644 --- a/templates/repo.html +++ b/templates/repo.html @@ -37,7 +37,7 @@ {{ if $parent }} {{ .Name }}/ {{ else }} - {{ .Name }}/ + {{ .Name }}/ {{ end }}