legit/routes/routes.go

309 lines
6.1 KiB
Go
Raw Normal View History

2022-12-11 05:52:47 +00:00
package routes
import (
2022-12-11 15:47:04 +00:00
"html/template"
2022-12-11 05:52:47 +00:00
"log"
"net/http"
2022-12-12 16:03:09 +00:00
"os"
2022-12-11 05:52:47 +00:00
"path/filepath"
"github.com/alexedwards/flow"
"github.com/dustin/go-humanize"
2022-12-11 05:52:47 +00:00
"icyphox.sh/legit/config"
"icyphox.sh/legit/git"
)
type deps struct {
c *config.Config
}
2022-12-12 15:23:58 +00:00
func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
2022-12-13 04:28:40 +00:00
dirs, err := os.ReadDir(d.c.Repo.ScanPath)
2022-12-12 16:03:09 +00:00
if err != nil {
d.Write500(w)
log.Printf("reading scan path: %s", err)
return
}
type info struct{ Name, Desc, Idle string }
infos := []info{}
2022-12-12 16:03:09 +00:00
for _, dir := range dirs {
2022-12-13 04:28:40 +00:00
path := filepath.Join(d.c.Repo.ScanPath, dir.Name())
2022-12-12 16:03:09 +00:00
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)
return
2022-12-12 16:03:09 +00:00
}
2022-12-17 16:03:04 +00:00
desc := getDescription(path)
infos = append(infos, info{
Name: dir.Name(),
Desc: desc,
Idle: humanize.Time(c.Author.When),
})
2022-12-12 16:03:09 +00:00
}
tpath := filepath.Join(d.c.Dirs.Templates, "*")
2022-12-12 16:03:09 +00:00
t := template.Must(template.ParseGlob(tpath))
data := make(map[string]interface{})
data["meta"] = d.c.Meta
data["info"] = infos
2022-12-12 15:23:58 +00:00
2022-12-12 16:03:09 +00:00
if err := t.ExecuteTemplate(w, "index", data); err != nil {
log.Println(err)
return
}
2022-12-12 15:23:58 +00:00
}
func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
name := flow.Param(r.Context(), "name")
name = filepath.Clean(name)
2022-12-13 04:28:40 +00:00
path := filepath.Join(d.c.Repo.ScanPath, name)
2022-12-11 15:47:04 +00:00
gr, err := git.Open(path, "")
if err != nil {
2022-12-12 15:23:58 +00:00
d.Write404(w)
return
}
2022-12-17 16:03:04 +00:00
commits, err := gr.Commits()
if err != nil {
2022-12-12 15:23:58 +00:00
d.Write500(w)
log.Println(err)
return
}
2022-12-12 15:23:58 +00:00
var readmeContent string
2022-12-13 04:28:40 +00:00
for _, readme := range d.c.Repo.Readme {
2022-12-12 15:23:58 +00:00
readmeContent, _ = gr.FileContent(readme)
if readmeContent != "" {
break
}
}
if readmeContent == "" {
log.Printf("no readme found for %s", name)
}
2022-12-13 04:28:40 +00:00
mainBranch, err := gr.FindMainBranch(d.c.Repo.MainBranch)
if err != nil {
d.Write500(w)
log.Println(err)
return
}
2022-12-17 16:03:04 +00:00
tpath := filepath.Join(d.c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath))
if len(commits) >= 3 {
commits = commits[:3]
2022-12-17 16:03:04 +00:00
}
2022-12-11 15:47:04 +00:00
data := make(map[string]any)
data["name"] = name
2022-12-13 04:28:40 +00:00
data["ref"] = mainBranch
2022-12-12 15:23:58 +00:00
data["readme"] = readmeContent
2022-12-17 16:03:04 +00:00
data["commits"] = commits
data["desc"] = getDescription(path)
if err := t.ExecuteTemplate(w, "repo", data); err != nil {
log.Println(err)
return
}
return
}
2022-12-11 08:48:39 +00:00
func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) {
2022-12-11 05:52:47 +00:00
name := flow.Param(r.Context(), "name")
treePath := flow.Param(r.Context(), "...")
ref := flow.Param(r.Context(), "ref")
2022-12-11 05:52:47 +00:00
name = filepath.Clean(name)
2022-12-13 04:28:40 +00:00
path := filepath.Join(d.c.Repo.ScanPath, name)
2022-12-11 15:47:04 +00:00
gr, err := git.Open(path, ref)
2022-12-11 05:52:47 +00:00
if err != nil {
2022-12-12 15:23:58 +00:00
d.Write404(w)
2022-12-11 05:52:47 +00:00
return
}
2022-12-11 15:47:04 +00:00
files, err := gr.FileTree(treePath)
if err != nil {
2022-12-12 15:23:58 +00:00
d.Write500(w)
log.Println(err)
return
}
2022-12-11 15:47:04 +00:00
data := make(map[string]any)
data["name"] = name
data["ref"] = ref
data["parent"] = treePath
data["desc"] = getDescription(path)
2022-12-11 05:52:47 +00:00
2022-12-11 15:47:04 +00:00
d.listFiles(files, data, w)
return
}
2022-12-11 08:48:39 +00:00
func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) {
name := flow.Param(r.Context(), "name")
treePath := flow.Param(r.Context(), "...")
ref := flow.Param(r.Context(), "ref")
2022-12-11 05:52:47 +00:00
2022-12-11 08:48:39 +00:00
name = filepath.Clean(name)
2022-12-13 04:28:40 +00:00
path := filepath.Join(d.c.Repo.ScanPath, name)
2022-12-11 15:47:04 +00:00
gr, err := git.Open(path, ref)
2022-12-11 08:48:39 +00:00
if err != nil {
2022-12-12 15:23:58 +00:00
d.Write404(w)
2022-12-11 08:48:39 +00:00
return
}
2022-12-11 05:52:47 +00:00
2022-12-11 15:47:04 +00:00
contents, err := gr.FileContent(treePath)
data := make(map[string]any)
data["name"] = name
data["ref"] = ref
data["desc"] = getDescription(path)
2022-12-18 05:44:38 +00:00
data["path"] = treePath
2022-12-11 15:47:04 +00:00
d.showFile(contents, data, w)
return
}
func (d *deps) Log(w http.ResponseWriter, r *http.Request) {
name := flow.Param(r.Context(), "name")
ref := flow.Param(r.Context(), "ref")
2022-12-13 04:28:40 +00:00
path := filepath.Join(d.c.Repo.ScanPath, name)
2022-12-11 15:47:04 +00:00
gr, err := git.Open(path, ref)
if err != nil {
2022-12-12 15:23:58 +00:00
d.Write404(w)
2022-12-11 15:47:04 +00:00
return
}
commits, err := gr.Commits()
2022-12-11 08:48:39 +00:00
if err != nil {
2022-12-12 15:23:58 +00:00
d.Write500(w)
2022-12-11 05:52:47 +00:00
log.Println(err)
return
}
2022-12-11 08:48:39 +00:00
tpath := filepath.Join(d.c.Dirs.Templates, "*")
2022-12-11 15:47:04 +00:00
t := template.Must(template.ParseGlob(tpath))
data := make(map[string]interface{})
data["commits"] = commits
data["meta"] = d.c.Meta
data["name"] = name
data["ref"] = ref
data["desc"] = getDescription(path)
2022-12-11 15:47:04 +00:00
if err := t.ExecuteTemplate(w, "log", data); err != nil {
log.Println(err)
return
}
2022-12-11 05:52:47 +00:00
}
2022-12-12 11:47:49 +00:00
func (d *deps) Diff(w http.ResponseWriter, r *http.Request) {
name := flow.Param(r.Context(), "name")
ref := flow.Param(r.Context(), "ref")
2022-12-13 04:28:40 +00:00
path := filepath.Join(d.c.Repo.ScanPath, name)
2022-12-12 11:47:49 +00:00
gr, err := git.Open(path, ref)
if err != nil {
2022-12-12 15:23:58 +00:00
d.Write404(w)
2022-12-12 11:47:49 +00:00
return
}
diff, err := gr.Diff()
if err != nil {
2022-12-12 15:23:58 +00:00
d.Write500(w)
2022-12-12 11:47:49 +00:00
log.Println(err)
return
}
tpath := filepath.Join(d.c.Dirs.Templates, "*")
2022-12-12 11:47:49 +00:00
t := template.Must(template.ParseGlob(tpath))
data := make(map[string]interface{})
data["commit"] = diff.Commit
data["stat"] = diff.Stat
data["diff"] = diff.Diff
data["meta"] = d.c.Meta
data["name"] = name
data["ref"] = ref
data["desc"] = getDescription(path)
2022-12-12 11:47:49 +00:00
if err := t.ExecuteTemplate(w, "commit", data); err != nil {
log.Println(err)
return
}
}
2022-12-12 16:28:47 +00:00
func (d *deps) Refs(w http.ResponseWriter, r *http.Request) {
name := flow.Param(r.Context(), "name")
2022-12-13 04:28:40 +00:00
path := filepath.Join(d.c.Repo.ScanPath, name)
2022-12-12 16:28:47 +00:00
gr, err := git.Open(path, "")
if err != nil {
d.Write404(w)
return
}
tags, err := gr.Tags()
if err != nil {
// Non-fatal, we *should* have at least one branch to show.
log.Println(err)
}
branches, err := gr.Branches()
if err != nil {
log.Println(err)
d.Write500(w)
return
}
tpath := filepath.Join(d.c.Dirs.Templates, "*")
2022-12-12 16:28:47 +00:00
t := template.Must(template.ParseGlob(tpath))
data := make(map[string]interface{})
data["meta"] = d.c.Meta
data["name"] = name
data["branches"] = branches
data["tags"] = tags
data["desc"] = getDescription(path)
2022-12-12 16:28:47 +00:00
if err := t.ExecuteTemplate(w, "refs", data); err != nil {
log.Println(err)
return
}
}
func (d *deps) ServeStatic(w http.ResponseWriter, r *http.Request) {
f := flow.Param(r.Context(), "file")
f = filepath.Clean(filepath.Join(d.c.Dirs.Static, f))
http.ServeFile(w, r, f)
}
2022-12-17 16:03:04 +00:00
func getDescription(path string) (desc string) {
db, err := os.ReadFile(filepath.Join(path, "description"))
if err == nil {
desc = string(db)
} else {
desc = ""
}
return
}