From 4a5dad998cf97b879e88c8dc1ce025432dfc90cb Mon Sep 17 00:00:00 2001 From: tjpcc Date: Sat, 26 Aug 2023 09:22:25 -0600 Subject: AutoAtom: middleware that supports adding .atom to any gemtext path --- gemini/gemtext/sub.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'gemini/gemtext/sub.go') diff --git a/gemini/gemtext/sub.go b/gemini/gemtext/sub.go index 365d41b..5030291 100644 --- a/gemini/gemtext/sub.go +++ b/gemini/gemtext/sub.go @@ -2,13 +2,18 @@ package gemtext import ( "bytes" + "context" "html/template" "io" + "mime" "net/url" "regexp" "strconv" "strings" "time" + + "tildegit.org/tjp/sliderule/gemini" + "tildegit.org/tjp/sliderule/internal/types" ) // GmisubToAtom converts a gemini document to Atom format. @@ -28,6 +33,50 @@ func GmisubToAtom(doc Document, location url.URL, out io.Writer) error { return nil } +// AutoAtom is a middleware which builds atom feeds for any gemtext pages. +// +// It looks for requests ending with the '.atom' extension, passes through the request +// with the extension clipped off, then if the response is in gemtext it converts it to +// an Atom feed according to the gmisub spec at +// gemini://gemini.circumlunar.space/docs/companion/subscription.gmi +var AutoAtom = types.Middleware(func(h types.Handler) types.Handler { + return types.HandlerFunc(func(ctx context.Context, request *types.Request) *types.Response { + if !strings.HasSuffix(request.Path, ".atom") { + return h.Handle(ctx, request) + } + + req := *request + u := *request.URL + u.Path = strings.TrimSuffix(u.Path, ".atom") + req.URL = &u + + response := h.Handle(ctx, &req) + if response.Status != gemini.StatusSuccess { + return response + } + + mtype, _, err := mime.ParseMediaType(response.Meta.(string)) + if err != nil || mtype != "text/gemini" { + return response + } + + defer func() { + _ = response.Close() + }() + + doc, err := Parse(response.Body) + if err != nil { + return gemini.Failure(err) + } + + buf := &bytes.Buffer{} + if err := GmisubToAtom(doc, *request.URL, buf); err != nil { + return gemini.Failure(err) + } + return gemini.Success("application/atom+xml; charset=utf-8", buf) + }) +}) + type gmiSub struct { ID template.URL Title string -- cgit v1.2.3