Small optimization

This commit is contained in:
Jan-Lukas Else 2023-11-11 19:13:50 +01:00
parent 0ce2d373bd
commit a70e22f996
6 changed files with 20 additions and 69 deletions

View File

@ -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)
}

View File

@ -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.

View File

@ -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 {

View File

@ -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)
}

View File

@ -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) {

View File

@ -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))
}