Categorygithub.com/balazshorvath/go-srv/v2
package
2.0.4
Repository: https://github.com/balazshorvath/go-srv.git
Documentation: pkg.go.dev

# 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",
		})
	}
}