Skip to content

Commit 5a89ec1

Browse files
committed
Server feature: Update
1. Add InnerIpPort and OuterIpPort functions 2. Add ListenPort, InnerIpPort and OuterIpPort methods to SrvConfig structure
1 parent 3e6d975 commit 5a89ec1

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

common.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,33 @@
1515
package ant
1616

1717
import (
18+
"net"
1819
"sync"
1920

21+
"github.com/henrylee2cn/goutil"
2022
tp "github.com/henrylee2cn/teleport"
2123
)
2224

25+
// InnerIpPort returns the service's intranet address, such as '192.168.1.120:8080'.
26+
func InnerIpPort(port string) (string, error) {
27+
host, err := goutil.IntranetIP()
28+
if err != nil {
29+
return "", err
30+
}
31+
hostPort := net.JoinHostPort(host, port)
32+
return hostPort, nil
33+
}
34+
35+
// OuterIpPort returns the service's extranet address, such as '113.116.141.121:8080'.
36+
func OuterIpPort(port string) (string, error) {
37+
host, err := goutil.ExtranetIP()
38+
if err != nil {
39+
return "", err
40+
}
41+
hostPort := net.JoinHostPort(host, port)
42+
return hostPort, nil
43+
}
44+
2345
var initOnce sync.Once
2446

2547
func doInit() {

samples/discovery/server.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,16 @@ func (p *P) Divide(args *Args) (int, *tp.Rerror) {
2424
}
2525

2626
func main() {
27-
srv := ant.NewServer(ant.SrvConfig{
27+
cfg := ant.SrvConfig{
2828
ListenAddress: ":9090",
2929
// EnableHeartbeat: true,
30-
},
31-
discovery.ServicePlugin(
32-
"localhost:9090",
33-
etcd.EasyConfig{
34-
Endpoints: []string{"http://127.0.0.1:2379"},
35-
},
36-
),
37-
)
30+
}
31+
srv := ant.NewServer(cfg, discovery.ServicePlugin(
32+
cfg.InnerIpPort(),
33+
etcd.EasyConfig{
34+
Endpoints: []string{"http://127.0.0.1:2379"},
35+
},
36+
))
3837
srv.RoutePull(new(P))
3938
srv.Listen()
4039
}

server.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,33 @@ func (s *SrvConfig) Reload(bind cfgo.BindFunc) error {
4848
return bind()
4949
}
5050

51+
// ListenPort returns the listened port, such as '8080'.
52+
func (s *SrvConfig) ListenPort() string {
53+
_, port, err := net.SplitHostPort(s.ListenAddress)
54+
if err != nil {
55+
Fatalf("%v", err)
56+
}
57+
return port
58+
}
59+
60+
// InnerIpPort returns the service's intranet address, such as '192.168.1.120:8080'.
61+
func (s *SrvConfig) InnerIpPort() string {
62+
hostPort, err := InnerIpPort(s.ListenPort())
63+
if err != nil {
64+
Fatalf("%v", err)
65+
}
66+
return hostPort
67+
}
68+
69+
// OuterIpPort returns the service's extranet address, such as '113.116.141.121:8080'.
70+
func (s *SrvConfig) OuterIpPort() string {
71+
hostPort, err := OuterIpPort(s.ListenPort())
72+
if err != nil {
73+
Fatalf("%v", err)
74+
}
75+
return hostPort
76+
}
77+
5178
func (s *SrvConfig) peerConfig() tp.PeerConfig {
5279
return tp.PeerConfig{
5380
DefaultSessionAge: s.DefaultSessionAge,

0 commit comments

Comments
 (0)