infra

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package infra contains the lowest-level resources needed by the microservice: configuration, the IoC container, lifecycle management, and logging setup.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogger

func NewLogger(cfg *Config) zerolog.Logger

NewLogger builds a zerolog.Logger from config settings.

func WaitForShutdown

func WaitForShutdown(ctx context.Context)

WaitForShutdown blocks until an OS interrupt/term signal is received or the given context is cancelled. The caller is responsible for any cleanup after this function returns.

Types

type Config

type Config struct {
	// HTTP server
	ServerPort   int           `env:"SERVER_PORT"        envDefault:"8080"`
	LogLevel     string        `env:"LOG_LEVEL"          envDefault:"info"`
	LogFormat    string        `env:"LOG_FORMAT"          envDefault:"json"`
	ReadTimeout  time.Duration `env:"HTTP_READ_TIMEOUT"  envDefault:"15s"`
	WriteTimeout time.Duration `env:"HTTP_WRITE_TIMEOUT" envDefault:"15s"`
	IdleTimeout  time.Duration `env:"HTTP_IDLE_TIMEOUT"  envDefault:"60s"`

	// Database connection
	DatabaseURL string `env:"DATABASE_URL" hush:"mask"`

	// pgx connection pool tuning - all optional with production-ready defaults.
	DBMaxConns          int           `env:"DB_MAX_CONNS"           envDefault:"25"`
	DBMinConns          int           `env:"DB_MIN_CONNS"           envDefault:"2"`
	DBMaxConnLifetime   time.Duration `env:"DB_MAX_CONN_LIFETIME"   envDefault:"30m"`
	DBMaxConnIdleTime   time.Duration `env:"DB_MAX_CONN_IDLE_TIME"  envDefault:"5m"`
	DBHealthCheckPeriod time.Duration `env:"DB_HEALTH_CHECK_PERIOD" envDefault:"1m"`

	// Cleanup worker - all optional; zero values disable the feature.
	CleanupInterval     time.Duration `env:"CLEANUP_INTERVAL"      envDefault:"1h"`
	CleanupMaxAge       time.Duration `env:"CLEANUP_MAX_AGE"       envDefault:"0"`
	CleanupKeepPerImage int           `env:"CLEANUP_KEEP_PER_IMAGE" envDefault:"0"`
}

Config holds all runtime configuration loaded from environment variables.

Struct tags:

  • env : environment variable name
  • envDefault : fallback value when the variable is absent
  • hush : "mask" or "hide" controls the printed config table

func Load

func Load() (*Config, error)

Load reads all settings from environment variables via goconf and returns a validated *Config. On success goconf also prints a masked config table to stdout.

func (*Config) Print

func (c *Config) Print() interface{}

Print implements goconf.Printer — returns the struct for the config table display.

func (*Config) Register

func (c *Config) Register() error

Register implements goconf.Configer — parses environment variables into Config.

func (*Config) ServerAddress

func (c *Config) ServerAddress() string

ServerAddress returns the formatted ":port" string for the HTTP server.

func (*Config) Validate

func (c *Config) Validate() error

Validate implements goconf.Validater — enforces required fields and sane constraints.

type Container

type Container struct {
	Config *Config
	Logger zerolog.Logger

	ScanRepo      *postgres.ScanRepository
	ScanUseCases  *usecases.ScanUseCases
	CleanupWorker *usecases.Cleaner
	// contains filtered or unexported fields
}

Container is a simple implementation of a dependency inversion container.

Dependencies are resolved manually — there is no fancy DI framework. This makes startup explicit, testable, and easy to trace.

func NewResolvedContainer

func NewResolvedContainer(ctx context.Context, cfg *Config) (*Container, error)

NewResolvedContainer builds and wires all dependencies, returning a ready-to-use Container. Call Destroy() when shutting down to release resources.

func (*Container) Destroy

func (c *Container) Destroy()

Destroy releases all resources held by the container (database pool, etc.). Safe to call multiple times.

Jump to

Keyboard shortcuts

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