-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
138 lines (122 loc) · 3.2 KB
/
api.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package eureka
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
// POST /eureka/v2/apps/appID
func register(instance *Instance, serverURL string, appName string) error {
b, err := json.Marshal(&InstanceInfo{Instance: instance})
if err != nil {
return err
}
urlAction := serverURL + "/eureka/apps" + "/" + appName
req, err := http.NewRequest(http.MethodPost, urlAction, bytes.NewReader(b))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
return fmt.Errorf("status code is %d, not in range [200, 300)", resp.StatusCode)
}
return nil
}
// DELETE /eureka/v2/apps/appID/instanceID
func unRegister(serverURL string, appName string, instanceID string) error {
urlAction := serverURL + "/eureka/apps" + "/" + appName + "/" + instanceID
req, err := http.NewRequest(http.MethodDelete, urlAction, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("status code is %d, require 200", resp.StatusCode)
}
return nil
}
// nolint
func getAllInstance(serverURL string) (*Applications, error) {
urlAction := serverURL + "/eureka/apps"
req, err := http.NewRequest(http.MethodGet, urlAction, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accpet", "application/xml")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status code is %d, require 200", resp.StatusCode)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
res := &Applications{}
err = xml.Unmarshal(b, res)
if err != nil {
return nil, err
}
return res, nil
}
func GetOneInstance(serverURL string, appName string) (*Application, error) {
urlAction := serverURL + "/eureka/apps" + "/" + appName
req, err := http.NewRequest(http.MethodGet, urlAction, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accpet", "application/xml")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status code is %d, require 200", resp.StatusCode)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
res := &Application{}
err = xml.Unmarshal(b, res)
if err != nil {
return nil, err
}
return res, nil
}
// PUT /eureka/apps/appID/instanceID
func heartbeat(serverURL string, appName string, instanceID string) error {
params := url.Values{
"status": {"UP"},
}
urlAction := serverURL + "/eureka/apps" + "/" + appName + "/" + instanceID + "?" + params.Encode()
req, err := http.NewRequest(http.MethodPut, urlAction, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("status code is %d, require 200", resp.StatusCode)
}
return nil
}