Skip to content

Commit

Permalink
add https listen and serve
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Nov 5, 2020
1 parent afffc10 commit c9ec22d
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions rest/internal/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,21 @@ import (
func StartHttp(host string, port int, handler http.Handler) error {
addr := fmt.Sprintf("%s:%d", host, port)
server := buildHttpServer(addr, handler)
return StartServer(server)
gracefulOnShutdown(server)
return server.ListenAndServe()
}

func StartHttps(host string, port int, certFile, keyFile string, handler http.Handler) error {
addr := fmt.Sprintf("%s:%d", host, port)
if server, err := buildHttpsServer(addr, handler, certFile, keyFile); err != nil {
return err
} else {
return StartServer(server)
gracefulOnShutdown(server)
// certFile and keyFile are set in buildHttpsServer
return server.ListenAndServeTLS("", "")
}
}

func StartServer(srv *http.Server) error {
proc.AddWrapUpListener(func() {
srv.Shutdown(context.Background())
})

return srv.ListenAndServe()
}

func buildHttpServer(addr string, handler http.Handler) *http.Server {
return &http.Server{Addr: addr, Handler: handler}
}
Expand All @@ -49,3 +44,9 @@ func buildHttpsServer(addr string, handler http.Handler, certFile, keyFile strin
TLSConfig: &config,
}, nil
}

func gracefulOnShutdown(srv *http.Server) {
proc.AddWrapUpListener(func() {
srv.Shutdown(context.Background())
})
}

0 comments on commit c9ec22d

Please sign in to comment.