diff options
author | tjp <tjp@ctrl-c.club> | 2023-11-13 07:25:39 -0700 |
---|---|---|
committer | tjp <tjp@ctrl-c.club> | 2023-11-13 07:27:16 -0700 |
commit | 1e0f8e0aaeaf1bd2ee39c02e922238b641bcf88b (patch) | |
tree | 020e5de91f2343119fed10dede9d2c8262a3cd83 /gopher/gophermap | |
parent | a808b4692656c10bb43e2d54a2f5ef2746d231d5 (diff) |
refactor contribs to work with a Protocol interface
Diffstat (limited to 'gopher/gophermap')
-rw-r--r-- | gopher/gophermap/extended.go | 32 |
1 files changed, 32 insertions, 0 deletions
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() + }) + }) +} |