Skip to content

Commit

Permalink
Small optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
jlelse committed Nov 11, 2023
1 parent 0ce2d37 commit a70e22f
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 69 deletions.
18 changes: 2 additions & 16 deletions geoMap.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package main

import (
"encoding/json"
"io"
"net/http"

"go.goblog.app/app/pkgs/contenttype"
)

const defaultGeoMapPath = "/map"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
24 changes: 3 additions & 21 deletions indieAuthServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 1 addition & 7 deletions micropub.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"mime"
"net/http"
"net/url"
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 2 additions & 16 deletions nodeinfo.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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) {
Expand All @@ -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)
}
10 changes: 1 addition & 9 deletions reactions.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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))
}

0 comments on commit a70e22f

Please sign in to comment.