This commit is contained in:
Liang Ding 2015-10-20 18:31:20 +08:00
parent fdba27c224
commit abe5672490
3 changed files with 43 additions and 19 deletions

View File

@ -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.

View File

@ -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();
}

View File

@ -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
}