diff options
Diffstat (limited to 'gemini.go')
-rw-r--r-- | gemini.go | 71 |
1 files changed, 39 insertions, 32 deletions
@@ -19,19 +19,24 @@ const ( reponamekey = "syw_reponame" ) -func GeminiRouter(repodir string) *sliderule.Router { +func GeminiRouter(repodir string, overrides *template.Template) *sliderule.Router { + tmpl, err := addTemplates(geminiTemplate, overrides) + if err != nil { + panic(err) + } + repoRouter := &sliderule.Router{} repoRouter.Use(assignRepo(repodir)) - repoRouter.Route("/", gmiTemplate(geminiTemplate, "repo_home.gmi")) - repoRouter.Route("/branches", gmiTemplate(geminiTemplate, "branch_list.gmi")) - repoRouter.Route("/tags", gmiTemplate(geminiTemplate, "tag_list.gmi")) - repoRouter.Route("/refs/:ref/", gmiTemplate(geminiTemplate, "ref.gmi")) - repoRouter.Route("/refs/:ref/tree/*path", sliderule.HandlerFunc(geminiTreePath)) - repoRouter.Route("/diffstat/:fromref/:toref", runTemplate(geminiTemplate, "diffstat.gmi", "text/plain")) - repoRouter.Route("/diff/:fromref/:toref", runTemplate(geminiTemplate, "diff.gmi", "text/x-diff")) + repoRouter.Route("/", gmiTemplate(tmpl, "repo_home.gmi")) + repoRouter.Route("/branches", gmiTemplate(tmpl, "branch_list.gmi")) + repoRouter.Route("/tags", gmiTemplate(tmpl, "tag_list.gmi")) + repoRouter.Route("/refs/:ref/", gmiTemplate(tmpl, "ref.gmi")) + repoRouter.Route("/refs/:ref/tree/*path", geminiTreePath(tmpl)) + repoRouter.Route("/diffstat/:fromref/:toref", runTemplate(tmpl, "diffstat.gmi", "text/plain")) + repoRouter.Route("/diff/:fromref/:toref", runTemplate(tmpl, "diff.gmi", "text/x-diff")) router := &sliderule.Router{} - router.Route("/", geminiRoot(repodir)) + router.Route("/", geminiRoot(repodir, tmpl)) router.Mount("/:"+reponamekey, repoRouter) return router @@ -46,7 +51,7 @@ func assignRepo(repodir string) sliderule.Middleware { } } -func geminiRoot(repodir string) sliderule.Handler { +func geminiRoot(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 { @@ -61,7 +66,7 @@ func geminiRoot(repodir string) sliderule.Handler { } buf := &bytes.Buffer{} - if err := geminiTemplate.ExecuteTemplate(buf, "repo_root.gmi", names); err != nil { + if err := tmpl.ExecuteTemplate(buf, "repo_root.gmi", names); err != nil { return gemini.Failure(err) } @@ -69,31 +74,33 @@ func geminiRoot(repodir string) sliderule.Handler { }) } -func geminiTreePath(ctx context.Context, request *sliderule.Request) *sliderule.Response { - params := sliderule.RouteParams(ctx) - if params["path"] == "" || strings.HasSuffix(params["path"], "/") { - return gmiTemplate(geminiTemplate, "tree.gmi").Handle(ctx, request) - } +func geminiTreePath(tmpl *template.Template) sliderule.Handler { + return sliderule.HandlerFunc(func(ctx context.Context, request *sliderule.Request) *sliderule.Response { + params := sliderule.RouteParams(ctx) + if params["path"] == "" || strings.HasSuffix(params["path"], "/") { + return gmiTemplate(tmpl, "tree.gmi").Handle(ctx, request) + } - repo := ctx.Value(repokey).(*Repository) + repo := ctx.Value(repokey).(*Repository) - body, err := repo.Blob(ctx, params["ref"], params["path"]) - if err != nil { - return gemini.Failure(err) - } + body, err := repo.Blob(ctx, params["ref"], params["path"]) + if err != nil { + return gemini.Failure(err) + } - mediaType := "" - ext := path.Ext(params["path"]) - if ext == ".gmi" { - mediaType = "text/gemini; charset=utf-8" - } else { - mediaType = mime.TypeByExtension(ext) - } - if mediaType == "" { - mediaType = "application/octet-stream" - } + mediaType := "" + ext := path.Ext(params["path"]) + if ext == ".gmi" { + mediaType = "text/gemini; charset=utf-8" + } else { + mediaType = mime.TypeByExtension(ext) + } + if mediaType == "" { + mediaType = "application/octet-stream" + } - return gemini.Success(mediaType, bytes.NewBuffer(body)) + return gemini.Success(mediaType, bytes.NewBuffer(body)) + }) } func gmiTemplate(tmpl *template.Template, name string) sliderule.Handler { |