Home

ugit @1187c9eeb3b9f062ff90370ccd6c5e230067dd03 - refs - log -
-
https://git.jolheiser.com/ugit.git
The code powering this h*ckin' site
ugit / internal / http / index.go
- raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package http

import (
	"net/http"
	"os"
	"sort"
	"strings"
	"time"

	"go.jolheiser.com/ugit/internal/git"
	"go.jolheiser.com/ugit/internal/html"
	"go.jolheiser.com/ugit/internal/http/httperr"
)

func (rh repoHandler) index(w http.ResponseWriter, r *http.Request) error {
	repoPaths, err := os.ReadDir(rh.s.RepoDir)
	if err != nil {
		return httperr.Error(err)
	}

	repos := make([]*git.Repo, 0, len(repoPaths))
	for _, repoName := range repoPaths {
		if !strings.HasSuffix(repoName.Name(), ".git") {
			continue
		}
		repo, err := git.NewRepo(rh.s.RepoDir, repoName.Name())
		if err != nil {
			return httperr.Error(err)
		}
		if !repo.Meta.Private {
			repos = append(repos, repo)
		}
	}
	sort.Slice(repos, func(i, j int) bool {
		var when1, when2 time.Time
		if c, err := repos[i].LastCommit(); err == nil {
			when1 = c.When
		}
		if c, err := repos[j].LastCommit(); err == nil {
			when2 = c.When
		}
		return when1.After(when2)
	})

	links := make([]html.IndexLink, 0, len(rh.s.Profile.Links))
	for _, link := range rh.s.Profile.Links {
		links = append(links, html.IndexLink{
			Name: link.Name,
			URL:  link.URL,
		})
	}

	if err := html.Index(html.IndexContext{
		BaseContext: rh.baseContext(),
		Profile: html.IndexProfile{
			Username: rh.s.Profile.Username,
			Email:    rh.s.Profile.Email,
			Links:    links,
		},
		Repos: repos,
	}).Render(r.Context(), w); err != nil {
		return httperr.Error(err)
	}

	return nil
}