File: routes.go

package info (click to toggle)
golang-github-rluisr-mysqlrouter-go 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 264 kB
  • sloc: sh: 215; javascript: 15; makefile: 11
file content (141 lines) | stat: -rw-r--r-- 3,627 bytes parent folder | download
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
139
140
141
package mysqlrouter

import (
	"encoding/json"
	"fmt"
	"time"
)

var (
	routeURL = "/routes"
)

// RoutesResponse is the response of getting all routes endpoint.
type RoutesResponse struct {
	Item []*Routes `json:"items"`
}

// Routes is the response of getting all routes endpoint.
type Routes struct {
	Name string `json:"name"`
}

// RouteStatus is the state of the route.
type RouteStatus struct {
	ActiveConnections int `json:"activeConnections"`
	TotalConnections  int `json:"totalConnections"`
	BlockedHosts      int `json:"blockedHosts"`
}

// RouteHealth is the structure of health check of the route.
type RouteHealth struct {
	IsAlive bool `json:"isAlive"`
}

// RouteDestinationsResponse is the response of getting the information of the route destination endpoint.
type RouteDestinationsResponse struct {
	Item []*RouteDestinations `json:"items"`
}

// RouteDestinations is the structure of destination of the route.
type RouteDestinations struct {
	Address string `json:"address"`
	Port    int    `json:"port"`
}

// RouteConnectionsResponse is the response of getting the information of route connection endpoint.
type RouteConnectionsResponse struct {
	Item []*RouteConnections `json:"items"`
}

// RouteConnections is the structure of the information of the connection of route.
type RouteConnections struct {
	BytesFromServer            int       `json:"bytesFromServer"`
	BytesToServer              int       `json:"bytesToServer"`
	SourceAddress              string    `json:"sourceAddress"`
	DestinationAddress         string    `json:"destinationAddress"`
	TimeStarted                time.Time `json:"timeStarted"`
	TimeConnectedToServer      time.Time `json:"timeConnectedToServer"`
	TimeLastSentToServer       time.Time `json:"timeLastSentToServer"`
	TimeLastReceivedFromServer time.Time `json:"timeLastReceivedFromServer"`
}

// GetAllRoutes returns all routes.
func (c *Client) GetAllRoutes() ([]*Routes, error) {
	b, err := c.request(c.URL + routeURL)
	if err != nil {
		return nil, err
	}

	var r *RoutesResponse
	err = json.Unmarshal(b, &r)
	if err != nil {
		return nil, err
	}

	return r.Item, nil
}

// GetRouteStatus return the status of the route.
func (c *Client) GetRouteStatus(rName string) (*RouteStatus, error) {
	b, err := c.request(fmt.Sprintf("%s%s/%s/status", c.URL, routeURL, rName))
	if err != nil {
		return nil, err
	}

	var rStatus *RouteStatus
	err = json.Unmarshal(b, &rStatus)
	if err != nil {
		return nil, err
	}

	return rStatus, nil
}

// GetRouteHealth returns the health check of the route.
func (c *Client) GetRouteHealth(rName string) (*RouteHealth, error) {
	b, err := c.request(fmt.Sprintf("%s%s/%s/health", c.URL, routeURL, rName))
	if err != nil {
		return nil, err
	}

	var rHealth *RouteHealth
	err = json.Unmarshal(b, &rHealth)
	if err != nil {
		return nil, err
	}

	return rHealth, nil
}

// GetRouteDestinations returns destinations of the route.
func (c *Client) GetRouteDestinations(rName string) ([]*RouteDestinations, error) {
	b, err := c.request(fmt.Sprintf("%s%s/%s/destinations", c.URL, routeURL, rName))
	if err != nil {
		return nil, err
	}

	var r *RouteDestinationsResponse
	err = json.Unmarshal(b, &r)
	if err != nil {
		return nil, err
	}

	return r.Item, nil
}

// GetRouteConnections return connections of the route.
func (c *Client) GetRouteConnections(rName string) ([]*RouteConnections, error) {
	b, err := c.request(fmt.Sprintf("%s%s/%s/connections", c.URL, routeURL, rName))
	if err != nil {
		return nil, err
	}

	var r *RouteConnectionsResponse
	err = json.Unmarshal(b, &r)
	if err != nil {
		return nil, err
	}

	return r.Item, nil
}