https://github.com/josestg/gracehttp
A wrapper around the net/http.Server that can be shutdown gracefully.
https://github.com/josestg/gracehttp
Last synced: about 1 month ago
JSON representation
A wrapper around the net/http.Server that can be shutdown gracefully.
- Host: GitHub
- URL: https://github.com/josestg/gracehttp
- Owner: josestg
- License: mit
- Created: 2024-02-18T12:33:59.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-02-28T12:50:25.000Z (about 1 year ago)
- Last Synced: 2025-01-31T06:47:17.123Z (3 months ago)
- Language: Go
- Homepage:
- Size: 5.86 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gracehttp
A wrapper around the `net/http.Server` that can be shutdown gracefully.
To read more about graceful shutdowns, see [this blog post](https://blog.stackademic.com/graceful-shutdown-in-go-820d28e1d1c4).
## Installation
```shell
go get github.com/josestg/gracehttp
```## Usage
```go
package mainimport (
"log/slog"
"net/http"
"os"
"syscall"
"time""github.com/josestg/gracehttp"
)func main() {
log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{}))serv := http.Server{Addr: ":8081"}
opts := []gracehttp.Option{
gracehttp.WithLogger(log),
gracehttp.WithSignals(syscall.SIGINT, syscall.SIGTERM), // default is syscall.SIGINT, syscall.SIGTERM
gracehttp.WithWaitTimeout(10 * time.Second), // default is 5 seconds
}gs := gracehttp.NewGracefulShutdownServer(&serv, opts...)
log.Info("server started")
defer log.Info("server stopped")if err := gs.ListenAndServe(); err != nil {
log.Error("server failed to start", "err", err)
os.Exit(1)
}
}
```