80 lines
1.6 KiB
Go
Executable File
80 lines
1.6 KiB
Go
Executable File
package pusher
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"wdd.io/agent-common/logger"
|
|
)
|
|
|
|
var log = logger.Log
|
|
|
|
// CmiiUpdateMessage message_pusher/cmii/CmiiMessage.go
|
|
type CmiiUpdateMessage struct {
|
|
Namespace string
|
|
AppName string
|
|
FromTag string
|
|
ToTag string
|
|
Replicas string
|
|
DeployStatus bool
|
|
}
|
|
|
|
type Message struct { // TODO combine with server.message
|
|
ID string
|
|
Event string
|
|
Time int64
|
|
Topic string
|
|
Message string
|
|
Title string
|
|
Priority int
|
|
Tags []string
|
|
Click string
|
|
Icon string
|
|
|
|
// Additional fields
|
|
TopicURL string
|
|
SubscriptionID string
|
|
Raw string
|
|
}
|
|
|
|
func (c *CmiiUpdateMessage) SendMessage() (message Message) {
|
|
|
|
// 将结构体转换为JSON字符串
|
|
requestBytes, err := json.Marshal(c)
|
|
if err != nil {
|
|
fmt.Println("Error encoding request body to JSON:", err)
|
|
return
|
|
}
|
|
|
|
url := "http://192.168.35.71:8080/cmii/update" // 替换为实际的API地址
|
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBytes))
|
|
if err != nil {
|
|
fmt.Println("Error creating request:", err)
|
|
return
|
|
}
|
|
|
|
// 添加请求头
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
// 发送请求并获取响应
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
fmt.Println("Error sending request:", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
bodyBytes, _ := io.ReadAll(resp.Body)
|
|
|
|
var m Message
|
|
err = json.Unmarshal(bodyBytes, &m)
|
|
if err != nil {
|
|
log.ErrorF("Error unmarshaling response body to JSON: %s", err.Error())
|
|
return message
|
|
}
|
|
|
|
return m
|
|
}
|