config

package module
v1.21.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: MIT Imports: 6 Imported by: 0

README

Config Package

Overview

The config package is a comprehensive and user-friendly configuration management solution for Go applications. It offers seamless integration of environment variables and .env file support, along with planned features for hot-reloading, multiple format support, and external system integration.

Features

  • Environment Variables: Leverage environment variables for configuration, with built-in default values.
  • .env File Support: Manage your configurations conveniently through a .env file.
  • Planned Features:
    • Hot-Reloading: Dynamically reload configurations without restarting the application.
    • Multiple Format Support: Extendable to support YAML, JSON, TOML formats.
    • External System Integration: Facilitate integration with systems like Consul, etcd for distributed configurations.
    • Validation: Validate configuration values to ensure reliability and correctness.

Installation

To install the package, execute the following command:

go get github.com/alekslesik/config

Usage

To use the package in your Go application, follow these steps:

  1. Import the package:

    import "github.com/alekslesik/config"
    
  2. Define a configuration structure:

    type AppConfig struct {
     Port int `env:"PORT" env-default:"8080"`
     // ... other configuration fields
     }
    
  3. Load the configuration:

    var cfg AppConfig
    err := config.Load(&cfg)
    if err != nil {
        log.Fatalf("Error loading config: %v", err)
    }
    

Documentation

Refer to the config documentation for detailed information on all functionalities of the package. Contributing

Contributions are welcome! If you wish to contribute to the project, please fork the repository and submit a pull request. For substantial changes, please open an issue first to discuss what you would like to change. License

This project is licensed under the MIT License, which permits both personal and commercial use, modification, distribution, and sublicensing.

Future Enhancements

Hot-reloading of configurations. Support for additional formats like YAML, JSON, TOML. Integration with external configuration management systems. Configuration validation.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEnvNotExists error = errors.New(".env file is not exists")
)

Functions

This section is empty.

Types

type AppConfig

type AppConfig struct {
	Port      int    `env:"PORT" env-default:"443"`
	Host      string `env:"HOST" env-default:"localhost"`
	Env       string `env:"ENV" env-default:"development"`
	AdminUser struct {
		Email    string `env:"ADMIN_EMAIL" env-default:"admin"`
		Password string `env:"ADMIN_PWD" env-default:"admin"`
	}
}

AppConfig is general config for application

type Config

type Config struct {
	App     AppConfig
	Logger  LoggerConfig
	MySQL   MySQLConfig
	Session SessionConfig
	TLS     TLSConfig
	SMTP    SMTPConfig
}

Config fo application

func New

func New() (*Config, error)

New return instance of config (singleton)

type LoggerConfig

type LoggerConfig struct {
	LogLevel    string `env:"LOG_LEVEL" env-default:"development"`
	LogFilePath string `env:"LOG_FILE" env-default:"./tmp/log.log"`
	MaxSize     int    `env:"LOG_MAXSIZE" env-default:"100"`
	MaxBackups  int    `env:"LOG_MAXBACKUP" env-default:"3"`
	MaxAge      int    `env:"LOG_MAXAGE" env-default:"24"`
	Compress    bool   `env:"LOG_COMPRESS" env-default:"true"`
}

LoggerConfig is config for logging part

type MySQLConfig

type MySQLConfig struct {
	Driver string `env:"MYSQL_DRIVER" env-default:"mysql"`
	DSN    string `env:"MYSQL_DSN" env-default:"root:486464@tcp(localhost:3306)/neuronews?parseTime=true"`
}

MySQLConfig is config fo MySQL part

type SMTPConfig

type SMTPConfig struct {
	Host     string `env:"SMTP_HOST"`
	Port     int    `env:"SMTP_PORT"`
	Username string `env:"SMTP_USERNAME"`
	Password string `env:"SMTP_PASSWORD"`
	Sender   string `env:"SMTP_SENDER"`
}

SMTPConfig is config for SMTP part

type SessionConfig

type SessionConfig struct {
	Secret string `env:"SESSION_SECRET" env-default:"s6Ndh+pPbnzHbS*+9Pk8qGWhTzbpa@ge"`
}

SessionConfig is config for session part

type TLSConfig

type TLSConfig struct {
	KeyPath  string `env:"TLS_KEY_PATH" env-default:"./tls/key.pem"`
	CertPath string `env:"TLS_CERT_PATH" env-default:"./tls/cert.pem"`
}

TLSConfig is config for TLS part

Jump to

Keyboard shortcuts

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