diff options
author | Anirudh Oppiliappan <x@icyphox.sh> | 2022-12-11 14:18:39 +0530 |
---|---|---|
committer | Anirudh Oppiliappan <x@icyphox.sh> | 2022-12-11 14:18:39 +0530 |
commit | ac6ca71f01885b3fff692b4b9ee36ed33965d396 (patch) | |
tree | 6270a3a3c849df8b6fcbd95f9930f39a239e4cfb | |
parent | ab30497e169cd3bfcd122e668c3cdde6bd48305b (diff) | |
download | legit-ac6ca71f01885b3fff692b4b9ee36ed33965d396.tar.gz legit-ac6ca71f01885b3fff692b4b9ee36ed33965d396.tar.bz2 legit-ac6ca71f01885b3fff692b4b9ee36ed33965d396.zip |
routes: file content view
-rw-r--r-- | routes/handler.go | 3 | ||||
-rw-r--r-- | routes/routes.go | 32 | ||||
-rw-r--r-- | routes/template.go | 35 | ||||
-rw-r--r-- | templates/file.html | 18 |
4 files changed, 70 insertions, 18 deletions
diff --git a/routes/handler.go b/routes/handler.go index e00d412..523239a 100644 --- a/routes/handler.go +++ b/routes/handler.go @@ -9,6 +9,7 @@ func Handlers(c *config.Config) *flow.Mux { mux := flow.New() d := deps{c} 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 } diff --git a/routes/routes.go b/routes/routes.go index cf2409f..5d5dc2c 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -1,7 +1,6 @@ package routes import ( - "html/template" "log" "net/http" "path/filepath" @@ -42,11 +41,11 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) { return } - d.renderFiles(files, w) + d.listFiles(files, w) 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") treePath := flow.Param(r.Context(), "...") ref := flow.Param(r.Context(), "ref") @@ -74,21 +73,32 @@ func (d *deps) RepoFiles(w http.ResponseWriter, r *http.Request) { return } - d.renderFiles(files, w) + d.listFiles(files, w) return } -func (d *deps) renderFiles(files []git.NiceTree, w http.ResponseWriter) { - tpath := filepath.Join(d.c.Template.Dir, "*") - t := template.Must(template.ParseGlob(tpath)) +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") - data := make(map[string]interface{}) - data["files"] = files - data["meta"] = d.c.Meta + name = filepath.Clean(name) + // TODO: remove .git + 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) log.Println(err) return } + + contents, err := git.FileContentAtRef(repo, *hash, treePath) + d.showFile(contents, w) + return } diff --git a/routes/template.go b/routes/template.go index 212ed21..5595f6e 100644 --- a/routes/template.go +++ b/routes/template.go @@ -2,11 +2,12 @@ package routes import ( "html/template" + "log" "net/http" - "os" "path/filepath" "icyphox.sh/legit/config" + "icyphox.sh/legit/git" ) func Write404(w http.ResponseWriter, c config.Config) { @@ -23,10 +24,32 @@ func Write500(w http.ResponseWriter, c config.Config) { t.Execute(w, nil) } -func funcMap() template.FuncMap { - return template.FuncMap{ - "prettyMode": func(mode uint32) string { - return os.FileMode(mode).String() - }, +func (d *deps) listFiles(files []git.NiceTree, w http.ResponseWriter) { + tpath := filepath.Join(d.c.Template.Dir, "*") + t := template.Must(template.ParseGlob(tpath)) + + 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 } } diff --git a/templates/file.html b/templates/file.html new file mode 100644 index 0000000..2ccf1c6 --- /dev/null +++ b/templates/file.html @@ -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 }} |