server

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2021 License: MIT Imports: 8 Imported by: 1

README

server

A golang gin-gonic compatible server, supporting safe shutdowns with health checks.

Usage

import (
  "context"
	"github.com/fulgurant/health"
	"github.com/fulgurant/server"
	"go.uber.org/zap"
)

func main() {
	logger, _ := zap.NewProduction()
	h := health.New(logger)

	svrOptions := server.
		DefaultOptions().
		WithListenAddress(":8080").
		WithHealth(h).
		WithLogger(logger).
		WithWarningDuration(time.Second * 5)
		WithShutdownDuration(time.Second * 5)

	svr, err := server.New(svrOptions)
    if err != nil {
        panic(err)
    }

    defer svr.Close()

    h.RegisterEndpoint(svr)

    svr.GET("Hello", func (ctx *gin.Context) {
        ctx.String("200", "Hello")
    })

    // Wait until app must end
}

Notes

On shutdown, this server first starts failing health checks, then stops accepting new connections but waits for existing connections to safely terminate, and finally closes any existing connections forcefully.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	ListenAddress    string        `kong:"default=':8080',help='Address on which the service will run',short='l',env='LISTEN_ADDR'"`
	ShutdownDuration time.Duration `kong:"default='1s',help='Duration to take to allow existing connections to complete',short='s'"`
	WarningDuration  time.Duration `kong:"default='1s',help='Duration to take to allow routing algorithms to reroute',short='w'"`
}

Config values for the server

type IServer

type IServer interface {
	gin.IRouter
	io.Closer
}

type Options

type Options struct {
	// contains filtered or unexported fields
}

func DefaultOptions

func DefaultOptions() *Options

func (*Options) WithConfig

func (o *Options) WithConfig(value *Config) *Options

WithConfig sets up stuff from the config

func (*Options) WithHealth

func (o *Options) WithHealth(value health.IHealth) *Options

WithHealth sets the health checker used to allow health checks to fail before actually refusing to accept new connections

func (*Options) WithLogger

func (o *Options) WithLogger(value *zap.Logger) *Options

WithLogger sets the logger for the server

type Server

type Server struct {
	*gin.Engine
	// contains filtered or unexported fields
}

func New

func New(options *Options) (svr *Server, err error)

func (*Server) Close

func (svr *Server) Close() error

Jump to

Keyboard shortcuts

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