@@ -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+
115140func (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