Capstone/ui/config/config.go

68 lines
1.5 KiB
Go
Raw Normal View History

2023-10-28 14:42:29 -04:00
package config
import (
"fmt"
"strings"
2023-10-28 14:42:29 -04:00
"github.com/spf13/viper"
)
type config struct {
Mongo *MongoConfig `mapstructure:"mongo"`
Vendors map[string]*VendorConfig `mapstructure:"vendors"`
JwtSecret string `mapstructure:"jwt_secret"`
Env string `mapstructure:"env"`
2023-10-28 14:42:29 -04:00
}
type MongoConfig struct {
2023-10-28 17:50:44 -04:00
Uri string `mapstructure:"uri"`
EntDb string `mapstructure:"ent_db"`
EntCol string `mapstructure:"ent_col"`
2023-10-28 14:42:29 -04:00
}
type VendorConfig struct {
ClientId string `mapstructure:"client_id"`
ClientSecret string `mapstructure:"client_secret"`
Scopes []string `mapstructure:"scopes"`
AuthUri string `mapstructure:"auth_uri"`
TokenUri string `mapstructure:"token_uri"`
RefreshEncode string `mapstructure:"refresh_encode"`
scope string
}
func (pco *VendorConfig) Scope() string {
if pco.scope == "" {
pco.scope = strings.Join(pco.Scopes, " ")
}
return pco.scope
}
2023-10-28 14:42:29 -04:00
var cfg *config
2023-10-28 17:50:44 -04:00
func Init() {
viper.SetConfigName("config") // name of config file (without extension)
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
2023-10-28 17:50:44 -04:00
viper.AddConfigPath("/etc/capstone") // path to look for the config file in
2023-10-28 14:42:29 -04:00
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
cfg = &config{}
err = viper.Unmarshal(cfg)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", cfg)
for key, value := range cfg.Vendors {
fmt.Printf("%s: %v\n", key, value)
}
2023-10-28 14:42:29 -04:00
}
func Config() *config {
return cfg
}