From 5b8f4dc1b0fa047516f513c8c0015f218fbd7a8c Mon Sep 17 00:00:00 2001 From: Makis Maropoulos Date: Wed, 29 Jun 2016 16:25:17 +0300 Subject: [PATCH] Multiserver listening - Try first for https://github.com/kataras/iris/issues/235 - example added --- http.go | 2 +- initiatory.go | 2 +- iris.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/http.go b/http.go index 4902e85a..b433254f 100644 --- a/http.go +++ b/http.go @@ -410,7 +410,7 @@ func (s *Server) Open() error { } // close closes the server -func (s *Server) close() (err error) { +func (s *Server) Close() (err error) { if !s.IsListening() { return errServerIsClosed.Return() } diff --git a/initiatory.go b/initiatory.go index 56743fe3..2480f793 100644 --- a/initiatory.go +++ b/initiatory.go @@ -176,7 +176,7 @@ func (s *Framework) openServer() (err error) { // closeServer is used to close the tcp listener from the server, returns an error func (s *Framework) closeServer() error { s.Plugins.DoPreClose(s) - return s.HTTPServer.close() + return s.HTTPServer.Close() } // justServe initializes the whole framework but server doesn't listens to a specific net.Listener diff --git a/iris.go b/iris.go index 62853a22..684ad4b8 100644 --- a/iris.go +++ b/iris.go @@ -90,6 +90,7 @@ type ( ListenUNIXWithErr(string, os.FileMode) error ListenUNIX(string, os.FileMode) NoListen() *Server + ListenToServer(config.Server) (*Server, error) Close() // global middleware prepending, registers to all subdomains, to all parties, you can call it at the last also MustUse(...Handler) @@ -553,6 +554,55 @@ func (s *Framework) TemplateString(templateFile string, pageContext interface{}, return res } +// ListenToServer starts a 'secondary' server which listens to this station +// this server will not be the main server (unless it's the first listen) +// Note that the view engine's functions {{ url }} and {{ urlpath }} will return the first's registered server's scheme (http/https) +// +// this is useful only when you want to have two listen ports ( two servers ) for the same station +// +// receives one parameter which is the config.Server for the new server +// returns the new standalone server( you can close this server by this reference) and an error +// +// If you have only one scheme this function is useless for you, instead you can use the normal .Listen/ListenTLS functions. +// +// this is a blocking version, like .Listen/ListenTLS +func ListenToServer(cfg config.Server) (*Server, error) { + return Default.ListenToServer(cfg) +} + +// ListenToServer starts a server which listens to this station +// Note that the view engine's functions {{ url }} and {{ urlpath }} will return the first's registered server's scheme (http/https) +// +// this is useful only when you want to have two listen ports ( two servers ) for the same station +// +// receives one parameter which is the config.Server for the new server +// returns the new standalone server( you can close this server by this reference) and an error +// +// If you have only one scheme this function is useless for you, instead you can use the normal .Listen/ListenTLS functions. +// +// this is a blocking version, like .Listen/ListenTLS +func (s *Framework) ListenToServer(cfg config.Server) (*Server, error) { + time.Sleep(time.Duration(3) * time.Second) // yes we wait, so simple, because the previous will run on goroutine + // if the main server is not yet started, then this is the main server + // although this function should not be used to Listen to the main server, but if so then do it right: + if !s.HTTPServer.IsListening() { + println("samae server") + s.HTTPServer.Config = &cfg + err := s.openServer() + return s.HTTPServer, err + } + + srv := newServer(&cfg) + srv.Handler = s.HTTPServer.Handler + err := srv.Open() + if err == nil { + ch := make(chan os.Signal) + <-ch + srv.Close() + } + return srv, err +} + // ------------------------------------------------------------------------------------- // ------------------------------------------------------------------------------------- // ----------------------------------MuxAPI implementation------------------------------