Skip to content

Commit

Permalink
v0.9.8 + Request & Network Process
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Nov 28, 2021
1 parent 63981f7 commit 622c9a9
Show file tree
Hide file tree
Showing 12 changed files with 353 additions and 29 deletions.
54 changes: 27 additions & 27 deletions data/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions engine/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func processPing(process *gou.Process) interface{} {
"version": share.VERSION,
"domain": share.DOMAIN,
"allows": config.Conf.Service.Allow,
"args": process.Args[0],
}
return res
}
Expand Down
5 changes: 5 additions & 0 deletions helper/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func init() {
gou.RegisterProcessHandler("xiang.helper.IF", ProcessIF)

gou.RegisterProcessHandler("xiang.helper.Print", ProcessPrint)

gou.RegisterProcessHandler("xiang.helper.Get", ProcessGet)
gou.RegisterProcessHandler("xiang.helper.Post", ProcessPost)
gou.RegisterProcessHandler("xiang.helper.Send", ProcessSend)

}

// ProcessPrint xiang.helper.Print 打印语句
Expand Down
116 changes: 116 additions & 0 deletions helper/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package helper

import (
"bytes"
"io/ioutil"
"net/http"
"strings"

jsoniter "github.com/json-iterator/go"
)

// Response 请求响应结果
type Response struct {
Status int `json:"status"`
Body string `json:"body"`
Data interface{} `json:"data"`
Headers map[string]interface{} `json:"headers"`
}

// RequestGet 发送GET请求
func RequestGet(url string, params map[string]interface{}, headers map[string]string) Response {
return RequestSend("GET", url, params, nil, headers)
}

// RequestPost 发送POST请求
func RequestPost(url string, data interface{}, headers map[string]string) Response {
return RequestSend("POST", url, map[string]interface{}{}, data, headers)
}

// RequestSend 发送Request请求
func RequestSend(method string, url string, params map[string]interface{}, data interface{}, headers map[string]string) Response {

var body []byte
var err error
if data != nil {
body, err = jsoniter.Marshal(data)
if err != nil {
return Response{
Status: 500,
Body: err.Error(),
Data: map[string]interface{}{"code": 500, "message": err.Error()},
Headers: map[string]interface{}{
"Content-Type": "application/json;charset=utf8",
},
}
}
}

req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
if err != nil {
return Response{
Status: 500,
Body: err.Error(),
Data: map[string]interface{}{"code": 500, "message": err.Error()},
Headers: map[string]interface{}{
"Content-Type": "application/json;charset=utf8",
},
}
}

// Request Header
for name, header := range headers {
req.Header.Set(name, header)
}

resp, err := (&http.Client{}).Do(req)
if err != nil {
return Response{
Status: 0,
Body: err.Error(),
Data: map[string]interface{}{"code": 500, "message": err.Error()},
Headers: map[string]interface{}{
"Content-Type": "application/json;charset=utf8",
},
}
}
defer resp.Body.Close()

body, err = ioutil.ReadAll(resp.Body) // response body is []byte
if err != nil {
return Response{
Status: 500,
Body: err.Error(),
Data: map[string]interface{}{"code": resp.StatusCode, "message": err.Error()},
Headers: map[string]interface{}{
"Content-Type": "application/json;charset=utf8",
},
}
}

// JSON 解析
var res interface{}
if strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") {
err = jsoniter.Unmarshal(body, &res)
if err != nil {
return Response{
Status: 500,
Body: err.Error(),
Data: map[string]interface{}{"code": resp.StatusCode, "message": err.Error()},
Headers: map[string]interface{}{
"Content-Type": "application/json;charset=utf8",
},
}
}
}
respHeaders := map[string]interface{}{}
for name := range resp.Header {
respHeaders[name] = resp.Header.Get(name)
}
return Response{
Status: resp.StatusCode,
Body: string(body),
Data: res,
Headers: respHeaders,
}
}
Loading

0 comments on commit 622c9a9

Please sign in to comment.