From 1e0f8e0aaeaf1bd2ee39c02e922238b641bcf88b Mon Sep 17 00:00:00 2001 From: tjp Date: Mon, 13 Nov 2023 07:25:39 -0700 Subject: refactor contribs to work with a Protocol interface --- gopher/gophermap/extended.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'gopher/gophermap') diff --git a/gopher/gophermap/extended.go b/gopher/gophermap/extended.go index 8e48e99..7d64fe0 100644 --- a/gopher/gophermap/extended.go +++ b/gopher/gophermap/extended.go @@ -3,6 +3,7 @@ package gophermap import ( "bufio" "bytes" + "context" "errors" "fmt" "io" @@ -14,6 +15,7 @@ import ( "strconv" "strings" + sr "tildegit.org/tjp/sliderule" "tildegit.org/tjp/sliderule/gopher" "tildegit.org/tjp/sliderule/internal" "tildegit.org/tjp/sliderule/internal/types" @@ -298,3 +300,33 @@ func openExtended(path string, location *url.URL, settings FileSystemSettings) ( return ParseExtended(file, location) } + +func ExtendMiddleware(fsroot, urlroot string, settings *FileSystemSettings) sr.Middleware { + return sr.Middleware(func(handler sr.Handler) sr.Handler { + return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { + response := handler.Handle(ctx, request) + + if !settings.ParseExtended || response.Status != gopher.MenuType { + return response + } + + defer func() { _ = response.Close() }() + + edoc, err := ParseExtended(response.Body, request.URL) + if err != nil { + return gopher.Error(err).Response() + } + + fpath := strings.TrimPrefix(request.Path, urlroot) + fpath = strings.Trim(fpath, "/") + fpath = filepath.Join(fsroot, fpath) + + doc, _, err := edoc.Compatible(filepath.Dir(fpath), *settings) + if err != nil { + return gopher.Error(err).Response() + } + + return doc.Response() + }) + }) +} -- cgit v1.2.3