Browse Source

封装json输出格式

master
xyiege 5 months ago
parent
commit
7a421dd638
  1. 26
      aufs/core/AuFile.go

26
aufs/core/AuFile.go

@ -1,11 +1,18 @@
package core
import (
"encoding/json"
"fmt"
"net/http"
"net/rpc/jsonrpc"
)
type AuResp struct {
Status int `json:"status"`
Message string `json:"message"`
Data map[string]interface{} `json:"data"`
}
// 文件操作
func Flist(w http.ResponseWriter, r *http.Request) {
// 获取需要监听的服务器
@ -21,6 +28,8 @@ func Flist(w http.ResponseWriter, r *http.Request) {
var reply string
var args = Args{"/www/wwwroot/fsc.com", "dir"}
err = client.Call("FileRpcService.GetFilePath", args, &reply)
// 执行完成后退出
defer client.Close()
if err != nil {
fmt.Fprintf(w, "jsonrpc call faild %v", err)
@ -29,5 +38,20 @@ func Flist(w http.ResponseWriter, r *http.Request) {
// 输出内容
w.Header().Set("Content-Type", "application/json")
// json.NewEncoder(w).Encode(dtm)
w.Write([]byte(reply))
// w.Write([]byte(reply))
var m map[string]interface{}
err = json.Unmarshal([]byte(reply), &m)
if err != nil {
fmt.Println("json.Unmarshal err:", err)
return
}
// 封装json
resp := AuResp{
Status: 200,
Message: "success",
Data: m,
}
json.NewEncoder(w).Encode(resp)
}

Loading…
Cancel
Save