indiescrobble/config/config.go

58 lines
1.3 KiB
Go
Raw Normal View History

2022-01-30 16:05:54 +00:00
package config
import (
"log"
"path/filepath"
"github.com/spf13/viper"
)
var config *viper.Viper
// Init is an exported method that takes the environment starts the viper
// (external lib) and returns the configuration struct.
func Init(env string) {
var err error
config = viper.New()
2022-02-05 16:24:07 +00:00
config.SetDefault("indieauth.clientName", "https://indiescrobble.club")
config.SetDefault("indieauth.redirectURL", "http://localhost:3000/auth")
config.SetDefault("indieauth.oauthSubject", "IndieScrobble OAuth Client")
config.SetDefault("indieauth.oauthCookieName","indiescrobble-oauth")
config.SetDefault("indieauth.sessionSubject", "IndieScrobble Session")
config.SetDefault("server.database.driver", "sqlite")
config.SetDefault("server.database.dsn", "indiescrobble.db")
2022-01-30 16:05:54 +00:00
config.SetConfigType("yaml")
config.SetConfigName(env)
config.AddConfigPath("../config/")
config.AddConfigPath("config/")
2022-02-05 16:24:07 +00:00
2022-01-30 16:05:54 +00:00
err = config.ReadInConfig()
if err != nil {
log.Fatal("error on parsing configuration file")
}
2022-02-05 16:24:07 +00:00
if config.GetString("jwt.signKey") == ""{
log.Fatal("You must set a JWT sign key (jwt.signKey in config yaml)")
}
config.BindEnv("server.port","PORT")
2022-01-30 16:05:54 +00:00
}
func relativePath(basedir string, path *string) {
p := *path
if len(p) > 0 && p[0] != '/' {
*path = filepath.Join(basedir, p)
}
}
func GetConfig() *viper.Viper {
return config
}