Skip to content

Commit

Permalink
feat: return early if req fails
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Gleich <[email protected]>
  • Loading branch information
gleich committed Jan 7, 2025
1 parent 96649cd commit ab319a9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
4 changes: 3 additions & 1 deletion internal/apis/applemusic/applemusic.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ type cacheDataResponse struct {

func serveHTTP(c *cache.Cache[cacheData]) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth.IsAuthorized(w, r)
if !auth.IsAuthorized(w, r) {
return
}
w.Header().Set("Content-Type", "application/json")
c.DataMutex.RLock()

Expand Down
4 changes: 3 additions & 1 deletion internal/apis/applemusic/playlists.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ func fetchPlaylist(id string) (playlist, error) {

func playlistEndpoint(c *cache.Cache[cacheData]) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth.IsAuthorized(w, r)
if !auth.IsAuthorized(w, r) {
return
}
id := chi.URLParam(r, "id")

c.DataMutex.RLock()
Expand Down
4 changes: 3 additions & 1 deletion internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ type CacheResponse[T any] struct {

func (c *Cache[T]) ServeHTTP() http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth.IsAuthorized(w, r)
if !auth.IsAuthorized(w, r) {
return
}
w.Header().Set("Content-Type", "application/json")
c.DataMutex.RLock()
err := json.NewEncoder(w).Encode(CacheResponse[T]{Data: c.Data, Updated: c.Updated})
Expand Down

0 comments on commit ab319a9

Please sign in to comment.