diff --git a/config/server.go b/config/server.go index 1652b990..3b2fff03 100644 --- a/config/server.go +++ b/config/server.go @@ -11,8 +11,8 @@ import ( // Default values for base Server conf const ( - // DefaultServerHostname returns the default hostname which is 127.0.0.1 - DefaultServerHostname = "127.0.0.1" + // DefaultServerHostname returns the default hostname which is 0.0.0.0 + DefaultServerHostname = "0.0.0.0" // DefaultServerPort returns the default port which is 8080 DefaultServerPort = 8080 // DefaultMaxRequestBodySize is 8MB @@ -37,7 +37,7 @@ const ( ) var ( - // DefaultServerAddr the default server addr which is: 127.0.0.1:8080 + // DefaultServerAddr the default server addr which is: 0.0.0.0:8080 DefaultServerAddr = DefaultServerHostname + ":" + strconv.Itoa(DefaultServerPort) ) diff --git a/context.go b/context.go index 86b1339e..2b4b05de 100644 --- a/context.go +++ b/context.go @@ -238,9 +238,14 @@ func (ctx *Context) VirtualHostname() string { } if idxDotAnd := strings.LastIndexByte(hostname, '.'); idxDotAnd > 0 { s := hostname[idxDotAnd:] + // means that we have the request's host mymachine.com or 127.0.0.1/0.0.0.0, but for the second option we will need to replace it with the hostname that the dev was registered + // this needed to parse correct the {{ url }} iris global template engine's function if s == ".1" { hostname = strings.Replace(hostname, "127.0.0.1", virtualhost, 1) + } else if s == ".0" { + hostname = strings.Replace(hostname, "0.0.0.0", virtualhost, 1) } + // } else { hostname = strings.Replace(hostname, "localhost", virtualhost, 1) } diff --git a/http.go b/http.go index d4336086..55281e12 100644 --- a/http.go +++ b/http.go @@ -1309,7 +1309,7 @@ type ( api *muxAPI errorHandlers map[int]Handler logger *logger.Logger - // the main server host's name, ex: localhost, 127.0.0.1, iris-go.com + // the main server host's name, ex: localhost, 127.0.0.1, 0.0.0.0, iris-go.com hostname string // if any of the trees contains not empty subdomain hosts bool @@ -1328,7 +1328,7 @@ func newServeMux(contextPool sync.Pool, logger *logger.Logger) *serveMux { cPool: &contextPool, lookups: make([]*route, 0), errorHandlers: make(map[int]Handler, 0), - hostname: "127.0.0.1", + hostname: config.DefaultServerHostname, // these are changing when the server is up escapePath: !config.DefaultDisablePathEscape, correctPath: !config.DefaultDisablePathCorrection, logger: logger, diff --git a/http_test.go b/http_test.go index 467d993f..8f9dbd9e 100644 --- a/http_test.go +++ b/http_test.go @@ -73,7 +73,7 @@ func TestServerHost(t *testing.T) { var server1, server2, server3 Server var expectedHost1 = "mydomain.com:1993" var expectedHost2 = "mydomain.com:80" - var expectedHost3 = "127.0.0.1:9090" + var expectedHost3 = config.DefaultServerHostname + ":9090" server1.Config.ListeningAddr = expectedHost1 server2.Config.ListeningAddr = "mydomain.com" server3.Config.ListeningAddr = ":9090"