initializer

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2024 License: MIT Imports: 4 Imported by: 8

README

Initializer

Initializer is a simple library to initialize main go service.

go get github.com/worldline-go/initializer

This library customized to use https://github.com/rakunlabs/into

Usage

Default logger is zerolog

var (
	version = "v0.0.0"
	commit  = "-"
	date    = "-"
)

func main() {
	// run the application
	initializer.Init(
		run,
		initializer.WithMsgf("awesome-service version:[%s] commit:[%s] date:[%s]", version, commit, date),
		// if you want to close the application after a certain time without waiting the waitgroup
		// initializer.WithWaitTimeout(0),  // 0 means no timeout as default (time.Duration)
		initializer.WithOptionsLogz(
			logz.WithLevel(zerolog.LevelDebugValue),
		))
}

func run(_ context.Context) error {
	// Do something here.
	log.Info().Msg("Hello World!")

	return fmt.Errorf("something went wrong")
}

Add shutdown function, it will be called when the context is done.

initializer.ShutdownAdd(server.Close, "server")

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// WaitGroup returns global wait group.
	WaitGroup = into.WaitGroup
	// ShutdownAdd add function will be called when the context is done.
	ShutdownAdd = into.ShutdownAdd
	// CtxCancel is a function that cancels the root context.
	CtxCancel = into.CtxCancel
)

Functions

func Init

func Init(fn func(context.Context) error, options ...Option)

Init is a function that initializes the application.

This function will initialize the logger and run the shutdown function on exit.

Types

type Option added in v0.4.0

type Option func(options *option)

func WithMsgf

func WithMsgf(format string, a ...any) Option

WithMsg is a function that sets the message to be logged when the application starts.

This will override the default message.

func WithOptionsInto added in v0.4.0

func WithOptionsInto(intoOpts ...into.Option) Option

func WithOptionsLogz

func WithOptionsLogz(logzOpts ...logz.Option) Option

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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