Skip to content

Commit

Permalink
fix can not access property or method of http.Server
Browse files Browse the repository at this point in the history
  • Loading branch information
tabalt committed Jun 18, 2019
1 parent c6579a3 commit c8f634e
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ const (

// HTTP server that supported graceful shutdown or restart
type Server struct {
httpServer *http.Server
listener net.Listener
*http.Server

listener net.Listener

isGraceful bool
signalChan chan os.Signal
Expand All @@ -37,7 +38,7 @@ func NewServer(addr string, handler http.Handler, readTimeout, writeTimeout time
}

return &Server{
httpServer: &http.Server{
Server: &http.Server{
Addr: addr,
Handler: handler,

Expand All @@ -52,7 +53,7 @@ func NewServer(addr string, handler http.Handler, readTimeout, writeTimeout time
}

func (srv *Server) ListenAndServe() error {
addr := srv.httpServer.Addr
addr := srv.Addr
if addr == "" {
addr = ":http"
}
Expand All @@ -67,14 +68,14 @@ func (srv *Server) ListenAndServe() error {
}

func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error {
addr := srv.httpServer.Addr
addr := srv.Addr
if addr == "" {
addr = ":https"
}

config := &tls.Config{}
if srv.httpServer.TLSConfig != nil {
*config = *srv.httpServer.TLSConfig
if srv.TLSConfig != nil {
*config = *srv.TLSConfig
}
if config.NextProtos == nil {
config.NextProtos = []string{"http/1.1"}
Expand All @@ -98,7 +99,7 @@ func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error {

func (srv *Server) Serve() error {
go srv.handleSignals()
err := srv.httpServer.Serve(srv.listener)
err := srv.Server.Serve(srv.listener)

srv.logf("waiting for connections closed.")
<-srv.shutdownChan
Expand Down Expand Up @@ -158,7 +159,7 @@ func (srv *Server) handleSignals() {
}

func (srv *Server) shutdownHTTPServer() {
if err := srv.httpServer.Shutdown(context.Background()); err != nil {
if err := srv.Shutdown(context.Background()); err != nil {
srv.logf("HTTP server shutdown error: %v", err)
} else {
srv.logf("HTTP server shutdown success.")
Expand Down Expand Up @@ -207,8 +208,8 @@ func (srv *Server) logf(format string, args ...interface{}) {
pids := strconv.Itoa(os.Getpid())
format = "[pid " + pids + "] " + format

if srv.httpServer.ErrorLog != nil {
srv.httpServer.ErrorLog.Printf(format, args...)
if srv.ErrorLog != nil {
srv.ErrorLog.Printf(format, args...)
} else {
log.Printf(format, args...)
}
Expand Down

0 comments on commit c8f634e

Please sign in to comment.