Skip to content

Commit

Permalink
api some gzip
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Nov 24, 2023
1 parent 3a09174 commit 44904c7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/api/records.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (h *RouteRecords) Pattern() string {

func (h *RouteRecords) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
records := h.crawler.GetStatistics().GetRecords()
h.OutJson(w, 0, "", records)
h.OutJsonGzip(w, 0, "", records)
}

func (h *RouteRecords) FromCrawler(crawler pkg.Crawler) pkg.Route {
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (h *RouteRequests) Pattern() string {

func (h *RouteRequests) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
requests := h.crawler.GetStatistics().GetRequests()
h.OutJson(w, 0, "", requests)
h.OutJsonGzip(w, 0, "", requests)
}

func (h *RouteRequests) FromCrawler(crawler pkg.Crawler) pkg.Route {
Expand Down
16 changes: 14 additions & 2 deletions pkg/api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,20 @@ func (r *Response) OutJson(w http.ResponseWriter, code int, msg string, data any
return
}

//w.Header().Set("Content-Type", "application/json")
//_, _ = w.Write(jsonData)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(jsonData)
}

func (r *Response) OutJsonGzip(w http.ResponseWriter, code int, msg string, data any) {
jsonData, err := json.Marshal(Response{
Code: code,
Msg: msg,
Data: data,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Encoding", "gzip")

Expand Down

0 comments on commit 44904c7

Please sign in to comment.