From f57d457d3942bf2985411d1171dcb0fc28cf6021 Mon Sep 17 00:00:00 2001 From: Makis Maropoulos Date: Wed, 29 Jun 2016 18:49:09 +0300 Subject: [PATCH] Finish the feature request https://github.com/kataras/iris/issues/235#issuecomment-229383650 --- config/server.go | 9 ++++++++- http.go | 12 ++++++++++++ iris.go | 14 ++++---------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/config/server.go b/config/server.go index 3b41ddc8..b580fce5 100644 --- a/config/server.go +++ b/config/server.go @@ -30,11 +30,18 @@ type Server struct { KeyFile string // Mode this is for unix only Mode os.FileMode + // RedirectTo, defaults to empty, set it in order to override the station's handler and redirect all requests to this address which is of form(HOST:PORT or :PORT) + // + // NOTE: the http status is 'StatusMovedPermanently', means one-time-redirect(the browser remembers the new addr and goes to the new address without need to request something from this server + // which means that if you want to change this address you have to clear your browser's cache in order this to be able to change to the new addr. + // + // example: https://github.com/iris-contrib/examples/tree/master/multiserver_listening2 + RedirectTo string } // DefaultServer returns the default configs for the server func DefaultServer() Server { - return Server{DefaultServerAddr, "", "", 0} + return Server{ListeningAddr: DefaultServerAddr} } // Merge merges the default with the given config and returns the result diff --git a/http.go b/http.go index b433254f..5eb0802b 100644 --- a/http.go +++ b/http.go @@ -403,6 +403,18 @@ func (s *Server) Open() error { s.Config.ListeningAddr = config.DefaultServerHostname + a } + if s.Config.RedirectTo != "" { + // override the handler and redirect all requests to this addr + s.Handler = func(reqCtx *fasthttp.RequestCtx) { + path := string(reqCtx.Path()) + redirectTo := s.Config.RedirectTo + if path != "/" { + redirectTo += "/" + path + } + reqCtx.Redirect(redirectTo, StatusMovedPermanently) + } + } + if s.Config.Mode > 0 { return s.listenUNIX() } diff --git a/iris.go b/iris.go index 90e2d052..381f88de 100644 --- a/iris.go +++ b/iris.go @@ -244,9 +244,13 @@ func ListenTLS(addr string, certFile string, keyFile string) { // if you need a func to panic on error use the ListenTLS // ex: log.Fatal(iris.ListenTLSWithErr(":8080","yourfile.cert","yourfile.key")) func (s *Framework) ListenTLSWithErr(addr string, certFile string, keyFile string) error { + if certFile == "" || keyFile == "" { + return fmt.Errorf("You should provide certFile and keyFile for TLS/SSL") + } s.Config.Server.ListeningAddr = addr s.Config.Server.CertFile = certFile s.Config.Server.KeyFile = keyFile + return s.openServer() } @@ -581,16 +585,6 @@ func SecondaryListen(cfg config.Server) *Server { // // this is a NOT A BLOCKING version, the main iris.Listen should be always executed LAST, so this function goes before the main .Listen. func (s *Framework) SecondaryListen(cfg config.Server) *Server { - /*This not suits us because if prelisten plugins makes a lot of time to execute then the IsListening is false after 3 seconds (if the plugins have much work to do) - 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() { - s.HTTPServer.Config = &cfg - s.openServer() - return s.HTTPServer - }*/ - srv := newServer(&cfg) // add a post listen event to start this server after the previous started s.Plugins.Add(PostListenFunc(func(*Framework) {