service

package module
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2023 License: MIT Imports: 31 Imported by: 0

README

= Go microservice framework

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/rs/zerolog[Zerolog], 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().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().
		InitDB(&embedMigrations).
		Run()
}
----

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

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

func main(){
	srv := NewServiceWithEnvironment()
	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().
		InitGrpcServerMetrics(grpcPrometheus.WithServerHandlingTimeHistogram()).
		Run()
}
----

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ReturnFalse = wrapperspb.Bool(false)
	ReturnTrue  = wrapperspb.Bool(true)
)

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 added in v0.0.9

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

type RestController deprecated

type RestController = HttpController

Deprecated: use HttpController

type Service

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

func NewServiceWithConfig

func NewServiceWithConfig(config *Config) *Service

func NewServiceWithEnvironment

func NewServiceWithEnvironment() *Service

func (*Service) AddHttpControllers added in v0.0.9

func (receiver *Service) AddHttpControllers(httpControllers ...HttpController)

func (*Service) AddRestControllers deprecated

func (receiver *Service) AddRestControllers(restControllers ...RestController)

Deprecated: use AddHttpControllers

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() *gorm.DB

func (*Service) GetGormWithEmbeddedMigrations

func (receiver *Service) GetGormWithEmbeddedMigrations(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(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