routes: render readme
This commit is contained in:
parent
7d99afc5cd
commit
82b8afe19b
5 changed files with 53 additions and 19 deletions
|
@ -1,5 +1,10 @@
|
||||||
git:
|
git:
|
||||||
scanPath: /home/icy/code/tmp
|
scanPath: /home/icy/code/tmp
|
||||||
|
readme:
|
||||||
|
- readme
|
||||||
|
- README
|
||||||
|
- readme.md
|
||||||
|
- README.md
|
||||||
template:
|
template:
|
||||||
dir: ./templates
|
dir: ./templates
|
||||||
meta:
|
meta:
|
||||||
|
|
|
@ -9,7 +9,8 @@ import (
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Git struct {
|
Git struct {
|
||||||
ScanPath string `yaml:"scanPath"`
|
ScanPath string `yaml:"scanPath"`
|
||||||
|
Readme []string `yaml:"readme"`
|
||||||
} `yaml:"git"`
|
} `yaml:"git"`
|
||||||
Template struct {
|
Template struct {
|
||||||
Dir string `yaml:"dir"`
|
Dir string `yaml:"dir"`
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/alexedwards/flow"
|
"github.com/alexedwards/flow"
|
||||||
"icyphox.sh/legit/config"
|
"icyphox.sh/legit/config"
|
||||||
)
|
)
|
||||||
|
@ -8,6 +10,12 @@ import (
|
||||||
func Handlers(c *config.Config) *flow.Mux {
|
func Handlers(c *config.Config) *flow.Mux {
|
||||||
mux := flow.New()
|
mux := flow.New()
|
||||||
d := deps{c}
|
d := deps{c}
|
||||||
|
|
||||||
|
mux.NotFound = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
d.Write404(w)
|
||||||
|
})
|
||||||
|
|
||||||
|
mux.HandleFunc("/", d.Index, "GET")
|
||||||
mux.HandleFunc("/:name", d.RepoIndex, "GET")
|
mux.HandleFunc("/:name", d.RepoIndex, "GET")
|
||||||
mux.HandleFunc("/:name/tree/:ref/...", d.RepoTree, "GET")
|
mux.HandleFunc("/:name/tree/:ref/...", d.RepoTree, "GET")
|
||||||
mux.HandleFunc("/:name/blob/:ref/...", d.FileContent, "GET")
|
mux.HandleFunc("/:name/blob/:ref/...", d.FileContent, "GET")
|
||||||
|
|
|
@ -15,6 +15,10 @@ type deps struct {
|
||||||
c *config.Config
|
c *config.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
|
func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
|
||||||
name := flow.Param(r.Context(), "name")
|
name := flow.Param(r.Context(), "name")
|
||||||
name = filepath.Clean(name)
|
name = filepath.Clean(name)
|
||||||
|
@ -22,21 +26,34 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
|
||||||
path := filepath.Join(d.c.Git.ScanPath, name+".git")
|
path := filepath.Join(d.c.Git.ScanPath, name+".git")
|
||||||
gr, err := git.Open(path, "")
|
gr, err := git.Open(path, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Write404(w, *d.c)
|
d.Write404(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
files, err := gr.FileTree("")
|
files, err := gr.FileTree("")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Write500(w, *d.c)
|
d.Write500(w)
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var readmeContent string
|
||||||
|
for _, readme := range d.c.Git.Readme {
|
||||||
|
readmeContent, _ = gr.FileContent(readme)
|
||||||
|
if readmeContent != "" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if readmeContent == "" {
|
||||||
|
log.Printf("no readme found for %s", name)
|
||||||
|
}
|
||||||
|
|
||||||
data := make(map[string]any)
|
data := make(map[string]any)
|
||||||
data["name"] = name
|
data["name"] = name
|
||||||
// TODO: make this configurable
|
// TODO: make this configurable
|
||||||
data["ref"] = "master"
|
data["ref"] = "master"
|
||||||
|
data["readme"] = readmeContent
|
||||||
|
|
||||||
d.listFiles(files, data, w)
|
d.listFiles(files, data, w)
|
||||||
return
|
return
|
||||||
|
@ -52,13 +69,13 @@ func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) {
|
||||||
path := filepath.Join(d.c.Git.ScanPath, name+".git")
|
path := filepath.Join(d.c.Git.ScanPath, name+".git")
|
||||||
gr, err := git.Open(path, ref)
|
gr, err := git.Open(path, ref)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Write404(w, *d.c)
|
d.Write404(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
files, err := gr.FileTree(treePath)
|
files, err := gr.FileTree(treePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Write500(w, *d.c)
|
d.Write500(w)
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -82,7 +99,7 @@ func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) {
|
||||||
path := filepath.Join(d.c.Git.ScanPath, name+".git")
|
path := filepath.Join(d.c.Git.ScanPath, name+".git")
|
||||||
gr, err := git.Open(path, ref)
|
gr, err := git.Open(path, ref)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Write404(w, *d.c)
|
d.Write404(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,13 +119,13 @@ func (d *deps) Log(w http.ResponseWriter, r *http.Request) {
|
||||||
path := filepath.Join(d.c.Git.ScanPath, name+".git")
|
path := filepath.Join(d.c.Git.ScanPath, name+".git")
|
||||||
gr, err := git.Open(path, ref)
|
gr, err := git.Open(path, ref)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Write404(w, *d.c)
|
d.Write404(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
commits, err := gr.Commits()
|
commits, err := gr.Commits()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Write500(w, *d.c)
|
d.Write500(w)
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -135,13 +152,13 @@ func (d *deps) Diff(w http.ResponseWriter, r *http.Request) {
|
||||||
path := filepath.Join(d.c.Git.ScanPath, name+".git")
|
path := filepath.Join(d.c.Git.ScanPath, name+".git")
|
||||||
gr, err := git.Open(path, ref)
|
gr, err := git.Open(path, ref)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Write404(w, *d.c)
|
d.Write404(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
diff, err := gr.Diff()
|
diff, err := gr.Diff()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Write500(w, *d.c)
|
d.Write500(w)
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,22 +6,25 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"icyphox.sh/legit/config"
|
|
||||||
"icyphox.sh/legit/git"
|
"icyphox.sh/legit/git"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Write404(w http.ResponseWriter, c config.Config) {
|
func (d *deps) Write404(w http.ResponseWriter) {
|
||||||
|
tpath := filepath.Join(d.c.Template.Dir, "*")
|
||||||
|
t := template.Must(template.ParseGlob(tpath))
|
||||||
w.WriteHeader(404)
|
w.WriteHeader(404)
|
||||||
tpath := filepath.Join(c.Template.Dir, "404.html")
|
if err := t.ExecuteTemplate(w, "404", nil); err != nil {
|
||||||
t := template.Must(template.ParseFiles(tpath))
|
log.Printf("404 template: %s", err)
|
||||||
t.Execute(w, nil)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Write500(w http.ResponseWriter, c config.Config) {
|
func (d *deps) Write500(w http.ResponseWriter) {
|
||||||
|
tpath := filepath.Join(d.c.Template.Dir, "*")
|
||||||
|
t := template.Must(template.ParseGlob(tpath))
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
tpath := filepath.Join(c.Template.Dir, "500.html")
|
if err := t.ExecuteTemplate(w, "500", nil); err != nil {
|
||||||
t := template.Must(template.ParseFiles(tpath))
|
log.Printf("500 template: %s", err)
|
||||||
t.Execute(w, nil)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *deps) listFiles(files []git.NiceTree, data map[string]any, w http.ResponseWriter) {
|
func (d *deps) listFiles(files []git.NiceTree, data map[string]any, w http.ResponseWriter) {
|
||||||
|
|
Loading…
Reference in a new issue