-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
51 lines (43 loc) · 904 Bytes
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package ray
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
// RayClient contains the configuration to Ray App
type RayClient struct {
Enabled bool `json:"enable"`
Host string `json:"host"`
Port int `json:"port"`
}
// NewRayClient create a client
func NewRayClient(host string, port int) *RayClient {
return &RayClient{
Enabled: true,
Host: host,
Port: port,
}
}
// Send to ray app
func (r *RayClient) Send(msg Message) {
if !r.Enabled {
return
}
jsonBytes, err := json.Marshal(msg)
if err != nil {
panic(err)
}
url := fmt.Sprintf("http://%s:%d", r.Host, r.Port)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBytes))
if err != nil {
panic(err)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
_, err = client.Do(req)
if err != nil {
panic(err)
}
}