Skip to content

tinh-tinh/config

Repository files navigation

Config Module for Tinh Tinh

Tinh Tinh Logo

Install

go get -u github.com/tinh-tinh/config/v2

Overview

The Config module for the Tinh Tinh framework provides flexible configuration management for Go applications. It supports loading environment variables from .env files and struct-based configs from YAML, with first-class integration for dependency injection and modular apps.

Features

  • Load configuration from .env, .yaml, or .yml files
  • Strongly-typed config structs with tags for mapping, default values, and validation
  • Namespace-based config injection for multiple configs (e.g., database, cache)
  • Conditional module registration based on environment or custom logic
  • Supports default values and validation via struct tags
  • Seamless integration with Tinh Tinh modules and controllers

Quick Start

1. Basic Usage with .env

import "github.com/tinh-tinh/config/v2"

type Config struct {
    NodeEnv   string        `env:"NODE_ENV"`
    Port      int           `env:"PORT"`
    ExpiresIn time.Duration `env:"EXPIRES_IN"`
    Log       bool          `env:"LOG"`
    Secret    string        `env:"SECRET"`
}

cfg, err := config.NewEnv[Config](".env")
if err != nil {
    panic(err)
}
fmt.Println(cfg.Port)

2. Using with YAML

type YamlConfig struct {
    Host string `yaml:"host"`
    Port int    `yaml:"port"`
}

cfg, err := config.NewYaml[YamlConfig]("config.yaml")
if err != nil {
    panic(err)
}
fmt.Println(cfg.Host)

Tinh Tinh Module Integration

Register as a Global Config

import "github.com/tinh-tinh/tinhtinh/v2/core"

appModule := core.NewModule(core.NewModuleOptions{
    Imports: []core.Modules{
        config.ForRoot[AppConfig](".env"),
    },
})

Inject Config Anywhere

cfg := config.Inject[AppConfig](module)
fmt.Println(cfg.Port)

Contributing

We welcome contributions! Please feel free to submit a Pull Request.

Support

If you encounter any issues or need help, you can:

  • Open an issue in the GitHub repository
  • Check our documentation
  • Join our community discussions

About

⚙️ Config Module for Tinh Tinh framwork

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors