-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
gsvc_endpoint.go
55 lines (47 loc) · 1.51 KB
/
gsvc_endpoint.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
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package gsvc provides service registry and discovery definition.
package gsvc
import (
"fmt"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
)
// LocalEndpoint implements interface Endpoint.
type LocalEndpoint struct {
host string // host can be either IPv4 or IPv6 address.
port int // port is port as commonly known.
}
// NewEndpoint creates and returns an Endpoint from address string of pattern "host:port",
// eg: "192.168.1.100:80".
func NewEndpoint(address string) Endpoint {
array := gstr.SplitAndTrim(address, endpointHostPortDelimiter)
if len(array) != 2 {
panic(gerror.NewCodef(
gcode.CodeInvalidParameter,
`invalid address "%s" for creating endpoint, endpoint address is like "ip:port"`,
address,
))
}
return &LocalEndpoint{
host: array[0],
port: gconv.Int(array[1]),
}
}
// Host returns the IPv4/IPv6 address of a service.
func (e *LocalEndpoint) Host() string {
return e.host
}
// Port returns the port of a service.
func (e *LocalEndpoint) Port() int {
return e.port
}
// String formats and returns the Endpoint as a string, like: 192.168.1.100:80.
func (e *LocalEndpoint) String() string {
return fmt.Sprintf(`%s:%d`, e.host, e.port)
}