https://github.com/dlampsi/gsrv
Wrapper for starting and maintaining HTTP and gRPC server
https://github.com/dlampsi/gsrv
go golang golang-library grpc-go grpc-server http http-server
Last synced: about 1 year ago
JSON representation
Wrapper for starting and maintaining HTTP and gRPC server
- Host: GitHub
- URL: https://github.com/dlampsi/gsrv
- Owner: dlampsi
- License: mit
- Created: 2023-03-02T19:57:51.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-03T07:42:42.000Z (about 3 years ago)
- Last Synced: 2025-02-05T02:43:27.954Z (about 1 year ago)
- Topics: go, golang, golang-library, grpc-go, grpc-server, http, http-server
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gsrv
[](https://pkg.go.dev/github.com/dlampsi/gsrv)
A useful wrapper for starting and maintaining HTTP and gRPC servers with graceful shutdown on the root context cancel.
## Usage
Basic usage:
```go
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/dlampsi/gsrv"
)
var (
shutdownTimeout := 15 * time.Second
)
func main() {
// Root context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Listening for OS interupt signals
terminateCh := make(chan os.Signal, 1)
signal.Notify(terminateCh, syscall.SIGINT, syscall.SIGTERM)
go func() {
select {
case <-terminateCh:
cancel()
case <-ctx.Done():
}
}()
// GSRV server
srv, err := gsrv.New(
"0.0.0.0:8080",
gsrv.WithTimeout(shutdownTimeout),
)
if err != nil {
// Process the error
}
/*
Here comes your custom HTTP handler (router)
which can be any framework (like gin or gorilla)
*/
handler := CreateYourCustomHttpHandler()
// This will block until the provided context is closed.
err = srv.ServeHTTP(ctx, handler)
cancel()
if err != nil {
// Process the error
}
}
```
### Custom logger
The module provides a [Logger](server.go#L63) interface that you can implement and use for your gsrv server.
For example, [zap logger](https://github.com/uber-go/zap) already satisfies this interface:
```go
logger, _ := zap.NewProduction()
defer logger.Sync()
srv, err := gsrv.New("0.0.0.0:8080", gsrv.WithLogger(logger))
if err != nil {
// Process the error
}
```