From 634b82d24335564139b43617d641d6880219e321 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Mon, 30 Oct 2023 13:41:59 -0600 Subject: support spartan in auto-atom conversion middleware --- gemini/gemtext/atomconv/convert.go | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/gemini/gemtext/atomconv/convert.go b/gemini/gemtext/atomconv/convert.go index 30228f3..43a1939 100644 --- a/gemini/gemtext/atomconv/convert.go +++ b/gemini/gemtext/atomconv/convert.go @@ -12,9 +12,11 @@ import ( "strings" "time" + "tildegit.org/tjp/sliderule" "tildegit.org/tjp/sliderule/gemini" "tildegit.org/tjp/sliderule/gemini/gemtext" "tildegit.org/tjp/sliderule/internal/types" + "tildegit.org/tjp/sliderule/spartan" ) // Convert turns a gemini document to Atom format. @@ -43,7 +45,7 @@ func Convert(wr io.Writer, doc gemtext.Document, location *url.URL) error { // gemini://gemini.circumlunar.space/docs/companion/subscription.gmi var Auto = types.Middleware(func(h types.Handler) types.Handler { return types.HandlerFunc(func(ctx context.Context, request *types.Request) *types.Response { - if request.Scheme != "gemini" || !strings.HasSuffix(request.Path, ".atom") { + if !(request.Scheme == "gemini" || request.Scheme == "spartan") || !strings.HasSuffix(request.Path, ".atom") { return h.Handle(ctx, request) } @@ -53,7 +55,24 @@ var Auto = types.Middleware(func(h types.Handler) types.Handler { r.URL = &u response := h.Handle(ctx, &r) - if response.Status != gemini.StatusSuccess { + + var ( + success sliderule.Status + buildSuccess func(string, io.Reader) *sliderule.Response + buildFailure func(error) *sliderule.Response + ) + switch request.Scheme { + case "gemini": + success = gemini.StatusSuccess + buildSuccess = gemini.Success + buildFailure = gemini.Failure + case "spartan": + success = spartan.StatusSuccess + buildSuccess = spartan.Success + buildFailure = spartan.ServerError + } + + if response.Status != success { return response } @@ -68,14 +87,14 @@ var Auto = types.Middleware(func(h types.Handler) types.Handler { doc, err := gemtext.Parse(response.Body) if err != nil { - return gemini.Failure(err) + return buildFailure(err) } buf := &bytes.Buffer{} if err := Convert(buf, doc, request.URL); err != nil { - return gemini.Failure(err) + return buildFailure(err) } - return gemini.Success("application/atom+xml; charset=utf-8", buf) + return buildSuccess("application/atom+xml; charset=utf-8", buf) }) }) -- cgit v1.2.3