/** * AltGit - altgit * * This file is licensed under the Affero General Public License version 3 or * later. See the COPYING file. * * @author Paolo Lulli * @copyright Paolo Lulli 2026 */ package routes import ( "altgit/internal/config" "altgit/internal/gitrepo" "altgit/internal/rendering" "fmt" "html/template" "net/http" "net/url" "strings" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" ) //TODO USE AS MIDDLEWARE func OIDCrootHandler() func(c *gin.Context) { return func(c *gin.Context) { if user := sessions.Default(c).Get("user"); user != nil { c.String(http.StatusOK, "Logged in as %v. Go to /profile or /logout", user) return } c.String(http.StatusOK, "Not logged in. Go to /login") } } // HandleIndex GET /:repo/ โ€” file list func HandleIndex(cfg config.AltGitConfiguration) gin.HandlerFunc { return func(c *gin.Context) { repoName, rp, ok := rendering.ResolveRepo(cfg, c.Param("repo")) if !ok { return } pd := rendering.BasePageData(c, repoName, rp, "files", template.HTML(fmt.Sprintf( `repos / %s`, url.PathEscape(repoName), template.HTMLEscapeString(repoName), ))) files, cmd, err := gitrepo.ListFiles(rp, pd.Branch) readmeHTML := rendering.RenderReadme(rp, pd.Branch) var body strings.Builder body.WriteString(fmt.Sprintf( `

%s

`+ `

branch: %s  ยท  %d files

`, template.HTMLEscapeString(repoName), template.HTMLEscapeString(pd.Branch), len(files), )) body.WriteString(fmt.Sprintf( `
$ git clone %s

`, resolveCloneUrl(cfg, c, repoName), resolveCloneUrl(cfg, c, repoName), )) if err != nil || len(files) == 0 { body.WriteString(`
No tracked files found on this branch.
`) } else { body.WriteString(``) for _, f := range files { parts := strings.Split(f.Path, "/") name := parts[len(parts)-1] dir := "" if len(parts) > 1 { dir = strings.Join(parts[:len(parts)-1], "/") + "/" } body.WriteString(fmt.Sprintf( ``+ ``, rendering.FileIcon(name), template.HTMLEscapeString(dir), url.PathEscape(repoName), url.QueryEscape(f.Path), url.QueryEscape(pd.Branch), template.HTMLEscapeString(name), f.CommitCount, )) } body.WriteString("
FileCommits
%s`+ `%s`+ `%s%d
") } if readmeHTML != "" { body.WriteString(`
`) body.WriteString(`
`) body.WriteString(`๐Ÿ“„ README.md`) body.WriteString(`
`) body.WriteString(`
`) body.WriteString(readmeHTML) body.WriteString(`
`) } pd.Title = repoName + " โ€” files" pd.GitCmd = cmd pd.Body = template.HTML(body.String()) rendering.RenderPage(cfg, c, pd) } }