Skip to content

Commit

Permalink
more updates to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mec07 committed Jun 5, 2019
1 parent a7f9f1e commit d0e734b
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions rununtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ For example:
}
}
}
func runHTTPServer(srv *http.Server) {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal().Stack().Err(err).Msg("ListenAndServe")
Expand All @@ -28,10 +29,25 @@ For example:
rununtil.KillSignal(Runner)
}
A nice pattern is to create a function that takes in the various depencies required, e.g. configuration, logger, private key, etc., and returns a runner function, e.g.
A nice pattern is to create a function that takes in the various depencies required, for example, a logger (but could be anything, e.g. configs, database, etc.), and returns a runner function:
func NewRunner(log *zerolog.Logger) func() func() {
return func() func() {
...all that was in the Runner function above...
r := chi.NewRouter()
r.Get("/healthz", healthzHandler)
httpServer := &http.Server{Addr: ":8080", Handler: r}
go runHTTPServer(httpServer, log)
return func() {
if err := httpServer.Shutdown(context.Background()); err != nil {
log.Error().Err(err).Msg("error occurred while shutting down http server")
}
}
}
}
func runHTTPServer(srv *http.Server, log *zerolog.Logger) {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal().Stack().Err(err).Msg("ListenAndServe")
}
}
Expand All @@ -52,7 +68,7 @@ import (
)

// KillSignal runs the provided runner function until it receives a kill signal,
// at which point it executes the graceful shutdown function.
// SIGINT or SIGTERM, at which point it executes the graceful shutdown function.
func KillSignal(runner func() func()) {
Signals(runner, syscall.SIGINT, syscall.SIGTERM)
}
Expand Down

0 comments on commit d0e734b

Please sign in to comment.