diff --git a/file/files.go b/file/files.go index 2bb15bc..ce86c74 100644 --- a/file/files.go +++ b/file/files.go @@ -82,8 +82,8 @@ func GetFilesHandler(w http.ResponseWriter, r *http.Request) { } username := httpSession.Values["username"].(string) - data := map[string]interface{}{"succ": true} - defer util.RetGzJSON(w, r, data) + result := util.NewResult() + defer util.RetGzResult(w, r, result) userWorkspace := conf.GetUserWorkspace(username) workspaces := filepath.SplitList(userWorkspace) @@ -118,7 +118,7 @@ func GetFilesHandler(w http.ResponseWriter, r *http.Request) { // add Go API node root.Children = append(root.Children, apiNode) - data["root"] = root + result.Data = root } // RefreshDirectoryHandler handles request of refresh a directory of file tree. diff --git a/static/js/tree.js b/static/js/tree.js index d884afa..42a0815 100644 --- a/static/js/tree.js +++ b/static/js/tree.js @@ -367,7 +367,7 @@ var tree = { } } }; - tree.fileTree = $.fn.zTree.init($("#files"), setting, data.root.children); + tree.fileTree = $.fn.zTree.init($("#files"), setting, data.data.children); session.restore(); } diff --git a/util/ret.go b/util/ret.go index 488dc91..2989894 100644 --- a/util/ret.go +++ b/util/ret.go @@ -51,27 +51,15 @@ func RetResult(w http.ResponseWriter, r *http.Request, res *Result) { data, err := json.Marshal(res) if err != nil { retLogger.Error(err) + return } w.Write(data) } -// RetJSON writes HTTP response with "Content-Type, application/json". -func RetJSON(w http.ResponseWriter, r *http.Request, res map[string]interface{}) { - w.Header().Set("Content-Type", "application/json") - - data, err := json.Marshal(res) - if err != nil { - retLogger.Error(err) - return - } - - w.Write(data) -} - -// RetGzJSON writes HTTP response with "Content-Type, application/json". -func RetGzJSON(w http.ResponseWriter, r *http.Request, res map[string]interface{}) { +// RetGzResult writes HTTP response with "Content-Type, application/json" and "Content-Encoding, gzip". +func RetGzResult(w http.ResponseWriter, r *http.Request, res *Result) { w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Encoding", "gzip") @@ -79,6 +67,42 @@ func RetGzJSON(w http.ResponseWriter, r *http.Request, res map[string]interface{ err := json.NewEncoder(gz).Encode(res) if nil != err { retLogger.Error(err) + + return + } + + err = gz.Close() + if nil != err { + retLogger.Error(err) + + return + } +} + +// RetJSON writes HTTP response with "Content-Type, application/json". +func RetJSON(w http.ResponseWriter, r *http.Request, res map[string]interface{}) { + w.Header().Set("Content-Type", "application/json") + + data, err := json.Marshal(res) + if err != nil { + retLogger.Error(err) + + return + } + + w.Write(data) +} + +// RetGzJSON writes HTTP response with "Content-Type, application/json" and "Content-Encoding, gzip". +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 { + retLogger.Error(err) + return }