Skip to content

Commit 760bd52

Browse files
committed
use host and port instead of address
1 parent 01ea0a3 commit 760bd52

File tree

3 files changed

+44
-31
lines changed

3 files changed

+44
-31
lines changed

node-registrar/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
run:
2-
go run cmds/main.go --postgres-host localhost --postgres-port 5432 --postgres-db postgres --postgres-user postgres --postgres-password password --address localhost --server-port 8080
2+
go run cmds/main.go --postgres-host localhost --postgres-port 5432 --postgres-db postgres --postgres-user postgres --postgres-password password --host localhost --server-port 8080
33

44
start-postgres:
55
docker run --name postgres -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password -e POSTGRES_DB=postgres -p 5432:5432 -d postgres
@@ -14,7 +14,7 @@ server-start:
1414
@go run cmds/main.go \
1515
--server-port 8080 \
1616
--debug \
17-
--address localhost \
17+
--host localhost \
1818
--sql-log-level 4 \
1919
--postgres-host localhost \
2020
--postgres-port 5432 \

node-registrar/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Node Registrar Service
32

43
[![Go Report Card](https://goreportcard.com/badge/github.com/threefoldtech/tfgrid-sdk-go/node-registrar)](https://goreportcard.com/report/github.com/threefoldtech/tfgrid-sdk-go/node-registrar)
@@ -76,10 +75,10 @@ It offers operations like registring, listing, and updating farms and nodes, as
7675
Once the server is running, Swagger documentation can be accessed at:
7776

7877
```bash
79-
http://<address>:<port>/swagger/index.html
78+
http://<host>:<port>/swagger/index.html
8079
```
8180

82-
Replace `<address>` and `<port>` with the appropriate values.
81+
Replace `<host>` and `<port>` with the appropriate values.
8382

8483
## How to Use the Server
8584

@@ -112,7 +111,7 @@ docker build -t registrar:latest -f node-registrar/Dockerfile .
112111
--max-open-conn=10 \
113112
--max-idle-conn=5 \
114113
--server-port=8080 \
115-
--<address=your-address-or-domain> \
114+
--host=<your-host> \
116115
--network=main\
117116
--admin_twin_id=1
118117
--debug

node-registrar/cmds/main.go

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ type flags struct {
1919
db.Config
2020
debug bool
2121
version bool
22-
addr string
23-
serverPort uint
22+
host string
23+
port uint
2424
network string
2525
adminTwinID uint64
2626
}
@@ -57,13 +57,13 @@ func Run() error {
5757

5858
flag.BoolVar(&f.version, "v", false, "shows the package version")
5959
flag.BoolVar(&f.debug, "debug", false, "allow debug logs")
60-
flag.UintVar(&f.serverPort, "server-port", 8080, "server port")
61-
flag.StringVar(&f.addr, "address", "", "address or domain on which the server will be served")
60+
flag.UintVar(&f.port, "server-port", 8080, "server port")
61+
flag.StringVar(&f.host, "host", "", "host on which the server will be served")
6262

6363
// Deprecated flag handling
64-
flag.Func("domain", "deprecated: use --address instead", func(val string) error {
65-
log.Warn().Msg("Warning: --domain flag is deprecated, please use --address instead")
66-
f.addr = val
64+
flag.Func("domain", "deprecated: use --host instead", func(val string) error {
65+
log.Warn().Msg("Warning: --domain flag is deprecated, please use --host instead")
66+
f.host = val
6767
return nil
6868
})
6969

@@ -102,42 +102,56 @@ func Run() error {
102102

103103
s := server.NewServer(db, f.network, f.adminTwinID)
104104

105-
log.Info().Msgf("server is running on port :%d", f.serverPort)
105+
log.Info().Msgf("server is running on port :%d", f.port)
106106

107-
err = s.Run(fmt.Sprintf("%s:%d", f.addr, f.serverPort))
107+
err = s.Run(fmt.Sprintf("%s:%d", f.host, f.port))
108108
if err != nil {
109109
return errors.Wrap(err, "failed to run gin server")
110110
}
111111

112112
return nil
113113
}
114114

115+
func (f flags) validateHost() error {
116+
host := strings.TrimSpace(f.host)
117+
if host == "" {
118+
return errors.New("host cannot be empty")
119+
}
120+
121+
// Check common binding addresses
122+
switch host {
123+
case "localhost", "0.0.0.0", "127.0.0.1":
124+
return nil
125+
}
126+
127+
// Check if valid IP address
128+
if ip := net.ParseIP(host); ip != nil {
129+
return nil
130+
}
131+
132+
// Check if valid hostname
133+
if _, err := net.LookupHost(host); err != nil {
134+
return errors.Wrapf(err, "invalid host %q: must be a valid IP address, hostname, or domain name", host)
135+
}
136+
137+
return nil
138+
}
139+
115140
func (f flags) validate() error {
116-
if f.serverPort < 1 || f.serverPort > 65535 {
117-
return errors.Errorf("invalid port %d, server port should be in the valid port range 1–65535", f.serverPort)
141+
if f.port < 1 || f.port > 65535 {
142+
return errors.Errorf("invalid port %d, server port should be in the valid port range 1–65535", f.port)
118143
}
119144

120145
if f.SqlLogLevel < 1 || f.SqlLogLevel > 4 {
121146
return errors.Errorf("invalid sql log level %d, sql log level should be in the range 1-4", f.SqlLogLevel)
122147
}
148+
123149
if f.adminTwinID == 0 {
124150
return errors.Errorf("invalid admin twin id %d, admin twin id should not be 0", f.adminTwinID)
125151
}
126152

127-
if strings.TrimSpace(f.addr) == "" {
128-
return errors.New("invalid domain/address, should not be empty")
129-
}
130-
131-
// Skip validation for common binding addresses
132-
if f.addr == "0.0.0.0" || f.addr == "localhost" || f.addr == "127.0.0.1" {
133-
return f.Config.Validate()
134-
}
135-
136-
if net.ParseIP(f.addr) == nil {
137-
// if not valid IP address, check if valid domain
138-
if _, err := net.LookupHost(f.addr); err != nil {
139-
return errors.Wrapf(err, "invalid domain %s", f.addr)
140-
}
153+
if err := f.validateHost(); err != nil {
154+
return err
141155
}
142156

143157
return f.Validate()

0 commit comments

Comments
 (0)