update repository list on timer instead of generating per request
3 files changed, 19 insertions(+), 8 deletions(-)
changed files
M routes/handler.go → routes/handler.go
@@ -13,6 +13,7 @@ "github.com/fsnotify/fsnotify" "github.com/savsgio/atreugo/v11" "alin.ovh/elgit/config" + "alin.ovh/elgit/data" ) var ErrMethodNotAllowed = fmt.Errorf("that's not possible")@@ -26,8 +27,9 @@ log.Fatal(err) } d := deps{ - c, - projects, + c: c, + projects: projects, + repos: &data.Entries{}, } addr := net.JoinHostPort(c.Server.Host, strconv.FormatInt(c.Server.Port, 10))@@ -60,6 +62,13 @@ err = watcher.Add(plist) if err != nil { log.Fatal(err) } + + go func() { + d.UpdateRepos() + for range time.Tick(time.Minute) { + d.UpdateRepos() + } + }() go func() { for {
M routes/routes.go → routes/routes.go
@@ -10,6 +10,7 @@ "strconv" "strings" "alin.ovh/elgit/config" + "alin.ovh/elgit/data" "alin.ovh/elgit/git" "alin.ovh/elgit/templates" "github.com/microcosm-cc/bluemonday"@@ -21,10 +22,11 @@ type deps struct { c *config.Config projects []string + repos *data.Entries } func (d *deps) Index(rc *atreugo.RequestCtx) error { - repos := d.getAllRepos() + pageData := templates.PageData{ Meta: d.c.Meta,@@ -32,7 +34,7 @@ } rc.SetContentType("text/html; charset=utf-8") - return templates.IndexPage(pageData, repos).Render(rc) + return templates.IndexPage(pageData, d.repos).Render(rc) } func (d *deps) RepoIndex(rc *atreugo.RequestCtx) error {
M routes/util.go → routes/util.go
@@ -4,6 +4,7 @@ import ( "bufio" "log" "os" + "path" "path/filepath" "slices" "strings"@@ -11,7 +12,6 @@ "alin.ovh/elgit/data" "alin.ovh/elgit/git" securejoin "github.com/cyphar/filepath-securejoin" - "github.com/dimfeld/httptreemux/v5" "github.com/savsgio/atreugo/v11" )@@ -47,7 +47,7 @@ func (d *deps) GetCleanPath(name string) (string, error) { return securejoin.SecureJoin( filepath.Join(d.c.Repo.Root, "repositories"), - httptreemux.Clean(name)+".git", + path.Clean(name)+".git", ) }@@ -71,7 +71,7 @@ return projects, nil } -func (d *deps) getAllRepos() *data.Entries { +func (d *deps) UpdateRepos() { entries := &data.Entries{ Children: []*data.Entry{}, Map: map[string]*data.Entry{},@@ -111,7 +111,7 @@ } entries.Sort() - return entries + d.repos = entries } func setContentDisposition(rc *atreugo.RequestCtx, name string) {