Multiserver listening - Try first for https://github.com/kataras/iris/issues/235 - example added

This commit is contained in:
Makis Maropoulos 2016-06-29 16:25:17 +03:00
parent 9d8f32ce1a
commit 5b8f4dc1b0
3 changed files with 52 additions and 2 deletions

View File

@ -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()
}

View File

@ -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

50
iris.go
View File

@ -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------------------------------