This commit is contained in:
Makis Maropoulos 2016-06-29 18:49:09 +03:00
parent 65184ddaca
commit f57d457d39
3 changed files with 24 additions and 11 deletions

View File

@ -30,11 +30,18 @@ type Server struct {
KeyFile string KeyFile string
// Mode this is for unix only // Mode this is for unix only
Mode os.FileMode 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 // DefaultServer returns the default configs for the server
func DefaultServer() Server { func DefaultServer() Server {
return Server{DefaultServerAddr, "", "", 0} return Server{ListeningAddr: DefaultServerAddr}
} }
// Merge merges the default with the given config and returns the result // Merge merges the default with the given config and returns the result

12
http.go
View File

@ -403,6 +403,18 @@ func (s *Server) Open() error {
s.Config.ListeningAddr = config.DefaultServerHostname + a 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 { if s.Config.Mode > 0 {
return s.listenUNIX() return s.listenUNIX()
} }

14
iris.go
View File

@ -244,9 +244,13 @@ func ListenTLS(addr string, certFile string, keyFile string) {
// if you need a func to panic on error use the ListenTLS // if you need a func to panic on error use the ListenTLS
// ex: log.Fatal(iris.ListenTLSWithErr(":8080","yourfile.cert","yourfile.key")) // ex: log.Fatal(iris.ListenTLSWithErr(":8080","yourfile.cert","yourfile.key"))
func (s *Framework) ListenTLSWithErr(addr string, certFile string, keyFile string) error { 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.ListeningAddr = addr
s.Config.Server.CertFile = certFile s.Config.Server.CertFile = certFile
s.Config.Server.KeyFile = keyFile s.Config.Server.KeyFile = keyFile
return s.openServer() 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. // 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 { 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) srv := newServer(&cfg)
// add a post listen event to start this server after the previous started // add a post listen event to start this server after the previous started
s.Plugins.Add(PostListenFunc(func(*Framework) { s.Plugins.Add(PostListenFunc(func(*Framework) {