You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.1 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package jsruntime
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. "strings"
  12. "time"
  13. )
  14. func (jr *JsRuntime) EnableRequestFunc() {
  15. jr.runtime.Set("GoRequest", func(call map[string]interface{}) (respData string) {
  16. url := ""
  17. if call["url"] != nil {
  18. url = call["url"].(string)
  19. }
  20. data := ""
  21. if call["data"] != nil {
  22. data = call["data"].(string)
  23. }
  24. header := map[string]interface{}{}
  25. if call["header"] != nil {
  26. header = call["header"].(map[string]interface{})
  27. }
  28. timeout := 0
  29. if call["timeout"] != nil {
  30. switch call["timeout"].(type) {
  31. case int64:
  32. timeout = int(call["timeout"].(int64))
  33. case int:
  34. timeout = call["timeout"].(int)
  35. }
  36. }
  37. method := "GET"
  38. if call["method"] != nil {
  39. method = call["method"].(string)
  40. }
  41. client := &http.Client{
  42. Timeout: time.Millisecond * time.Duration(timeout),
  43. }
  44. var contentReader *bytes.Reader
  45. contentReader = bytes.NewReader([]byte(data))
  46. //log.Println(method, url, data)
  47. req, err := http.NewRequest(method, url, contentReader)
  48. if err != nil {
  49. rs, _ := json.Marshal(map[string]interface{}{
  50. "statusCode": 504,
  51. "data": err.Error(),
  52. })
  53. return string(rs)
  54. }
  55. for e := range header {
  56. req.Header.Set(e, header[e].(string))
  57. }
  58. resp, err := client.Do(req)
  59. var statusCode int
  60. var respDatas string
  61. if err == nil {
  62. defer resp.Body.Close()
  63. statusCode = resp.StatusCode
  64. rb, _ := ioutil.ReadAll(resp.Body)
  65. respDatas = string(rb)
  66. } else {
  67. statusCode = 502
  68. respDatas = err.Error()
  69. }
  70. log2File("netword", "-------------------------------------------------")
  71. log2File("netword", "网络请求:", method, url)
  72. headerStr, _ := json.Marshal(header)
  73. log2File("netword", "网络请求头:", string(headerStr))
  74. log2File("netword", "网络请求发送数据:", data)
  75. log2File("netword", "请求返回状态:", err == nil)
  76. log2File("netword", "请求返回状态码:", statusCode)
  77. log2File("netword", "请求返回:", respDatas)
  78. log2File("netword", "-------------------------------------------------")
  79. rs, _ := json.Marshal(map[string]interface{}{
  80. "statusCode": statusCode,
  81. "data": string(respDatas),
  82. })
  83. return string(rs)
  84. })
  85. }
  86. func log2File(tag string, strs ...interface{}) {
  87. str := fmt.Sprintln(strs...)
  88. path := filepath.Join(getSelfFilePath(), fmt.Sprintf("jsruntime-%s.log", tag))
  89. if pathExists(path) {
  90. raw, _ := ioutil.ReadFile(path)
  91. str = string(raw) + str
  92. }
  93. ioutil.WriteFile(path, []byte(str), 0644)
  94. }
  95. func pathExists(path string) bool {
  96. _, err := os.Stat(path)
  97. if err == nil {
  98. return true
  99. }
  100. if os.IsNotExist(err) {
  101. return false
  102. }
  103. return false
  104. }
  105. var selfFilePath string
  106. func getSelfFilePath() string {
  107. if selfFilePath == "" {
  108. file, err := exec.LookPath(os.Args[0])
  109. if err != nil {
  110. return ""
  111. }
  112. path, err := filepath.Abs(file)
  113. if err != nil {
  114. return ""
  115. }
  116. i := strings.LastIndex(path, "/")
  117. if i < 0 {
  118. i = strings.LastIndex(path, "\\")
  119. }
  120. if i < 0 {
  121. return ""
  122. }
  123. selfFilePath, _ = filepath.Abs(string(path[0 : i+1]))
  124. }
  125. return selfFilePath
  126. }