routes: index view

This commit is contained in:
Anirudh Oppiliappan 2022-12-12 21:33:09 +05:30
parent 82b8afe19b
commit 01f27147ba
No known key found for this signature in database
GPG key ID: 8A93F96F78C5D4C4
7 changed files with 86 additions and 11 deletions

View file

@ -1,5 +1,5 @@
git: git:
scanPath: /home/icy/code/tmp scanPath: /home/icy/code/tmp/testrepos
readme: readme:
- readme - readme
- README - README

View file

@ -52,6 +52,14 @@ func (g *GitRepo) Commits() ([]*object.Commit, error) {
return commits, nil 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) { func (g *GitRepo) FileContent(path string) (string, error) {
c, err := g.r.CommitObject(g.h) c, err := g.r.CommitObject(g.h)
if err != nil { if err != nil {

View file

@ -4,7 +4,9 @@ import (
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
"os"
"path/filepath" "path/filepath"
"time"
"github.com/alexedwards/flow" "github.com/alexedwards/flow"
"icyphox.sh/legit/config" "icyphox.sh/legit/config"
@ -16,14 +18,50 @@ type deps struct {
} }
func (d *deps) Index(w http.ResponseWriter, r *http.Request) { 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) { func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
name := flow.Param(r.Context(), "name") name := flow.Param(r.Context(), "name")
name = filepath.Clean(name) name = filepath.Clean(name)
// TODO: remove .git path := filepath.Join(d.c.Git.ScanPath, name)
path := filepath.Join(d.c.Git.ScanPath, name+".git")
gr, err := git.Open(path, "") gr, err := git.Open(path, "")
if err != nil { if err != nil {
d.Write404(w) d.Write404(w)
@ -65,8 +103,7 @@ func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) {
ref := flow.Param(r.Context(), "ref") ref := flow.Param(r.Context(), "ref")
name = filepath.Clean(name) name = filepath.Clean(name)
// TODO: remove .git path := filepath.Join(d.c.Git.ScanPath, name)
path := filepath.Join(d.c.Git.ScanPath, name+".git")
gr, err := git.Open(path, ref) gr, err := git.Open(path, ref)
if err != nil { if err != nil {
d.Write404(w) d.Write404(w)
@ -95,8 +132,7 @@ func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) {
ref := flow.Param(r.Context(), "ref") ref := flow.Param(r.Context(), "ref")
name = filepath.Clean(name) name = filepath.Clean(name)
// TODO: remove .git path := filepath.Join(d.c.Git.ScanPath, name)
path := filepath.Join(d.c.Git.ScanPath, name+".git")
gr, err := git.Open(path, ref) gr, err := git.Open(path, ref)
if err != nil { if err != nil {
d.Write404(w) d.Write404(w)
@ -116,7 +152,7 @@ func (d *deps) Log(w http.ResponseWriter, r *http.Request) {
name := flow.Param(r.Context(), "name") name := flow.Param(r.Context(), "name")
ref := flow.Param(r.Context(), "ref") 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) gr, err := git.Open(path, ref)
if err != nil { if err != nil {
d.Write404(w) d.Write404(w)
@ -149,7 +185,7 @@ func (d *deps) Diff(w http.ResponseWriter, r *http.Request) {
name := flow.Param(r.Context(), "name") name := flow.Param(r.Context(), "name")
ref := flow.Param(r.Context(), "ref") 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) gr, err := git.Open(path, ref)
if err != nil { if err != nil {
d.Write404(w) d.Write404(w)

View file

@ -1,7 +1,7 @@
{{ define "500" }} {{ define "500" }}
<html> <html>
<title>500</title> <title>500</title>
{{ template "header" . }} {{ template "head" . }}
<body> <body>
500 &mdash; something broke! 500 &mdash; something broke!
</body> </body>

27
templates/index.html Normal file
View file

@ -0,0 +1,27 @@
{{ define "index" }}
<html>
{{ template "head" . }}
<header>
<h1>{{ .meta.Title }}</h1>
<h2>{{ .meta.Description }}</h2>
</header>
<body>
{{ template "nav" . }}
<main>
<table>
<tr>
<td>repository</td>
<td>last active</td>
</tr>
{{ range $repo, $lc := .info }}
<tr>
<td><a href="/{{ $repo }}">{{ $repo }}</a></td>
<td>{{ $lc }}</td>
</tr>
{{ end }}
</table>
</main>
</body>
</html>
{{ end }}

View file

@ -2,9 +2,13 @@
<nav> <nav>
<ul> <ul>
<li><a href="/">all repos</a> <li><a href="/">all repos</a>
{{ if .name }}
<li><a href="/{{ .name }}">{{ .name }}</a> <li><a href="/{{ .name }}">{{ .name }}</a>
{{ end }}
{{ if .ref }}
<li><a href="/{{ .name }}/tree/{{ .ref }}/">tree</a> <li><a href="/{{ .name }}/tree/{{ .ref }}/">tree</a>
<li><a href="/{{ .name }}/log/{{ .ref }}">log</a> <li><a href="/{{ .name }}/log/{{ .ref }}">log</a>
{{ end }}
</ul> </ul>
</nav> </nav>
{{ end }} {{ end }}

View file

@ -37,7 +37,7 @@
{{ if $parent }} {{ if $parent }}
<a href="/{{ $repo }}/tree/{{ $ref }}/{{ $parent }}/{{ .Name }}">{{ .Name }}/</a> <a href="/{{ $repo }}/tree/{{ $ref }}/{{ $parent }}/{{ .Name }}">{{ .Name }}/</a>
{{ else }} {{ else }}
<a href="{{ $repo }}/tree/{{ $ref }}/{{ .Name }}">{{ .Name }}/</a> <a href="/{{ $repo }}/tree/{{ $ref }}/{{ .Name }}">{{ .Name }}/</a>
{{ end }} {{ end }}
</td> </td>
</tr> </tr>