legit/routes/routes.go

166 lines
3.3 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"
"path/filepath"
"github.com/alexedwards/flow"
"icyphox.sh/legit/config"
"icyphox.sh/legit/git"
)
type deps struct {
c *config.Config
}
func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
name := flow.Param(r.Context(), "name")
name = filepath.Clean(name)
// TODO: remove .git
path := filepath.Join(d.c.Git.ScanPath, name+".git")
2022-12-11 15:47:04 +00:00
gr, err := git.Open(path, "")
if err != nil {
Write404(w, *d.c)
return
}
2022-12-11 15:47:04 +00:00
files, err := gr.FileTree("")
if err != nil {
Write500(w, *d.c)
log.Println(err)
return
}
2022-12-11 15:47:04 +00:00
data := make(map[string]any)
data["name"] = name
// TODO: make this configurable
data["ref"] = "master"
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) 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)
// TODO: remove .git
path := filepath.Join(d.c.Git.ScanPath, name+".git")
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 {
Write404(w, *d.c)
return
}
2022-12-11 15:47:04 +00:00
files, err := gr.FileTree(treePath)
if err != nil {
Write500(w, *d.c)
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
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)
// TODO: remove .git
path := filepath.Join(d.c.Git.ScanPath, name+".git")
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 {
Write404(w, *d.c)
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
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")
path := filepath.Join(d.c.Git.ScanPath, name+".git")
gr, err := git.Open(path, ref)
if err != nil {
Write404(w, *d.c)
return
}
commits, err := gr.Commits()
2022-12-11 08:48:39 +00:00
if err != nil {
2022-12-11 05:52:47 +00:00
Write500(w, *d.c)
log.Println(err)
return
}
2022-12-11 08:48:39 +00:00
2022-12-11 15:47:04 +00:00
tpath := filepath.Join(d.c.Template.Dir, "*")
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
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")
path := filepath.Join(d.c.Git.ScanPath, name+".git")
gr, err := git.Open(path, ref)
if err != nil {
Write404(w, *d.c)
return
}
diff, err := gr.Diff()
if err != nil {
Write500(w, *d.c)
log.Println(err)
return
}
tpath := filepath.Join(d.c.Template.Dir, "*")
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
if err := t.ExecuteTemplate(w, "commit", data); err != nil {
log.Println(err)
return
}
}