base

package module
v0.0.0-...-de12d2e Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2024 License: MIT Imports: 6 Imported by: 0

README

go-base

Base package provide base tools to every service

Documentation

Overview

Package base provides a way to initialize default resources that is used by almost all services. This package is used to load: - Context - Environment Variables - Logger - Time Resources - HTTP Client

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultHTTPClientTimeout is the default timeout for the HTTP Client.
	DefaultHTTPClientTimeout = 10 * time.Second

	// DefaultTimeLocation is the default time location.
	DefaultTimeLocation = "UTC"

	// DefaultLoggerLevel is the default logger level.
	DefaultLoggerLevel = zerolog.InfoLevel

	// DefaultLoggerOutput is the default logger output.
	DefaultLoggerOutput = &os.Stdout
)

Functions

This section is empty.

Types

type Baser

type Baser[envType any] interface {
	Ctx() context.Context
	CancelCtx() func()
	Env() envType
	Logger() *zerolog.Logger
	TimeLocation() *time.Location
	// TimeNow returns the current time in the time location.
	TimeNow() time.Time
	HTTPClient() *http.Client
}

Baser is the interface that wraps the basic methods to get the initialized resources.

func New

func New[envType any](cfg Config) Baser[envType]

New initialize default resources that is used by almost all services. - Context - Environment Variables - Logger - Time Resources - HTTP Client

To load the environment variables need to pass the envType as struct with the 'env' labels like the example below.

type envModel struct {
	Environment string `env:"ENVIRONMENT"`
	Version     string `env:"VERSION"`
	Database    string `env:"DATABASE"`
}

type Config

type Config struct {
	// If not provided, a default logger will be created with the level set to Info and output to os.Stdout.
	Logger *zerolog.Logger
	// If not provided, the default location will be used (UTC).
	TimeLocation string
	// If not provided, the default configuration will be used.
	HTTPClientConfig HTTPClientConfig
}

Config is used to configure the resources that will be initialized.

type HTTPClientConfig

type HTTPClientConfig struct {
	// If not provided, the default transport will be used (http.DefaultTransport).
	Transport http.RoundTripper
	Jar       http.CookieJar
	// If not provided, the default timeout will be used (10 seconds).
	Timeout time.Duration
}

Jump to

Keyboard shortcuts

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