feat: Complete env system

This commit is contained in:
2026-04-04 14:58:45 +03:00
parent 89f71ec7e7
commit 3a89106c1b
4 changed files with 61 additions and 0 deletions
View File
+8
View File
@@ -1,3 +1,11 @@
module github.com/lorsanstand/HomeOps-Hub
go 1.26.1
require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/ilyakaznacheev/cleanenv v1.5.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
)
+11
View File
@@ -0,0 +1,11 @@
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2lOrsHHvr4=
github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 h1:slmdOY3vp8a7KQbHkL+FLbvbkgMqmXojpFUO/jENuqQ=
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3/go.mod h1:oVgVk4OWVDi43qWBEyGhXgYxt7+ED4iYNpTngSLX2Iw=
+42
View File
@@ -0,0 +1,42 @@
package cfg
import (
"fmt"
"log"
"github.com/ilyakaznacheev/cleanenv"
)
type Config struct {
DBHost string `env:"DB_HOST" env-required:"true"`
DBPort int `env:"DB_PORT" env-required:"true"`
DBPassword string `env:"DB_PASS" env-required:"true"`
DBUser string `env:"DB_USER" env-required:"true"`
DBName string `env:"DB_NAME" env-required:"true"`
LogLevel string `env:"LOG_LEVEL" env-default:"INFO"`
Mode string `env:"MODE" env-default:"DEV"`
}
func NewConfig() (*Config, error) {
var cfg Config
if err := cleanenv.ReadConfig(".env", &cfg); err != nil {
log.Printf("failed read config: %v", err)
if err = cleanenv.ReadEnv(&cfg); err != nil {
return nil, fmt.Errorf("env read failed: %v", err)
}
}
return &cfg, nil
}
func (c *Config) GetURLPostgres() string {
return fmt.Sprintf(
"postgres://%v:%v@%v:%v/%v?sslmode=disable",
c.DBUser,
c.DBPassword,
c.DBHost,
c.DBPort,
c.DBName)
}