From 315edbcbb66baa51feea019c8c8d011a2f4795ea Mon Sep 17 00:00:00 2001 From: aaron <462826@qq.com> Date: Wed, 22 Jul 2020 08:13:30 +0800 Subject: [PATCH] decode request from http interface --- src/gen/decode.go | 7 +++++-- src/service/request.go | 4 ++-- src/zd.go | 20 ++++++++++---------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/gen/decode.go b/src/gen/decode.go index 08bd9a6e..34081eb8 100644 --- a/src/gen/decode.go +++ b/src/gen/decode.go @@ -3,6 +3,7 @@ package gen import ( "encoding/json" "github.com/easysoft/zendata/src/model" + constant "github.com/easysoft/zendata/src/utils/const" fileUtils "github.com/easysoft/zendata/src/utils/file" i118Utils "github.com/easysoft/zendata/src/utils/i118" logUtils "github.com/easysoft/zendata/src/utils/log" @@ -29,10 +30,12 @@ func Decode(defaultFile, configFile, fieldsToExportStr, input, output string) { ret := []map[string]interface{}{} LinesToMap(data, fieldsToExport, &ret) jsonObj, _ := json.Marshal(ret) - respJson := string(jsonObj) + vari.JsonResp = string(jsonObj) - fileUtils.WriteFile(output, respJson) logUtils.Screen(i118Utils.I118Prt.Sprintf("analyse_success", output )) + if vari.RunMode != constant.RunModeServerRequest { + fileUtils.WriteFile(output, vari.JsonResp) + } } func LinesToMap(str string, fieldsToExport []string, ret *[]map[string]interface{}) { diff --git a/src/service/request.go b/src/service/request.go index 09cd9898..f23b9b89 100644 --- a/src/service/request.go +++ b/src/service/request.go @@ -9,8 +9,8 @@ import ( "strconv" ) -func ParseRequestParams(req *http.Request) (root, defaultFile, yamlFile string, count int, - fields, human string, format, table string) { +func ParseRequestParams(req *http.Request) (root, defaultFile, yamlFile, fields string, count int, + human string, format, table string, decode bool, input, output string) { query := req.URL.Query() root = GetRequestParams(query,"root", "R") diff --git a/src/zd.go b/src/zd.go index 438eecd6..edf8adf0 100644 --- a/src/zd.go +++ b/src/zd.go @@ -194,17 +194,17 @@ func StartServer() { } func DataHandler(w http.ResponseWriter, req *http.Request) { - root, defaultFile, configFile, count, fields, vari.HeadSep, - format, table = service.ParseRequestParams(req) - - if defaultFile == "" && configFile == "" { - return + root, defaultFile, configFile, fields, count, vari.HeadSep, + format, table, decode, input, output = service.ParseRequestParams(req) + + if decode { + gen.Decode(defaultFile, configFile, fields, input, output) + fmt.Fprintln(w, vari.JsonResp) + } else if defaultFile != "" || configFile != "" { + vari.RunMode = constant.RunModeServerRequest + toGen() + fmt.Fprintln(w, vari.JsonResp) } - - vari.RunMode = constant.RunModeServerRequest - output = "" - toGen() - fmt.Fprintln(w, vari.JsonResp) } func init() { -- GitLab