From fac3adc11b22226d6795120cc1f7d92c6194808b Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Tue, 9 Dec 2014 10:36:33 +0800 Subject: [PATCH] optimize file tree loading :zap: --- file/files.go | 2 +- util/ret.go | 27 ++++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/file/files.go b/file/files.go index a847405..17b1416 100644 --- a/file/files.go +++ b/file/files.go @@ -69,7 +69,7 @@ func initAPINode() { // so that users can easily view the Go API source code in file tree. func GetFiles(w http.ResponseWriter, r *http.Request) { data := map[string]interface{}{"succ": true} - defer util.RetJSON(w, r, data) + defer util.RetGzJSON(w, r, data) session, _ := session.HTTPSession.Get(r, "wide-session") if session.IsNew { diff --git a/util/ret.go b/util/ret.go index 3f4d045..4e74ddb 100644 --- a/util/ret.go +++ b/util/ret.go @@ -1,11 +1,11 @@ // Copyright (c) 2014, B3log -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -15,6 +15,7 @@ package util import ( + "compress/gzip" "encoding/json" "net/http" @@ -33,3 +34,23 @@ func RetJSON(w http.ResponseWriter, r *http.Request, res map[string]interface{}) w.Write(data) } + +// RetGzJSON writes HTTP response with "Content-Type, application/json". +func RetGzJSON(w http.ResponseWriter, r *http.Request, res map[string]interface{}) { + w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Encoding", "gzip") + + gz := gzip.NewWriter(w) + err := json.NewEncoder(gz).Encode(res) + if nil != err { + glog.Error(err) + return + } + + err = gz.Close() + if nil != err { + glog.Error(err) + + return + } +}