From a70e22f9965ea56c648973327180a3266755040d Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Sat, 11 Nov 2023 19:13:50 +0100 Subject: [PATCH] Small optimization --- geoMap.go | 18 ++---------------- indieAuthServer.go | 24 +++--------------------- micropub.go | 8 +------- nodeinfo.go | 18 ++---------------- reactions.go | 10 +--------- utils.go | 11 +++++++++++ 6 files changed, 20 insertions(+), 69 deletions(-) diff --git a/geoMap.go b/geoMap.go index e4b0545..d160503 100644 --- a/geoMap.go +++ b/geoMap.go @@ -1,11 +1,7 @@ package main import ( - "encoding/json" - "io" "net/http" - - "go.goblog.app/app/pkgs/contenttype" ) const defaultGeoMapPath = "/map" @@ -88,12 +84,7 @@ func (a *goBlog) serveGeoMapTracks(w http.ResponseWriter, r *http.Request) { } } - pr, pw := io.Pipe() - go func() { - _ = pw.CloseWithError(json.NewEncoder(pw).Encode(tracks)) - }() - w.Header().Set(contentType, contenttype.JSONUTF8) - _ = pr.CloseWithError(a.min.Get().Minify(contenttype.JSON, w, pr)) + a.respondWithMinifiedJson(w, tracks) } const geoMapLocationsSubpath = "/locations.json" @@ -131,10 +122,5 @@ func (a *goBlog) serveGeoMapLocations(w http.ResponseWriter, r *http.Request) { } } - pr, pw := io.Pipe() - go func() { - _ = pw.CloseWithError(json.NewEncoder(pw).Encode(locations)) - }() - w.Header().Set(contentType, contenttype.JSONUTF8) - _ = pr.CloseWithError(a.min.Get().Minify(contenttype.JSON, w, pr)) + a.respondWithMinifiedJson(w, locations) } diff --git a/indieAuthServer.go b/indieAuthServer.go index 05fcab3..2d2f88e 100644 --- a/indieAuthServer.go +++ b/indieAuthServer.go @@ -2,16 +2,13 @@ package main import ( "database/sql" - "encoding/json" "errors" - "io" "net/http" "net/url" "strings" "time" "github.com/google/uuid" - "go.goblog.app/app/pkgs/contenttype" "go.hacdias.com/indielib/indieauth" ) @@ -44,12 +41,7 @@ func (a *goBlog) indieAuthMetadata(w http.ResponseWriter, _ *http.Request) { "scopes_supported": []string{"create", "update", "delete", "undelete", "media"}, "code_challenge_methods_supported": indieauth.CodeChallengeMethods, } - pr, pw := io.Pipe() - go func() { - _ = pw.CloseWithError(json.NewEncoder(pw).Encode(resp)) - }() - w.Header().Set(contentType, contenttype.JSONUTF8) - _ = pr.CloseWithError(a.min.Get().Minify(contenttype.JSON, w, pr)) + a.respondWithMinifiedJson(w, resp) } // Parse Authorization Request @@ -166,12 +158,7 @@ func (a *goBlog) indieAuthVerification(w http.ResponseWriter, r *http.Request, w resp["access_token"] = token resp["scope"] = strings.Join(data.Scopes, " ") } - pr, pw := io.Pipe() - go func() { - _ = pw.CloseWithError(json.NewEncoder(pw).Encode(resp)) - }() - w.Header().Set(contentType, contenttype.JSONUTF8) - _ = pr.CloseWithError(a.min.Get().Minify(contenttype.JSON, w, pr)) + a.respondWithMinifiedJson(w, resp) } // Save the authorization request and return the code @@ -232,12 +219,7 @@ func (a *goBlog) indieAuthTokenVerification(w http.ResponseWriter, r *http.Reque "scope": strings.Join(data.Scopes, " "), } } - pr, pw := io.Pipe() - go func() { - _ = pw.CloseWithError(json.NewEncoder(pw).Encode(res)) - }() - w.Header().Set(contentType, contenttype.JSONUTF8) - _ = pr.CloseWithError(a.min.Get().Minify(contenttype.JSON, w, pr)) + a.respondWithMinifiedJson(w, res) } // Checks the database for the token and returns the indieAuthData with client and scope. diff --git a/micropub.go b/micropub.go index cee62e7..f00eb34 100644 --- a/micropub.go +++ b/micropub.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "io" "mime" "net/http" "net/url" @@ -75,12 +74,7 @@ func (a *goBlog) serveMicropubQuery(w http.ResponseWriter, r *http.Request) { a.serve404(w, r) return } - pr, pw := io.Pipe() - go func() { - _ = pw.CloseWithError(json.NewEncoder(pw).Encode(result)) - }() - w.Header().Set(contentType, contenttype.JSONUTF8) - _ = pr.CloseWithError(a.min.Get().Minify(contenttype.JSON, w, pr)) + a.respondWithMinifiedJson(w, result) } func (a *goBlog) getMicropubChannelsMap() []map[string]any { diff --git a/nodeinfo.go b/nodeinfo.go index 4006c67..a59d97a 100644 --- a/nodeinfo.go +++ b/nodeinfo.go @@ -1,11 +1,7 @@ package main import ( - "encoding/json" - "io" "net/http" - - "go.goblog.app/app/pkgs/contenttype" ) func (a *goBlog) serveNodeInfoDiscover(w http.ResponseWriter, _ *http.Request) { @@ -17,12 +13,7 @@ func (a *goBlog) serveNodeInfoDiscover(w http.ResponseWriter, _ *http.Request) { }, }, } - pr, pw := io.Pipe() - go func() { - _ = pw.CloseWithError(json.NewEncoder(pw).Encode(result)) - }() - w.Header().Set(contentType, contenttype.JSONUTF8) - _ = pr.CloseWithError(a.min.Get().Minify(contenttype.JSON, w, pr)) + a.respondWithMinifiedJson(w, result) } func (a *goBlog) serveNodeInfo(w http.ResponseWriter, _ *http.Request) { @@ -49,10 +40,5 @@ func (a *goBlog) serveNodeInfo(w http.ResponseWriter, _ *http.Request) { }, "metadata": map[string]any{}, } - pr, pw := io.Pipe() - go func() { - _ = pw.CloseWithError(json.NewEncoder(pw).Encode(result)) - }() - w.Header().Set(contentType, contenttype.JSONUTF8) - _ = pr.CloseWithError(a.min.Get().Minify(contenttype.JSON, w, pr)) + a.respondWithMinifiedJson(w, result) } diff --git a/reactions.go b/reactions.go index ff048b9..3841a5b 100644 --- a/reactions.go +++ b/reactions.go @@ -1,15 +1,12 @@ package main import ( - "encoding/json" "errors" - "io" "net/http" "github.com/dgraph-io/ristretto" "github.com/samber/lo" "go.goblog.app/app/pkgs/builderpool" - "go.goblog.app/app/pkgs/contenttype" ) // Hardcoded for now @@ -88,13 +85,8 @@ func (a *goBlog) getReactions(w http.ResponseWriter, r *http.Request) { a.serveError(w, r, "", http.StatusInternalServerError) return } - pr, pw := io.Pipe() - go func() { - _ = pw.CloseWithError(json.NewEncoder(pw).Encode(reactions)) - }() - w.Header().Set(contentType, contenttype.JSONUTF8) w.Header().Set(cacheControl, "no-store") - _ = pr.CloseWithError(a.min.Get().Minify(contenttype.JSON, w, pr)) + a.respondWithMinifiedJson(w, reactions) } func (a *goBlog) getReactionsFromDatabase(path string) (map[string]int, error) { diff --git a/utils.go b/utils.go index 61f8583..815c054 100644 --- a/utils.go +++ b/utils.go @@ -2,6 +2,7 @@ package main import ( "context" + "encoding/json" "errors" "fmt" "io" @@ -25,6 +26,7 @@ import ( "github.com/microcosm-cc/bluemonday" "github.com/samber/lo" "go.goblog.app/app/pkgs/builderpool" + "go.goblog.app/app/pkgs/contenttype" "golang.org/x/net/html" "golang.org/x/text/language" ) @@ -439,3 +441,12 @@ func truncateStringWithEllipsis(s string, l int) string { } return s } + +func (a *goBlog) respondWithMinifiedJson(w http.ResponseWriter, v any) { + pr, pw := io.Pipe() + go func() { + _ = pw.CloseWithError(json.NewEncoder(pw).Encode(v)) + }() + w.Header().Set(contentType, contenttype.JSONUTF8) + _ = pr.CloseWithError(a.min.Get().Minify(contenttype.JSON, w, pr)) +}