all repos — elgit @ 82b8afe19b77fe02e1a29a5b5e20882d8e5c3fc5

fork of legit: web frontend for git, written in go

routes: render readme
Anirudh Oppiliappan x@icyphox.sh
Mon, 12 Dec 2022 20:53:58 +0530
commit

82b8afe19b77fe02e1a29a5b5e20882d8e5c3fc5

parent

7d99afc5cd3ddfc7a875754f4593812b8c2664c8

5 files changed, 53 insertions(+), 19 deletions(-)

jump to
M config.yamlconfig.yaml
@@ -1,5 +1,10 @@ git:
   scanPath: /home/icy/code/tmp
+  readme:
+    - readme
+    - README
+    - readme.md
+    - README.md
 template:
   dir: ./templates
 meta:
M config/config.goconfig/config.go
@@ -9,7 +9,8 @@ ) 
 type Config struct {
 	Git struct {
-		ScanPath string `yaml:"scanPath"`
+		ScanPath string   `yaml:"scanPath"`
+		Readme   []string `yaml:"readme"`
 	} `yaml:"git"`
 	Template struct {
 		Dir string `yaml:"dir"`
M routes/handler.goroutes/handler.go
@@ -1,6 +1,8 @@ package routes
 
 import (
+	"net/http"
+
 	"github.com/alexedwards/flow"
 	"icyphox.sh/legit/config"
 )
@@ -8,6 +10,12 @@ func Handlers(c *config.Config) *flow.Mux {
 	mux := flow.New()
 	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/tree/:ref/...", d.RepoTree, "GET")
 	mux.HandleFunc("/:name/blob/:ref/...", d.FileContent, "GET")
M routes/routes.goroutes/routes.go
@@ -15,6 +15,10 @@ type deps struct { 	c *config.Config
 }
 
+func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
+
+}
+
 func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
 	name := flow.Param(r.Context(), "name")
 	name = filepath.Clean(name)
@@ -22,21 +26,34 @@ // TODO: remove .git 	path := filepath.Join(d.c.Git.ScanPath, name+".git")
 	gr, err := git.Open(path, "")
 	if err != nil {
-		Write404(w, *d.c)
+		d.Write404(w)
 		return
 	}
 
 	files, err := gr.FileTree("")
 	if err != nil {
-		Write500(w, *d.c)
+		d.Write500(w)
 		log.Println(err)
 		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["name"] = name
 	// TODO: make this configurable
 	data["ref"] = "master"
+	data["readme"] = readmeContent
 
 	d.listFiles(files, data, w)
 	return
@@ -52,13 +69,13 @@ // TODO: remove .git 	path := filepath.Join(d.c.Git.ScanPath, name+".git")
 	gr, err := git.Open(path, ref)
 	if err != nil {
-		Write404(w, *d.c)
+		d.Write404(w)
 		return
 	}
 
 	files, err := gr.FileTree(treePath)
 	if err != nil {
-		Write500(w, *d.c)
+		d.Write500(w)
 		log.Println(err)
 		return
 	}
@@ -82,7 +99,7 @@ // TODO: remove .git 	path := filepath.Join(d.c.Git.ScanPath, name+".git")
 	gr, err := git.Open(path, ref)
 	if err != nil {
-		Write404(w, *d.c)
+		d.Write404(w)
 		return
 	}
 
@@ -102,13 +119,13 @@ 	path := filepath.Join(d.c.Git.ScanPath, name+".git")
 	gr, err := git.Open(path, ref)
 	if err != nil {
-		Write404(w, *d.c)
+		d.Write404(w)
 		return
 	}
 
 	commits, err := gr.Commits()
 	if err != nil {
-		Write500(w, *d.c)
+		d.Write500(w)
 		log.Println(err)
 		return
 	}
@@ -135,13 +152,13 @@ 	path := filepath.Join(d.c.Git.ScanPath, name+".git")
 	gr, err := git.Open(path, ref)
 	if err != nil {
-		Write404(w, *d.c)
+		d.Write404(w)
 		return
 	}
 
 	diff, err := gr.Diff()
 	if err != nil {
-		Write500(w, *d.c)
+		d.Write500(w)
 		log.Println(err)
 		return
 	}
M routes/template.goroutes/template.go
@@ -6,22 +6,25 @@ "log" 	"net/http"
 	"path/filepath"
 
-	"icyphox.sh/legit/config"
 	"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)
-	tpath := filepath.Join(c.Template.Dir, "404.html")
-	t := template.Must(template.ParseFiles(tpath))
-	t.Execute(w, nil)
+	if err := t.ExecuteTemplate(w, "404", nil); err != nil {
+		log.Printf("404 template: %s", err)
+	}
 }
 
-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)
-	tpath := filepath.Join(c.Template.Dir, "500.html")
-	t := template.Must(template.ParseFiles(tpath))
-	t.Execute(w, nil)
+	if err := t.ExecuteTemplate(w, "500", nil); err != nil {
+		log.Printf("500 template: %s", err)
+	}
 }
 
 func (d *deps) listFiles(files []git.NiceTree, data map[string]any, w http.ResponseWriter) {