From 40bc7f5054a11b12275f0d3ea83567af2a7c5e46 Mon Sep 17 00:00:00 2001 From: Gerasimos Maropoulos Date: Tue, 26 Jul 2016 20:19:50 +0300 Subject: [PATCH] Nothing special - Set a server.Config field which can override the ListeningAddr when server.Host()/Port. Mostly used inside glb tmpl funcs --- config/server.go | 14 +++++++++++++- http.go | 5 ++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/config/server.go b/config/server.go index 3b2fff03..3fbb52c2 100644 --- a/config/server.go +++ b/config/server.go @@ -79,6 +79,13 @@ type Server struct { RedirectTo string // Virtual If this server is not really listens to a real host, it mostly used in order to achieve testing without system modifications Virtual bool + // VListeningAddr, can be used for both virtual = true or false, + // if it's setted to not empty, then the server's Host() will return this addr instead of the ListeningAddr. + // server's Host() is used inside global template helper funcs + // set it when you are sure you know what it does. + // + // Default is empty "" + VListeningAddr string // Name the server's name, defaults to "iris". // You're free to change it, but I will trust you to don't, this is the only setting whose somebody, like me, can see if iris web framework is used Name string @@ -117,10 +124,15 @@ func ServerParseAddr(listeningAddr string) string { // DefaultServer returns the default configs for the server func DefaultServer() Server { - return Server{ListeningAddr: DefaultServerAddr, Name: DefaultServerName, + return Server{ + ListeningAddr: DefaultServerAddr, + Name: DefaultServerName, MaxRequestBodySize: DefaultMaxRequestBodySize, ReadBufferSize: DefaultReadBufferSize, WriteBufferSize: DefaultWriteBufferSize, + RedirectTo: "", + Virtual: false, + VListeningAddr: "", } } diff --git a/http.go b/http.go index 55281e12..b05e588a 100644 --- a/http.go +++ b/http.go @@ -303,13 +303,16 @@ func (s *Server) Listener() net.Listener { // Host returns the registered host for the server func (s *Server) Host() (host string) { + if s.Config.VListeningAddr != "" { + return s.Config.VListeningAddr + } return s.Config.ListeningAddr } // Port returns the port which server listening for // if no port given with the ListeningAddr, it returns 80 func (s *Server) Port() (port int) { - a := s.Config.ListeningAddr + a := s.Host() if portIdx := strings.IndexByte(a, ':'); portIdx != -1 { p, err := strconv.Atoi(a[portIdx+1:]) if err != nil {