package util import ( "errors" "os" "strings" "github.com/joho/godotenv" ) func LoadConfig() (*Config, error) { config := Config{} config.def() err := config.loadEnv() if err != nil { return nil, err } // other sources? config.verify() return &config, nil } type Config struct { ClientID string ClientSecret string RedirectURI string SQliteDB string WSAuthorization string } func (c *Config) def() { c.SQliteDB = "./db.sqlite" } func (c *Config) loadEnv() error { err := godotenv.Load() if err != nil { return err } if str, found := os.LookupEnv("CLIENT_ID"); found { c.ClientID = strings.TrimSpace(str) } if str, found := os.LookupEnv("CLIENT_SECRET"); found { c.ClientSecret = strings.TrimSpace(str) } if str, found := os.LookupEnv("REDIR_URI"); found { c.RedirectURI = strings.TrimSpace(str) } if str, found := os.LookupEnv("SQLITE_DB"); found { c.SQliteDB = strings.TrimSpace(str) } if str, found := os.LookupEnv("WS_AUTHORIZATION"); found { c.WSAuthorization = strings.TrimSpace(str) } return nil } func (c *Config) verify() error { if c.ClientID == "" { return errors.New("unable to load a configured Client ID") } if c.ClientSecret == "" { return errors.New("unable to load a configured Client Secret") } if c.RedirectURI == "" { return errors.New("unable to load a configured Redirect URI") } if c.WSAuthorization == "" { return errors.New("unable to load a configured WS Authorization code") } return nil }