Sfoglia il codice sorgente

Compress JSON, CSS and Javascript responses

Frédéric Guillot 7 anni fa
parent
commit
9f6533ece9

+ 1 - 1
api/category.go

@@ -83,7 +83,7 @@ func (c *Controller) GetCategories(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	json.OK(w, categories)
+	json.OK(w, r, categories)
 }
 
 // RemoveCategory is the API handler to remove a category.

+ 4 - 4
api/entry.go

@@ -48,7 +48,7 @@ func (c *Controller) GetFeedEntry(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	json.OK(w, entry)
+	json.OK(w, r, entry)
 }
 
 // GetEntry is the API handler to get a single entry.
@@ -73,7 +73,7 @@ func (c *Controller) GetEntry(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	json.OK(w, entry)
+	json.OK(w, r, entry)
 }
 
 // GetFeedEntries is the API handler to get all feed entries.
@@ -132,7 +132,7 @@ func (c *Controller) GetFeedEntries(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	json.OK(w, &entriesResponse{Total: count, Entries: entries})
+	json.OK(w, r, &entriesResponse{Total: count, Entries: entries})
 }
 
 // GetEntries is the API handler to fetch entries.
@@ -184,7 +184,7 @@ func (c *Controller) GetEntries(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	json.OK(w, &entriesResponse{Total: count, Entries: entries})
+	json.OK(w, r, &entriesResponse{Total: count, Entries: entries})
 }
 
 // SetEntryStatus is the API handler to change the status of entries.

+ 2 - 2
api/feed.go

@@ -146,7 +146,7 @@ func (c *Controller) GetFeeds(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	json.OK(w, feeds)
+	json.OK(w, r, feeds)
 }
 
 // GetFeed is the API handler to get a feed.
@@ -168,7 +168,7 @@ func (c *Controller) GetFeed(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	json.OK(w, feed)
+	json.OK(w, r, feed)
 }
 
 // RemoveFeed is the API handler to remove a feed.

+ 1 - 1
api/icon.go

