forked from redboxllc/scuttle
-
Notifications
You must be signed in to change notification settings - Fork 4
/
http.go
49 lines (41 loc) · 957 Bytes
/
http.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
package main
import (
"context"
"encoding/json"
"io"
"net/http"
)
func getServerInfo(ctx context.Context, url string) (ServerInfo, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return ServerInfo{}, err
}
req.Header.Set("Accept", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return ServerInfo{}, err
}
b, err := readBody(res)
if err != nil {
return ServerInfo{}, err
}
si := ServerInfo{}
err = json.Unmarshal(b, &si)
return si, err
}
func postKill(ctx context.Context, url string) (statusCode int, err error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, nil)
if err != nil {
return 0, err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return 0, err
}
_, _ = readBody(res)
return res.StatusCode, nil
}
func readBody(r *http.Response) ([]byte, error) {
defer r.Body.Close()
return io.ReadAll(r.Body)
}