42 lines
763 B
Go
42 lines
763 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
|
||
|
"github.com/joho/godotenv"
|
||
|
"github.com/kelseyhightower/envconfig"
|
||
|
)
|
||
|
|
||
|
type Config struct {
|
||
|
PgHost string `envconfig:"PG_HOST"`
|
||
|
PgName string `envconfig:"PG_NAME"`
|
||
|
PgUser string `envconfig:"PG_USER"`
|
||
|
PgPass string `envconfig:"PG_PASS"`
|
||
|
PgPort int `envconfig:"PG_PORT"`
|
||
|
Listen string `envconfig:"LISTEN"`
|
||
|
Secret string `envconfig:"SECRET"`
|
||
|
}
|
||
|
|
||
|
func (c *Config) DSN() string {
|
||
|
return fmt.Sprintf(
|
||
|
"host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Europe/Moscow",
|
||
|
c.PgHost,
|
||
|
c.PgUser,
|
||
|
c.PgPass,
|
||
|
c.PgName,
|
||
|
c.PgPort,
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func GetConfig() (*Config, error) {
|
||
|
c := new(Config)
|
||
|
|
||
|
err := godotenv.Load()
|
||
|
if err != nil {
|
||
|
log.Println("not .env")
|
||
|
}
|
||
|
|
||
|
return c, envconfig.Process("", c)
|
||
|
}
|