init project and add config
This commit is contained in:
commit
358b89da58
|
@ -0,0 +1,12 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"recipes/internal/config"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// load config
|
||||
cfg := config.MustLoad()
|
||||
fmt.Printf("%+v", cfg)
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
env: "local" # local / dev / prod
|
||||
|
||||
http-server:
|
||||
address: "localhost:4444"
|
||||
timeout: 4s
|
||||
idle_timeout: 60s
|
||||
|
||||
postgresql:
|
||||
db_name: "user"
|
||||
user: "user"
|
||||
password: "password"
|
||||
address: "127.0.0.1:5432"
|
||||
|
||||
redis:
|
||||
user: "user"
|
||||
password: "password"
|
||||
address: "127.0.0.1:6379"
|
||||
|
||||
minio:
|
||||
user: "user"
|
||||
password: "password"
|
||||
address: "127.0.0.1:9000"
|
|
@ -0,0 +1,11 @@
|
|||
module recipes
|
||||
|
||||
go 1.21.5
|
||||
|
||||
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
|
||||
)
|
|
@ -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=
|
|
@ -0,0 +1,66 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/ilyakaznacheev/cleanenv"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Env string `yaml:"env" env-required:"true"`
|
||||
HTTPServer struct {
|
||||
Address string `yaml:"address"`
|
||||
Timeout time.Duration `yaml:"timeout"`
|
||||
IdleTimeout time.Duration `yaml:"idle_timeout"`
|
||||
} `yaml:"http-server"`
|
||||
Postgresql struct {
|
||||
DBName string `yaml:"db_name"`
|
||||
User string `yaml:"user"`
|
||||
Password string `yaml:"password"`
|
||||
Address string `yaml:"address"`
|
||||
} `yaml:"postgresql"`
|
||||
Redis struct {
|
||||
User string `yaml:"user"`
|
||||
Password string `yaml:"password"`
|
||||
Address string `yaml:"address"`
|
||||
} `yaml:"redis"`
|
||||
Minio struct {
|
||||
User string `yaml:"user"`
|
||||
Password string `yaml:"password"`
|
||||
Address string `yaml:"address"`
|
||||
} `yaml:"minio"`
|
||||
}
|
||||
|
||||
func MustLoad() *Config {
|
||||
configPath := fetchConfigPath()
|
||||
if configPath == "" {
|
||||
panic("CONFIG_PATH is not set")
|
||||
}
|
||||
// check if file exists
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
panic(fmt.Sprintf("config file does not exist: %s", configPath))
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
// read config
|
||||
if err := cleanenv.ReadConfig(configPath, &cfg); err != nil {
|
||||
panic(fmt.Errorf("cannot read config: %w", err))
|
||||
}
|
||||
|
||||
return &cfg
|
||||
}
|
||||
|
||||
// fetchConfigPath returns config path from env or flag.
|
||||
func fetchConfigPath() string {
|
||||
var res string = os.Getenv("CONFIG_PATH")
|
||||
|
||||
if res == "" {
|
||||
flag.StringVar(&res, "config", "", "path to config file")
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
Loading…
Reference in New Issue