routes: file content view

This commit is contained in:
Anirudh Oppiliappan 2022-12-11 14:18:39 +05:30
parent ab30497e16
commit ac6ca71f01
No known key found for this signature in database
GPG Key ID: 8A93F96F78C5D4C4
4 changed files with 70 additions and 18 deletions

View File

@ -9,6 +9,7 @@ func Handlers(c *config.Config) *flow.Mux {
mux := flow.New() mux := flow.New()
d := deps{c} d := deps{c}
mux.HandleFunc("/:name", d.RepoIndex, "GET") mux.HandleFunc("/:name", d.RepoIndex, "GET")
mux.HandleFunc("/:name/tree/:ref/...", d.RepoFiles, "GET") mux.HandleFunc("/:name/tree/:ref/...", d.RepoTree, "GET")
mux.HandleFunc("/:name/blob/:ref/...", d.FileContent, "GET")
return mux return mux
} }

View File

@ -1,7 +1,6 @@
package routes package routes
import ( import (
"html/template"
"log" "log"
"net/http" "net/http"
"path/filepath" "path/filepath"
@ -42,11 +41,11 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
return return
} }
d.renderFiles(files, w) d.listFiles(files, w)
return return
} }
func (d *deps) RepoFiles(w http.ResponseWriter, r *http.Request) { func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) {
name := flow.Param(r.Context(), "name") name := flow.Param(r.Context(), "name")
treePath := flow.Param(r.Context(), "...") treePath := flow.Param(r.Context(), "...")
ref := flow.Param(r.Context(), "ref") ref := flow.Param(r.Context(), "ref")
@ -74,21 +73,32 @@ func (d *deps) RepoFiles(w http.ResponseWriter, r *http.Request) {
return return
} }
d.renderFiles(files, w) d.listFiles(files, w)
return return
} }
func (d *deps) renderFiles(files []git.NiceTree, w http.ResponseWriter) { func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) {
tpath := filepath.Join(d.c.Template.Dir, "*") name := flow.Param(r.Context(), "name")
t := template.Must(template.ParseGlob(tpath)) treePath := flow.Param(r.Context(), "...")
ref := flow.Param(r.Context(), "ref")
data := make(map[string]interface{}) name = filepath.Clean(name)
data["files"] = files // TODO: remove .git
data["meta"] = d.c.Meta path := filepath.Join(d.c.Git.ScanPath, name+".git")
repo, err := gogit.PlainOpen(path)
if err != nil {
Write404(w, *d.c)
return
}
if err := t.ExecuteTemplate(w, "repo", data); err != nil { hash, err := repo.ResolveRevision(plumbing.Revision(ref))
if err != nil {
Write500(w, *d.c) Write500(w, *d.c)
log.Println(err) log.Println(err)
return return
} }
contents, err := git.FileContentAtRef(repo, *hash, treePath)
d.showFile(contents, w)
return
} }

View File

@ -2,11 +2,12 @@ package routes
import ( import (
"html/template" "html/template"
"log"
"net/http" "net/http"
"os"
"path/filepath" "path/filepath"
"icyphox.sh/legit/config" "icyphox.sh/legit/config"
"icyphox.sh/legit/git"
) )
func Write404(w http.ResponseWriter, c config.Config) { func Write404(w http.ResponseWriter, c config.Config) {
@ -23,10 +24,32 @@ func Write500(w http.ResponseWriter, c config.Config) {
t.Execute(w, nil) t.Execute(w, nil)
} }
func funcMap() template.FuncMap { func (d *deps) listFiles(files []git.NiceTree, w http.ResponseWriter) {
return template.FuncMap{ tpath := filepath.Join(d.c.Template.Dir, "*")
"prettyMode": func(mode uint32) string { t := template.Must(template.ParseGlob(tpath))
return os.FileMode(mode).String()
}, data := make(map[string]interface{})
data["files"] = files
data["meta"] = d.c.Meta
if err := t.ExecuteTemplate(w, "repo", data); err != nil {
Write500(w, *d.c)
log.Println(err)
return
}
}
func (d *deps) showFile(content string, w http.ResponseWriter) {
tpath := filepath.Join(d.c.Template.Dir, "*")
t := template.Must(template.ParseGlob(tpath))
data := make(map[string]interface{})
data["content"] = content
data["meta"] = d.c.Meta
if err := t.ExecuteTemplate(w, "file", data); err != nil {
Write500(w, *d.c)
log.Println(err)
return
} }
} }

18
templates/file.html Normal file
View File

@ -0,0 +1,18 @@
{{ define "file" }}
<html>
{{ template "head" . }}
<header>
<h1>{{ .meta.Title }}</h1>
<h2>{{ .meta.Description }}</h2>
</header>
<body>
{{ template "nav" . }}
<main>
<pre>
{{ .content }}
</pre>
</main>
</body>
</html>
{{ end }}