From f7614b73a597c1df3a2a445c6659c6153a9a868d Mon Sep 17 00:00:00 2001 From: aaron <462826@qq.com> Date: Tue, 4 Aug 2020 14:32:55 +0800 Subject: [PATCH] test post request --- res/en/usage.txt | 2 +- res/zh/usage.txt | 2 +- src/gen/definition.go | 25 +++++++++++++++++-------- src/service/request.go | 1 + src/utils/http/http.go | 13 ++----------- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/res/en/usage.txt b/res/en/usage.txt index d4382418..3fdbd1ab 100644 --- a/res/en/usage.txt +++ b/res/en/usage.txt @@ -41,7 +41,7 @@ $>zd.exe -d demo/default.yaml -c demo/test.yaml -n 100 # Using the parameter of $>zd.exe -d demo/default.yaml -c demo/test.yaml -n 100 -o test.txt # Output data in original format. $>zd.exe -d demo/default.yaml -c demo/test.yaml -n 100 -o test.json # Output data in JSON. $>zd.exe -d demo/default.yaml -c demo/test.yaml -n 100 -o test.xml # Output data in XML. -$>zd.exe -d demo/default.yaml -n 100 -o test.sql -t user -s mysql # Output the sql inserted into the table user. +$>zd.exe -d demo/default.yaml -n 100 -o test.sql -t user # Output the sql inserted into the table user. $>zd.exe -d demo/default.yaml -o test.sql -t user -s mysql --trim # Remove the prefix and postfix of every field. $>zd.exe -i db.sql -o db # Generate YAML files for each table by parsing db.sql and store them in the db directory. diff --git a/res/zh/usage.txt b/res/zh/usage.txt index 03f59a77..f0572602 100644 --- a/res/zh/usage.txt +++ b/res/zh/usage.txt @@ -35,7 +35,7 @@ $>zd.exe -d demo\default.yaml -c demo\test.yaml -n 100 -c和-d两个文件的 $>zd.exe -d demo\default.yaml -c demo\test.yaml -n 100 -o test.txt 输出原始格式的数据。 $>zd.exe -d demo\default.yaml -c demo\test.yaml -n 100 -o test.json 输出json格式的数据。 $>zd.exe -d demo\default.yaml -c demo\test.yaml -n 100 -o test.xml 输出xml格式的数据。 -$>zd.exe -d demo\default.yaml -n 100 -o test.sql -t user -s mysql 输出插入到user表里面的sql。 +$>zd.exe -d demo\default.yaml -n 100 -o test.sql -t user 输出插入到user表里面的sql。 $>zd.exe -i db.sql -o db 根据db.sql的定义生成每个表的yaml文件,存储到db目录里面。 $>zd.exe -c demo\default.yaml -i test.txt --decode 将-i指定的文件根据-d参数的配置进行解析。 diff --git a/src/gen/definition.go b/src/gen/definition.go index 2049ad9f..8356038f 100644 --- a/src/gen/definition.go +++ b/src/gen/definition.go @@ -3,6 +3,7 @@ package gen import ( "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" "github.com/easysoft/zendata/src/utils/vari" @@ -17,32 +18,40 @@ func LoadConfigDef(defaultFile, configFile string, fieldsToExport *[]string) mod configDef := model.DefData{} // load defaultDef - path := vari.ExeDir + defaultFile if defaultFile != "" { - defaultContent, err := ioutil.ReadFile(path) + pathDefaultFile := defaultFile + if !fileUtils.IsAbosutePath(pathDefaultFile) { + pathDefaultFile = vari.ExeDir + pathDefaultFile + } + + defaultContent, err := ioutil.ReadFile(pathDefaultFile) defaultContent = ReplaceSpecialChars(defaultContent) if err != nil { - logUtils.PrintToWithColor(i118Utils.I118Prt.Sprintf("fail_to_read_file", path), color.FgCyan) + logUtils.PrintToWithColor(i118Utils.I118Prt.Sprintf("fail_to_read_file", pathDefaultFile), color.FgCyan) return defaultDef } err = yaml.Unmarshal(defaultContent, &defaultDef) if err != nil { - logUtils.PrintToWithColor(i118Utils.I118Prt.Sprintf("fail_to_read_file", path), color.FgCyan) + logUtils.PrintToWithColor(i118Utils.I118Prt.Sprintf("fail_to_read_file", pathDefaultFile), color.FgCyan) return defaultDef } } // load configDef - path = vari.ExeDir + configFile - yamlContent, err := ioutil.ReadFile(path) + pathConfigFile := configFile + if !fileUtils.IsAbosutePath(pathConfigFile) { + pathConfigFile = vari.ExeDir + pathConfigFile + } + + yamlContent, err := ioutil.ReadFile(pathConfigFile) yamlContent = ReplaceSpecialChars(yamlContent) if err != nil { - logUtils.PrintToWithColor(i118Utils.I118Prt.Sprintf("fail_to_read_file", path), color.FgCyan) + logUtils.PrintToWithColor(i118Utils.I118Prt.Sprintf("fail_to_read_file", pathConfigFile), color.FgCyan) return configDef } err = yaml.Unmarshal(yamlContent, &configDef) if err != nil { - logUtils.PrintToWithColor(i118Utils.I118Prt.Sprintf("fail_to_parse_file", path), color.FgCyan) + logUtils.PrintToWithColor(i118Utils.I118Prt.Sprintf("fail_to_parse_file", pathConfigFile), color.FgCyan) return configDef } diff --git a/src/service/request.go b/src/service/request.go index fd0d5c71..c7d4485e 100644 --- a/src/service/request.go +++ b/src/service/request.go @@ -28,6 +28,7 @@ func ParseRequestParams(req *http.Request) (defaultFile, yamlFile, fields string human = GetRequestParams(query,"human", "H") if req.Method == http.MethodPost { + // save to files req.ParseForm() defaultDefContent := req.FormValue("default") configDefContent := req.FormValue("config") diff --git a/src/utils/http/http.go b/src/utils/http/http.go index da2ba5a6..295887cb 100644 --- a/src/utils/http/http.go +++ b/src/utils/http/http.go @@ -7,7 +7,6 @@ import ( "log" "net/http" "net/url" - "strings" ) func PostForm(urlStr string, data url.Values) (interface{}, bool) { @@ -33,15 +32,7 @@ func PostForm(urlStr string, data url.Values) (interface{}, bool) { return body, true } -func GenUrl(server string, path string) string { - server = UpdateUrl(server) - url := fmt.Sprintf("%s%s", server, path) - return url -} - -func UpdateUrl(url string) string { - if strings.LastIndex(url, "/") < len(url)-1 { - url += "/" - } +func GenUrl(server string, port int, path string) string { + url := fmt.Sprintf("http://%s:%d/%s", server, port, path) return url } \ No newline at end of file -- GitLab