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) { // 获取需要监听的服务器 // sc := r.Form.Get("srcip") // 测试JSONRPC service := "192.168.66.92:9098" client, err := jsonrpc.Dial("tcp", service) if err != nil { fmt.Fprintf(w, "jsonrpc dial faild %v", err) return } // 调用远程方法 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) return } // 输出内容 w.Header().Set("Content-Type", "application/json") // json.NewEncoder(w).Encode(dtm) // 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) }