@@ -37,7 +37,7 @@ func (c *Controller) FeedIcon(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	json.OK(w, &feedIcon{
+	json.OK(w, r, &feedIcon{
 		ID:       icon.ID,
 		MimeType: icon.MimeType,
 		Data:     icon.DataURL(),

+ 1 - 1
api/subscription.go

@@ -36,5 +36,5 @@ func (c *Controller) GetSubscriptions(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	json.OK(w, subscriptions)
+	json.OK(w, r, subscriptions)
 }

+ 4 - 4
api/user.go

@@ -22,7 +22,7 @@ func (c *Controller) CurrentUser(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	json.OK(w, user)
+	json.OK(w, r, user)
 }
 
 // CreateUser is the API handler to create a new user.
@@ -119,7 +119,7 @@ func (c *Controller) Users(w http.ResponseWriter, r *http.Request) {
 	}
 
 	users.UseTimezone(ctx.UserTimezone())
-	json.OK(w, users)
+	json.OK(w, r, users)
 }
 
 // UserByID is the API handler to fetch the given user by the ID.
@@ -148,7 +148,7 @@ func (c *Controller) UserByID(w http.ResponseWriter, r *http.Request) {
 	}
 
 	user.UseTimezone(ctx.UserTimezone())
-	json.OK(w, user)
+	json.OK(w, r, user)
 }
 
 // UserByUsername is the API handler to fetch the given user by the username.
@@ -171,7 +171,7 @@ func (c *Controller) UserByUsername(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	json.OK(w, user)
+	json.OK(w, r, user)
 }
 
 // RemoveUser is the API handler to remove an existing user.

+ 10 - 10
fever/fever.go

@@ -155,7 +155,7 @@ func (c *Controller) Handler(w http.ResponseWriter, r *http.Request) {
 	case r.FormValue("mark") == "group":
 		c.handleWriteGroups(w, r)
 	default:
-		json.OK(w, newBaseResponse())
+		json.OK(w, r, newBaseResponse())
 	}
 }
 
@@ -203,7 +203,7 @@ func (c *Controller) handleGroups(w http.ResponseWriter, r *http.Request) {
 
 	result.FeedsGroups = c.buildFeedGroups(feeds)
 	result.SetCommonValues()
-	json.OK(w, result)
+	json.OK(w, r, result)
 }
 
 /*
@@ -262,7 +262,7 @@ func (c *Controller) handleFeeds(w http.ResponseWriter, r *http.Request) {
 
 	result.FeedsGroups = c.buildFeedGroups(feeds)
 	result.SetCommonValues()
-	json.OK(w, result)
+	json.OK(w, r, result)
 }
 
 /*
@@ -304,7 +304,7 @@ func (c *Controller) handleFavicons(w http.ResponseWriter, r *http.Request) {
 	}
 
 	result.SetCommonValues()
-	json.OK(w, result)
+	json.OK(w, r, result)
 }
 
 /*
@@ -414,7 +414,7 @@ func (c *Controller) handleItems(w http.ResponseWriter, r *http.Request) {
 	}
 
 	result.SetCommonValues()
-	json.OK(w, result)
+	json.OK(w, r, result)
 }
 
 /*
@@ -445,7 +445,7 @@ func (c *Controller) handleUnreadItems(w http.ResponseWriter, r *http.Request) {
 	var result unreadResponse
 	result.ItemIDs = strings.Join(itemIDs, ",")
 	result.SetCommonValues()
-	json.OK(w, result)
+	json.OK(w, r, result)
 }
 
 /*
@@ -477,7 +477,7 @@ func (c *Controller) handleSavedItems(w http.ResponseWriter, r *http.Request) {
 
 	result := &savedResponse{ItemIDs: strings.Join(itemsIDs, ",")}
 	result.SetCommonValues()
-	json.OK(w, result)
+	json.OK(w, r, result)
 }
 
 /*
@@ -534,7 +534,7 @@ func (c *Controller) handleWriteItems(w http.ResponseWriter, r *http.Request) {
 		}()
 	}
 
-	json.OK(w, newBaseResponse())
+	json.OK(w, r, newBaseResponse())
 }
 
 /*
@@ -575,7 +575,7 @@ func (c *Controller) handleWriteFeeds(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	json.OK(w, newBaseResponse())
+	json.OK(w, r, newBaseResponse())
 }
 
 /*
@@ -616,7 +616,7 @@ func (c *Controller) handleWriteGroups(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	json.OK(w, newBaseResponse())
+	json.OK(w, r, newBaseResponse())
 }
 
 /*

+ 3 - 3
http/response/json/json.go

@@ -9,14 +9,14 @@ import (
 	"errors"
 	"net/http"
 
+	"github.com/miniflux/miniflux/http/response"
 	"github.com/miniflux/miniflux/logger"
 )
 
 // OK sends a JSON response with the status code 200.
-func OK(w http.ResponseWriter, v interface{}) {
+func OK(w http.ResponseWriter, r *http.Request, v interface{}) {
 	commonHeaders(w)
-	w.WriteHeader(http.StatusOK)
-	w.Write(toJSON(v))
+	response.Compress(w, r, toJSON(v))
 }
 
 // Created sends a JSON response with the status code 201.

+ 9 - 3
http/response/response.go

@@ -23,7 +23,7 @@ func NotModified(w http.ResponseWriter) {
 }
 
 // Cache returns a response with caching headers.
-func Cache(w http.ResponseWriter, r *http.Request, mimeType, etag string, content []byte, duration time.Duration) {
+func Cache(w http.ResponseWriter, r *http.Request, mimeType, etag string, data []byte, duration time.Duration) {
 	w.Header().Set("Content-Type", mimeType)
 	w.Header().Set("ETag", etag)
 	w.Header().Set("Cache-Control", "public")
@@ -31,8 +31,14 @@ func Cache(w http.ResponseWriter, r *http.Request, mimeType, etag string, conten
 
 	if etag == r.Header.Get("If-None-Match") {
 		w.WriteHeader(http.StatusNotModified)
-	} else {
-		w.Write(content)
+		return
+	}
+
+	switch mimeType {
+	case "text/javascript; charset=utf-8", "text/css; charset=utf-8":
+		Compress(w, r, data)
+	default:
+		w.Write(data)
 	}
 }
 

+ 2 - 2
middleware/fever.go

@@ -20,13 +20,13 @@ func (m *Middleware) FeverAuth(next http.Handler) http.Handler {
 		user, err := m.store.UserByFeverToken(apiKey)
 		if err != nil {
 			logger.Error("[Middleware:Fever] %v", err)
-			json.OK(w, map[string]int{"api_version": 3, "auth": 0})
+			json.OK(w, r, map[string]int{"api_version": 3, "auth": 0})
 			return
 		}
 
 		if user == nil {
 			logger.Info("[Middleware:Fever] Fever authentication failure")
-			json.OK(w, map[string]int{"api_version": 3, "auth": 0})
+			json.OK(w, r, map[string]int{"api_version": 3, "auth": 0})
 			return
 		}
 

+ 1 - 1
ui/entry_scraper.go

@@ -49,5 +49,5 @@ func (c *Controller) FetchContent(w http.ResponseWriter, r *http.Request) {
 	entry.Content = sanitizer.Sanitize(entry.URL, content)
 	c.store.UpdateEntryContent(entry)
 
-	json.Created(w, map[string]string{"content": entry.Content})
+	json.OK(w, r, map[string]string{"content": entry.Content})
 }

+ 1 - 1
ui/entry_toggle_bookmark.go

@@ -28,5 +28,5 @@ func (c *Controller) ToggleBookmark(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	json.OK(w, "OK")
+	json.OK(w, r, "OK")
 }

+ 1 - 1
ui/entry_update_status.go

@@ -35,5 +35,5 @@ func (c *Controller) UpdateEntriesStatus(w http.ResponseWriter, r *http.Request)
 		return
 	}
 
-	json.OK(w, "OK")
+	json.OK(w, r, "OK")
 }

+ 1 - 1
ui/static_manifest.go

@@ -50,5 +50,5 @@ func (c *Controller) WebManifest(w http.ResponseWriter, r *http.Request) {
 		},
 	}
 
-	json.OK(w, manifest)
+	json.OK(w, r, manifest)
 }