service

package module
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2023 License: MIT Imports: 32 Imported by: 0

README

= Go microservice framework

image:https://img.shields.io/github/checks-status/itbasis/go-service/main[GitHub branch checks state]
image:https://img.shields.io/github/go-mod/go-version/itbasis/go-service[GitHub go.mod Go version]
image:https://img.shields.io/badge/godoc-reference-blue.svg[link=https://pkg.go.dev/github.com/itbasis/go-service]
image:https://img.shields.io/github/v/tag/itbasis/go-service[GitHub tag (with filter)]
https://codecov.io/gh/itbasis/go-service[image:https://codecov.io/gh/itbasis/go-service/graph/badge.svg?token=GCqCXxG0xL[codecov]]
https://goreportcard.com/report/github.com/itbasis/go-hashtag[image:https://goreportcard.com/badge/github.com/itbasis/go-hashtag[Go Report Card]]


Possibilities:

// suppress inspection "AsciiDocLinkResolve"
. Running a link:https://gin-gonic.com/[gin] based HTTP/REST server ( see link:config.go#L10[config] )
// suppress inspection "AsciiDocLinkResolve"
. Running a gRPC server with metrics for Prometheus (see link:config.go#L14[config] and link:https://github.com/grpc-ecosystem/go-grpc-middleware/blob/main/providers/prometheus/options.go[options])
. Working with the database via link:https://gorm.io/[GORM] with support for database migration via link:https://pressly.github.io/goose/[Goose] with the ability to specify migration files from the file system or via `embed.FS`
. Using the link:https://github.com/go-co-op/gocron[gocron] task scheduler
. Different log output format based on link:https://github.com/uber-go/zap[Zap], depending on the launch environment - launch in container or not
. Getting settings via link:https://github.com/caarlos0/env[environment] variables

== Configuration files

. link:config.go[]

. link:db/config.go[] - more details in the link:db/README.adoc[]

To read the configuration from the environment, it is recommended to use the method link:https://github.com/itbasis/go-core-utils/blob/main/env-reader.go[`ReadEnvConfig()`]

== TODO

see link:TODO.adoc[]

== Examples

.Minimum code configuration to run
[source,go]
----
func main() {
	_ := NewServiceWithEnvironment(context.Background(), zap.NewProductionConfig()).Run()
}
----

.Configuration with the transfer of migration scripts for database migration via Embedded FS
[source,go]
----
import "embed"

//go:embed folder/*.sql
var embedMigrations embed.FS

func main(){
	_ := NewServiceWithEnvironment(context.Background(), zap.NewProductionConfig()).
		InitDB(&embedMigrations).
		Run()
}
----

alternative:
[source,go]
----
import "embed"

//go:embed folder/*.sql
var embedMigrations embed.FS

func main(){
	srv := NewServiceWithEnvironment(context.Background(), zap.NewProductionConfig())
	gormDB := srv.GetGormWithEmbeddedMigrations(&embedMigrations)

	srv.Run()
}
----


.Specifying custom metrics for Prometheus for gRPC server
[source,go]
----
import grpcPrometheus "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"

func main() {
	_ := NewServiceWithEnvironment(context.Background(), zap.NewProductionConfig()).
		InitGrpcServerMetrics(grpcPrometheus.WithServerHandlingTimeHistogram()).
		Run()
}
----

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	ServiceName string `env:"SERVICE_NAME"`

	DbGormDisabled bool `env:"DB_GORM_DISABLED"`

	SchedulerEnabled bool `env:"SCHEDULER_ENABLED"`

	RestServerHost string `env:"REST_SERVER_HOST" envDefault:"0.0.0.0"`
	RestServerPort int    `env:"REST_SERVER_PORT" envDefault:"8080"`

	GrpcServerDisabled    bool   `env:"GRPC_SERVER_DISABLED"`
	GrpcServerHost        string `env:"GRPC_SERVER_HOST" envDefault:"0.0.0.0"`
	GrpcServerPort        int    `env:"GRPC_SERVER_PORT" envDefault:"9000"`
	GrpcReflectionEnabled bool   `env:"GRPC_REFLECTION_ENABLED" envDefault:"true"`
}

type HTTPController

type HTTPController struct {
	Method  string
	Path    string
	Handler gin.HandlerFunc
}

type Service

type Service struct {
	GrpcClientInterceptors []grpc.DialOption
	// contains filtered or unexported fields
}

func NewServiceWithConfig

func NewServiceWithConfig(ctx context.Context, zapConfig zap.Config, config *Config) *Service

func NewServiceWithEnvironment

func NewServiceWithEnvironment(ctx context.Context, zapConfig zap.Config) *Service

func (*Service) AddHTTPControllers

func (receiver *Service) AddHTTPControllers(httpControllers ...HTTPController)

func (*Service) GetClock

func (receiver *Service) GetClock() clock.Clock

func (*Service) GetConfig

func (receiver *Service) GetConfig() Config

func (*Service) GetGin

func (receiver *Service) GetGin() *gin.Engine

func (*Service) GetGorm

func (receiver *Service) GetGorm(ctx context.Context) *gorm.DB

func (*Service) GetGormWithEmbeddedMigrations

func (receiver *Service) GetGormWithEmbeddedMigrations(ctx context.Context, dbEmbedMigrations *embed.FS) *gorm.DB

func (*Service) GetGrpc deprecated

func (receiver *Service) GetGrpc() *grpc.Server

Deprecated: use GetGrpcServer

func (*Service) GetGrpcClientInterceptors

func (receiver *Service) GetGrpcClientInterceptors(authClientInterceptor *client.AuthClientInterceptor)

func (*Service) GetGrpcServer

func (receiver *Service) GetGrpcServer() *grpc.Server

func (*Service) GetGrpcServerMetrics

func (receiver *Service) GetGrpcServerMetrics() *grpcPrometheus.ServerMetrics

func (*Service) GetJwtToken

func (receiver *Service) GetJwtToken() jwtToken.JwtToken

func (*Service) GetScheduler

func (receiver *Service) GetScheduler() *gocron.Scheduler

func (*Service) InitDB

func (receiver *Service) InitDB(ctx context.Context, dbEmbedMigrations *embed.FS) *Service

func (*Service) InitGrpcServerMetrics

func (receiver *Service) InitGrpcServerMetrics(
	serverMetricsOptions []grpcPrometheus.ServerMetricsOption,
	promHTTPHandlerOpts *promhttp.HandlerOpts,
) *Service

func (*Service) Run

func (receiver *Service) Run()

Directories

Path Synopsis
utils

Jump to

Keyboard shortcuts

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