first commit
This commit is contained in:
commit
3d986c4ee2
27 changed files with 597 additions and 0 deletions
45
helpers/config.go
Normal file
45
helpers/config.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package helpers
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/golobby/dotenv"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Debug bool `env:"DEBUG"`
|
||||
App struct {
|
||||
ListenAddress string `env:"APP_LISTEN_ADDRESS"`
|
||||
ListenPort uint `env:"APP_LISTEN_PORT"`
|
||||
}
|
||||
Database struct {
|
||||
Location string `env:"DATABASE_LOCATION"`
|
||||
}
|
||||
}
|
||||
|
||||
var configParsed bool
|
||||
var config Config
|
||||
|
||||
func GetConfig() (Config, error) {
|
||||
if configParsed {
|
||||
return config, nil
|
||||
}
|
||||
|
||||
return parseConfig()
|
||||
}
|
||||
|
||||
func parseConfig() (Config, error) {
|
||||
file, err := os.Open(".env")
|
||||
if err != nil {
|
||||
return config, err
|
||||
}
|
||||
|
||||
err = dotenv.NewDecoder(file).Decode(&config)
|
||||
if err != nil {
|
||||
return config, err
|
||||
}
|
||||
|
||||
configParsed = true
|
||||
|
||||
return config, err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue