From ae8b9aebd2f651130b682ae033e074a7e323c8ae Mon Sep 17 00:00:00 2001 From: acoshift Date: Fri, 18 May 2018 15:04:14 +0700 Subject: [PATCH] fix graceful shutdown --- app.go | 20 ++++++++++++++------ graceful.go | 4 ++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/app.go b/app.go index 17ef448..119148c 100644 --- a/app.go +++ b/app.go @@ -101,26 +101,34 @@ func (app *App) configServer(addr string) { app.srv.Addr = addr } -// ListenAndServe starts web server -func (app *App) ListenAndServe(addr string) error { +func (app *App) listenAndServe(addr string) error { + app.configServer(addr) + + return app.srv.ListenAndServe() +} + +func (app *App) listenAndServeTLS(addr, certFile, keyFile string) error { app.configServer(addr) + return app.srv.ListenAndServeTLS(certFile, keyFile) +} + +// ListenAndServe starts web server +func (app *App) ListenAndServe(addr string) error { if app.gracefulShutdown != nil { return app.GracefulShutdown().ListenAndServe(addr) } - return app.srv.ListenAndServe() + return app.listenAndServe(addr) } // ListenAndServeTLS starts web server in tls mode func (app *App) ListenAndServeTLS(addr, certFile, keyFile string) error { - app.configServer(addr) - if app.gracefulShutdown != nil { return app.GracefulShutdown().ListenAndServeTLS(addr, certFile, keyFile) } - return app.srv.ListenAndServeTLS(certFile, keyFile) + return app.listenAndServeTLS(addr, certFile, keyFile) } // GracefulShutdown returns graceful shutdown server diff --git a/graceful.go b/graceful.go index fc8cef1..bd4a5c9 100644 --- a/graceful.go +++ b/graceful.go @@ -94,10 +94,10 @@ func (gs *GracefulShutdown) start(listenAndServe func() error) (err error) { // ListenAndServe starts web server in graceful shutdown mode func (gs *GracefulShutdown) ListenAndServe(addr string) error { - return gs.start(func() error { return gs.App.ListenAndServe(addr) }) + return gs.start(func() error { return gs.App.listenAndServe(addr) }) } // ListenAndServeTLS starts web server in graceful shutdown and tls mode func (gs *GracefulShutdown) ListenAndServeTLS(addr, certFile, keyFile string) error { - return gs.start(func() error { return gs.App.ListenAndServeTLS(addr, certFile, keyFile) }) + return gs.start(func() error { return gs.App.listenAndServeTLS(addr, certFile, keyFile) }) }