summaryrefslogtreecommitdiff
path: root/gopher.go
diff options
context:
space:
mode:
Diffstat (limited to 'gopher.go')
-rw-r--r--gopher.go86
1 files changed, 28 insertions, 58 deletions
diff --git a/gopher.go b/gopher.go
index d715641..7d9bdec 100644
--- a/gopher.go
+++ b/gopher.go
@@ -3,9 +3,7 @@ package syw
import (
"bytes"
"context"
- "os"
"path"
- "path/filepath"
"text/template"
"tildegit.org/tjp/sliderule"
@@ -65,55 +63,47 @@ func GopherRouter(repodir string, overrides *template.Template) *sliderule.Route
repoRouter := &sliderule.Router{}
repoRouter.Use(assignRepo(repodir))
- repoRouter.Route("/branches", runGopherTemplate(tmpl, "branch_list.gph", gopher.MenuType))
- repoRouter.Route("/tags", runGopherTemplate(tmpl, "tag_list.gph", gopher.MenuType))
- repoRouter.Route("/refs/:ref", runGopherTemplate(tmpl, "ref.gph", gopher.MenuType))
- repoRouter.Route("/refs/:ref/tree", gopherTreePath(tmpl, false))
- repoRouter.Route("/refs/:ref/tree/*path", gopherTreePath(tmpl, true))
- repoRouter.Route("/diffstat/:fromref/:toref", runGopherTemplate(tmpl, "diffstat.gph.txt", gopher.TextFileType))
- repoRouter.Route("/diff/:fromref/:toref", runGopherTemplate(tmpl, "diff.gph.txt", gopher.TextFileType))
+ repoRouter.Route("/branches", repoRouteHandler(gopherProto, tmpl, "branch_list.gph"))
+ repoRouter.Route("/tags", repoRouteHandler(gopherProto, tmpl, "tag_list.gph"))
+ repoRouter.Route("/refs/:ref", repoRouteHandler(gopherProto, tmpl, "ref.gph"))
+ repoRouter.Route("/refs/:ref/tree", gopherTreePath(tmpl))
+ repoRouter.Route("/refs/:ref/tree/*path", gopherTreePath(tmpl))
+ repoRouter.Route("/diffstat/:fromref/:toref", repoRouteHandler(gopherProto, tmpl, "diffstat.gph.txt"))
+ repoRouter.Route("/diff/:fromref/:toref", repoRouteHandler(gopherProto, tmpl, "diff.gph.txt"))
router := &sliderule.Router{}
- router.Route("/", gopherRoot(repodir, tmpl))
- router.Route("/:"+reponamekey, assignRepo(repodir)(runGopherTemplate(tmpl, "repo_home.gph", gopher.MenuType)))
+ router.Route("/", rootDirHandler(gopherProto, repodir, tmpl, "repo_root.gph"))
+ router.Route("/:"+reponamekey, assignRepo(repodir)(repoRouteHandler(gopherProto, tmpl, "repo_home.gph")))
router.Mount("/:"+reponamekey, repoRouter)
return router
}
-func gopherRoot(repodir string, tmpl *template.Template) sliderule.Handler {
- return sliderule.HandlerFunc(func(ctx context.Context, request *sliderule.Request) *sliderule.Response {
- entries, err := os.ReadDir(repodir)
- if err != nil {
- return gopher.Error(err).Response()
- }
-
- names := []string{}
- for _, item := range entries {
- if Open(filepath.Join(repodir, item.Name())) != nil {
- names = append(names, item.Name())
- }
- }
+type gopherProtocol struct{ sliderule.ServerProtocol }
- buf := &bytes.Buffer{}
- obj := map[string]any{
- "Repos": names,
- "Host": request.Hostname(),
- "Port": request.Port(),
- "Selector": request.Path,
- }
- if err := tmpl.ExecuteTemplate(buf, "repo_root.gph", obj); err != nil {
- return gopher.Error(err).Response()
- }
+func (gopherProtocol) TemplateBaseData(ctx context.Context, request *sliderule.Request) map[string]any {
+ return map[string]any{
+ "Host": request.Hostname(),
+ "Port": request.Port(),
+ "Selector": request.Path,
+ }
+}
- return gopher.File(gopher.MenuType, buf)
- })
+func (gopherProtocol) TemplateRepoData(ctx context.Context, request *sliderule.Request) map[string]any {
+ return map[string]any{
+ "Ctx": ctx,
+ "Repo": ctx.Value(repokey),
+ "Params": sliderule.RouteParams(ctx),
+ }
}
-func gopherTreePath(tmpl *template.Template, haspath bool) sliderule.Handler {
+var gopherProto = gopherProtocol{gopher.ServerProtocol}
+
+func gopherTreePath(tmpl *template.Template) sliderule.Handler {
return sliderule.HandlerFunc(func(ctx context.Context, request *sliderule.Request) *sliderule.Response {
repo := ctx.Value(repokey).(*Repository)
params := sliderule.RouteParams(ctx)
+ _, haspath := params["path"]
t := "tree"
if haspath {
@@ -128,7 +118,7 @@ func gopherTreePath(tmpl *template.Template, haspath bool) sliderule.Handler {
if !haspath {
params["path"] = ""
}
- return runGopherTemplate(tmpl, "tree.gph", gopher.MenuType).Handle(ctx, request)
+ return repoRouteHandler(gopherProto, tmpl, "tree.gph").Handle(ctx, request)
}
body, err := repo.Blob(ctx, params["ref"], params["path"])
@@ -143,23 +133,3 @@ func gopherTreePath(tmpl *template.Template, haspath bool) sliderule.Handler {
return gopher.File(filetype, bytes.NewBuffer(body))
})
}
-
-func runGopherTemplate(tmpl *template.Template, name string, filetype sliderule.Status) sliderule.Handler {
- return sliderule.HandlerFunc(func(ctx context.Context, request *sliderule.Request) *sliderule.Response {
- obj := map[string]any{
- "Ctx": ctx,
- "Repo": ctx.Value(repokey),
- "Params": sliderule.RouteParams(ctx),
- "Host": request.Hostname(),
- "Port": request.Port(),
- "Selector": request.Path,
- }
- buf := &bytes.Buffer{}
-
- if err := tmpl.ExecuteTemplate(buf, name, obj); err != nil {
- return gopher.Error(err).Response()
- }
-
- return gopher.File(filetype, buf)
- })
-}