server

package module
v2.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 1, 2021 License: MIT Imports: 9 Imported by: 0

README

go-srv

Interface for implementing servers in go with graceful shutdown on OS signals.

Example server implementations

gin-gonic/zerolog/gorm
import (
	"context"
	"fmt"
	"net/http"
	"os"

	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	"github.com/rs/zerolog"

	"golang.org/x/sync/errgroup"
	server "github.com/balazshorvath/go-srv"
)

const defaultConfig = "config.yaml"

type httpServer struct {
	server.BasicHttpServer

	logger *zerolog.Logger
	config *config
	db     *gorm.DB
}

func New(ctx context.Context, group *errgroup.Group) server.Server {
	path := os.Getenv("CONFIG_PATH")
	if path == "" {
		path = defaultConfig
	}
	config := parseConfig(path)
	return &httpServer{
		BasicHttpServer: server.BasicHttpServer{
			BasicServer: server.BasicServer{
				Ctx:   ctx,
				Group: group,
			},
		},
		config: config,
	}
}

func (h *httpServer) Init() {
	router := gin.Default()
	h.Srv = &http.Server{
		Addr:    fmt.Sprintf("%s:%s", h.config.server.Host, h.config.server.Port),
		Handler: router,
	}
	// Init dependencies
	h.initDatabase(&h.config.database)
	// Setup http
	router.GET("/api/path", handlePath(h.db))
}

func handlePath() gin.HandlerFunc {
	return func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"hello": "there",
		})
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var OsExitError = errors.New("os exit signal")

Functions

func CreateAndRunServer

func CreateAndRunServer(constructor Constructor, gracefulTimeout time.Duration) (err error)

CreateAndRunServer creates, initializes and starts a Server. Also makes sure that all the subroutines are finished before exiting.

Types

type BasicHttpServer

type BasicHttpServer struct {
	BasicServer
	Srv *http.Server
}

BasicHttpServer implements the Start() and Shutdown() functions of the Server interface. Implement Init() to initialize the routing and the constructor to instantiate dependencies.

func (*BasicHttpServer) Shutdown

func (b *BasicHttpServer) Shutdown(ctx context.Context) error

func (*BasicHttpServer) Start

func (b *BasicHttpServer) Start()

type BasicServer

type BasicServer struct {
	Ctx   context.Context
	Group *errgroup.Group
}

func (*BasicServer) Init added in v2.0.2

func (b *BasicServer) Init()

type Constructor

type Constructor func(ctx context.Context, group *errgroup.Group) Server

type Server

type Server interface {
	Init()
	Start()
	Shutdown(ctx context.Context) error
}

Server is an interface for implementing concurrent servers. CreateAndRunServer() implements a graceful shutdown waiting for all the resources to close.

It calls Init() and Start() on the server.

Use BasicServer.Group to start goroutines When implementing an http server, use BasicHttpServer struct.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL