From d3aedbeb9ad73de46564f1212b657a715d644e3e Mon Sep 17 00:00:00 2001 From: Nimi Wariboko Jr Date: Mon, 4 May 2015 21:38:49 -0700 Subject: [PATCH 1/2] core: add bindaddr directive, allowing you to specify what address to listen on --- config/directives.go | 1 + config/setup/bindaddr.go | 12 ++++++++++++ server/config.go | 6 ++++++ 3 files changed, 19 insertions(+) create mode 100644 config/setup/bindaddr.go diff --git a/config/directives.go b/config/directives.go index 679ea5ea2..4f7c5b8a2 100644 --- a/config/directives.go +++ b/config/directives.go @@ -43,6 +43,7 @@ var directiveOrder = []directive{ // Essential directives that initialize vital configuration settings {"root", setup.Root}, {"tls", setup.TLS}, + {"bindaddr", setup.BindAddr}, // Other directives that don't create HTTP handlers {"startup", setup.Startup}, diff --git a/config/setup/bindaddr.go b/config/setup/bindaddr.go new file mode 100644 index 000000000..23b1eb2ed --- /dev/null +++ b/config/setup/bindaddr.go @@ -0,0 +1,12 @@ +package setup + +import "github.com/mholt/caddy/middleware" + +func BindAddr(c *Controller) (middleware.Middleware, error) { + for c.Next() { + if !c.Args(&c.BindAddress) { + return nil, c.ArgErr() + } + } + return nil, nil +} diff --git a/server/config.go b/server/config.go index 591f9a48c..a45e4797b 100644 --- a/server/config.go +++ b/server/config.go @@ -11,6 +11,9 @@ type Config struct { // The hostname or IP on which to serve Host string + // The address to bind on - defaults to Host if empty + BindAddress string + // The port to listen on Port string @@ -44,6 +47,9 @@ type Config struct { // Address returns the host:port of c as a string. func (c Config) Address() string { + if c.BindAddress != "" { + return net.JoinHostPort(c.BindAddress, c.Port) + } return net.JoinHostPort(c.Host, c.Port) } From 46f7930787aed2b133248ad89dd4c750ed6ed4fc Mon Sep 17 00:00:00 2001 From: Nimi Wariboko Jr Date: Mon, 4 May 2015 22:58:08 -0700 Subject: [PATCH 2/2] Rename bindaddr to just bind --- config/directives.go | 2 +- config/setup/{bindaddr.go => bindhost.go} | 4 ++-- server/config.go | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) rename config/setup/{bindaddr.go => bindhost.go} (57%) diff --git a/config/directives.go b/config/directives.go index 4f7c5b8a2..fe9a9f929 100644 --- a/config/directives.go +++ b/config/directives.go @@ -43,7 +43,7 @@ var directiveOrder = []directive{ // Essential directives that initialize vital configuration settings {"root", setup.Root}, {"tls", setup.TLS}, - {"bindaddr", setup.BindAddr}, + {"bind", setup.BindHost}, // Other directives that don't create HTTP handlers {"startup", setup.Startup}, diff --git a/config/setup/bindaddr.go b/config/setup/bindhost.go similarity index 57% rename from config/setup/bindaddr.go rename to config/setup/bindhost.go index 23b1eb2ed..3e4bf89b3 100644 --- a/config/setup/bindaddr.go +++ b/config/setup/bindhost.go @@ -2,9 +2,9 @@ package setup import "github.com/mholt/caddy/middleware" -func BindAddr(c *Controller) (middleware.Middleware, error) { +func BindHost(c *Controller) (middleware.Middleware, error) { for c.Next() { - if !c.Args(&c.BindAddress) { + if !c.Args(&c.BindHost) { return nil, c.ArgErr() } } diff --git a/server/config.go b/server/config.go index a45e4797b..b037b613c 100644 --- a/server/config.go +++ b/server/config.go @@ -11,8 +11,8 @@ type Config struct { // The hostname or IP on which to serve Host string - // The address to bind on - defaults to Host if empty - BindAddress string + // The host address to bind on - defaults to (virtual) Host if empty + BindHost string // The port to listen on Port string @@ -47,8 +47,8 @@ type Config struct { // Address returns the host:port of c as a string. func (c Config) Address() string { - if c.BindAddress != "" { - return net.JoinHostPort(c.BindAddress, c.Port) + if c.BindHost != "" { + return net.JoinHostPort(c.BindHost, c.Port) } return net.JoinHostPort(c.Host, c.Port) }