Set the default hostname to "0.0.0.0" from "127.0.0.1" - as golang's net.Listener does by default

I changed that because some guys has problems in their hosting machines,
and they should use the "127.0..0.1" instead of "0.0.0.0", if you have
problems just pass `iris.Listen("127.0.0.1:8080") instead of
`iris.Listen(":8080")`
This commit is contained in:
Gerasimos Maropoulos 2016-07-21 00:03:36 +03:00
parent 352b297977
commit 0dbab32d9d
4 changed files with 11 additions and 6 deletions

View File

@ -11,8 +11,8 @@ import (
// Default values for base Server conf // Default values for base Server conf
const ( const (
// DefaultServerHostname returns the default hostname which is 127.0.0.1 // DefaultServerHostname returns the default hostname which is 0.0.0.0
DefaultServerHostname = "127.0.0.1" DefaultServerHostname = "0.0.0.0"
// DefaultServerPort returns the default port which is 8080 // DefaultServerPort returns the default port which is 8080
DefaultServerPort = 8080 DefaultServerPort = 8080
// DefaultMaxRequestBodySize is 8MB // DefaultMaxRequestBodySize is 8MB
@ -37,7 +37,7 @@ const (
) )
var ( 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) DefaultServerAddr = DefaultServerHostname + ":" + strconv.Itoa(DefaultServerPort)
) )

View File

@ -238,9 +238,14 @@ func (ctx *Context) VirtualHostname() string {
} }
if idxDotAnd := strings.LastIndexByte(hostname, '.'); idxDotAnd > 0 { if idxDotAnd := strings.LastIndexByte(hostname, '.'); idxDotAnd > 0 {
s := hostname[idxDotAnd:] 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" { if s == ".1" {
hostname = strings.Replace(hostname, "127.0.0.1", virtualhost, 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 { } else {
hostname = strings.Replace(hostname, "localhost", virtualhost, 1) hostname = strings.Replace(hostname, "localhost", virtualhost, 1)
} }

View File

@ -1309,7 +1309,7 @@ type (
api *muxAPI api *muxAPI
errorHandlers map[int]Handler errorHandlers map[int]Handler
logger *logger.Logger 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 hostname string
// if any of the trees contains not empty subdomain // if any of the trees contains not empty subdomain
hosts bool hosts bool
@ -1328,7 +1328,7 @@ func newServeMux(contextPool sync.Pool, logger *logger.Logger) *serveMux {
cPool: &contextPool, cPool: &contextPool,
lookups: make([]*route, 0), lookups: make([]*route, 0),
errorHandlers: make(map[int]Handler, 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, escapePath: !config.DefaultDisablePathEscape,
correctPath: !config.DefaultDisablePathCorrection, correctPath: !config.DefaultDisablePathCorrection,
logger: logger, logger: logger,

View File

@ -73,7 +73,7 @@ func TestServerHost(t *testing.T) {
var server1, server2, server3 Server var server1, server2, server3 Server
var expectedHost1 = "mydomain.com:1993" var expectedHost1 = "mydomain.com:1993"
var expectedHost2 = "mydomain.com:80" var expectedHost2 = "mydomain.com:80"
var expectedHost3 = "127.0.0.1:9090" var expectedHost3 = config.DefaultServerHostname + ":9090"
server1.Config.ListeningAddr = expectedHost1 server1.Config.ListeningAddr = expectedHost1
server2.Config.ListeningAddr = "mydomain.com" server2.Config.ListeningAddr = "mydomain.com"
server3.Config.ListeningAddr = ":9090" server3.Config.ListeningAddr = ":9090"