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 package main
import ( import (
"encoding/json"
"io"
"net/http" "net/http"
"go.goblog.app/app/pkgs/contenttype"
) )
const defaultGeoMapPath = "/map" const defaultGeoMapPath = "/map"
@ -88,12 +84,7 @@ func (a *goBlog) serveGeoMapTracks(w http.ResponseWriter, r *http.Request) {
} }
} }
pr, pw := io.Pipe() a.respondWithMinifiedJson(w, tracks)
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))
} }
const geoMapLocationsSubpath = "/locations.json" const geoMapLocationsSubpath = "/locations.json"
@ -131,10 +122,5 @@ func (a *goBlog) serveGeoMapLocations(w http.ResponseWriter, r *http.Request) {
} }
} }
pr, pw := io.Pipe() a.respondWithMinifiedJson(w, locations)
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))
} }

View File

@ -2,16 +2,13 @@ package main
import ( import (
"database/sql" "database/sql"
"encoding/json"
"errors" "errors"
"io"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"go.goblog.app/app/pkgs/contenttype"
"go.hacdias.com/indielib/indieauth" "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"}, "scopes_supported": []string{"create", "update", "delete", "undelete", "media"},
"code_challenge_methods_supported": indieauth.CodeChallengeMethods, "code_challenge_methods_supported": indieauth.CodeChallengeMethods,
} }
pr, pw := io.Pipe() a.respondWithMinifiedJson(w, resp)
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))
} }
// Parse Authorization Request // Parse Authorization Request
@ -166,12 +158,7 @@ func (a *goBlog) indieAuthVerification(w http.ResponseWriter, r *http.Request, w
resp["access_token"] = token resp["access_token"] = token
resp["scope"] = strings.Join(data.Scopes, " ") resp["scope"] = strings.Join(data.Scopes, " ")
} }
pr, pw := io.Pipe() a.respondWithMinifiedJson(w, resp)
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))
} }
// Save the authorization request and return the code // 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, " "), "scope": strings.Join(data.Scopes, " "),
} }
} }
pr, pw := io.Pipe() a.respondWithMinifiedJson(w, res)
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))
} }
// Checks the database for the token and returns the indieAuthData with client and scope. // Checks the database for the token and returns the indieAuthData with client and scope.

View File

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io"
"mime" "mime"
"net/http" "net/http"
"net/url" "net/url"
@ -75,12 +74,7 @@ func (a *goBlog) serveMicropubQuery(w http.ResponseWriter, r *http.Request) {
a.serve404(w, r) a.serve404(w, r)
return return
} }
pr, pw := io.Pipe() a.respondWithMinifiedJson(w, result)
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))
} }
func (a *goBlog) getMicropubChannelsMap() []map[string]any { func (a *goBlog) getMicropubChannelsMap() []map[string]any {

View File

@ -1,11 +1,7 @@
package main package main
import ( import (
"encoding/json"
"io"
"net/http" "net/http"
"go.goblog.app/app/pkgs/contenttype"
) )
func (a *goBlog) serveNodeInfoDiscover(w http.ResponseWriter, _ *http.Request) { 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() a.respondWithMinifiedJson(w, result)
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))
} }
func (a *goBlog) serveNodeInfo(w http.ResponseWriter, _ *http.Request) { 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{}, "metadata": map[string]any{},
} }
pr, pw := io.Pipe() a.respondWithMinifiedJson(w, result)
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))
} }

View File

@ -1,15 +1,12 @@
package main package main
import ( import (
"encoding/json"
"errors" "errors"
"io"
"net/http" "net/http"
"github.com/dgraph-io/ristretto" "github.com/dgraph-io/ristretto"
"github.com/samber/lo" "github.com/samber/lo"
"go.goblog.app/app/pkgs/builderpool" "go.goblog.app/app/pkgs/builderpool"
"go.goblog.app/app/pkgs/contenttype"
) )
// Hardcoded for now // Hardcoded for now
@ -88,13 +85,8 @@ func (a *goBlog) getReactions(w http.ResponseWriter, r *http.Request) {
a.serveError(w, r, "", http.StatusInternalServerError) a.serveError(w, r, "", http.StatusInternalServerError)
return 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") 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) { func (a *goBlog) getReactionsFromDatabase(path string) (map[string]int, error) {

View File

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -25,6 +26,7 @@ import (
"github.com/microcosm-cc/bluemonday" "github.com/microcosm-cc/bluemonday"
"github.com/samber/lo" "github.com/samber/lo"
"go.goblog.app/app/pkgs/builderpool" "go.goblog.app/app/pkgs/builderpool"
"go.goblog.app/app/pkgs/contenttype"
"golang.org/x/net/html" "golang.org/x/net/html"
"golang.org/x/text/language" "golang.org/x/text/language"
) )
@ -439,3 +441,12 @@ func truncateStringWithEllipsis(s string, l int) string {
} }
return s 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))
}