Skip to content
/ gsrv Public

Wrapper for starting and maintaining HTTP and gRPC server

License

Notifications You must be signed in to change notification settings

dlampsi/gsrv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gsrv

Go Reference

A useful wrapper for starting and maintaining HTTP and gRPC servers with graceful shutdown on the root context cancel.

Usage

Basic usage:

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 interface that you can implement and use for your gsrv server.

For example, zap logger already satisfies this interface:

logger, _ := zap.NewProduction()
defer logger.Sync()

srv, err := gsrv.New("0.0.0.0:8080", gsrv.WithLogger(logger))
if err != nil {
	// Process the error 
}