Commit 92fb2858 authored by Bess严根旺's avatar Bess严根旺

修改回调方式

parent 3982ebcd
......@@ -2,22 +2,27 @@ package console
import (
"bufio"
"bytes"
"context"
"doc-service/domain/entity/serversitemicros/docservercallback"
"doc-service/domain/entity/serversitemicros/docserverproject"
"doc-service/infra/db"
"errors"
"fmt"
"github.com/nacos-group/nacos-sdk-go/v2/common/logger"
"gitlab.galaxy-immi.com/Backend-group/go-com/third/log"
"io"
"net/http"
"os"
"strconv"
"strings"
"time"
)
func recoverPanicCallBack() {
if r := recover(); r != nil {
/*if r := recover(); r != nil {
logger.Error("执行请求回调异常", r)
}
}*/
}
// 执行回调
......@@ -25,7 +30,7 @@ func DoCallback() {
defer recoverPanicCallBack()
fmt.Println("开始请求回调...")
ctxCall := context.Background()
tickerCallback := time.NewTicker(20 * time.Second)
tickerCallback := time.NewTicker(10 * time.Second)
defer tickerCallback.Stop()
go func() {
......@@ -97,6 +102,7 @@ func callBackUrl(c context.Context, projectId int32) (bool, string) {
docservercallback.ServerProjectID(projectId),
).First(c)
fmt.Println(callInfo)
var msg string
var res bool
res = true
......@@ -118,7 +124,90 @@ func callBackUrl(c context.Context, projectId int32) (bool, string) {
if res {
//回调
if callInfo.CallbackType == 0 {
body := []byte(callInfo.CallbackData)
headers := map[string]string{
"Content-Type": "application/json",
}
code, respBody, errs := sendPostRequest(callInfo.CallbackURL, body, headers)
if code != 200 || errs != nil {
msg = fmt.Sprintf("post回调 : code = %d , 原因:%s", code, errs.Error())
res = false
}
logger.Info("Response Body:", string(respBody))
} else {
code, respBody, errs := sendGetRequest(callInfo.CallbackURL)
if code != 200 || errs != nil {
msg = fmt.Sprintf("get 回调 : code = %d , 原因:%s", code, errs.Error())
res = false
}
logger.Info("Response Body:", string(respBody))
}
}
return res, msg
}
//回调业务方
func sendGetRequest(url string) (int, []byte, error) {
resp, err := http.Get(url)
if err != nil {
return 0, nil, err
}
defer func() {
_ = resp.Body.Close()
}()
if resp == nil {
return 2, []byte("响应为空"), errors.New("响应请求为空")
}
if resp.StatusCode != 200 {
return 4, []byte("请求失败状态码 : "), errors.New(strconv.Itoa(resp.StatusCode))
}
respBody, errs := io.ReadAll(resp.Body)
if errs != nil {
return 5, []byte("读取body失败"), errs
}
return resp.StatusCode, respBody, nil
}
func sendPostRequest(url string, body []byte, headers map[string]string) (int, []byte, error) {
req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
if err != nil {
return 1, []byte("创建post出错"), err
}
// 设置请求头
for key, value := range headers {
req.Header.Set(key, value)
}
client := &http.Client{}
resp, errDO := client.Do(req)
defer func() {
_ = resp.Body.Close()
}()
if resp == nil {
return 2, []byte("响应为空"), errors.New("响应请求为空")
}
if errDO != nil {
return 3, []byte("http请求出错"), errDO
}
if resp.StatusCode != 200 {
return 4, []byte("请求失败状态码 : "), errors.New(strconv.Itoa(resp.StatusCode))
}
respBody, errRes := io.ReadAll(resp.Body)
if errRes != nil {
return 5, respBody, errors.New(strconv.Itoa(resp.StatusCode))
}
return 200, respBody, nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment