2023-10-28 14:42:29 -04:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2023-11-03 01:01:33 -04:00
|
|
|
"strings"
|
|
|
|
|
2023-10-28 14:42:29 -04:00
|
|
|
"github.com/spf13/viper"
|
2023-11-07 22:34:57 -05:00
|
|
|
"golang.org/x/oauth2"
|
2023-10-28 14:42:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type config struct {
|
2023-11-18 21:15:15 -05:00
|
|
|
Mongo *MongoConfig `mapstructure:"mongo"`
|
|
|
|
Vendors map[string]*VendorConfig `mapstructure:"vendors"`
|
|
|
|
JwtSecret string `mapstructure:"jwt_secret"`
|
|
|
|
Env string `mapstructure:"env"`
|
|
|
|
AppSettings *AppSettings `mapstructure:"app_settings"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type AppSettings struct {
|
|
|
|
WebhookServiceUrl string `mapstructure:"webhook_service_url"`
|
|
|
|
FrontendServiceUrl string `mapstructure:"frontend_service_url"`
|
2023-10-28 14:42:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type MongoConfig struct {
|
2023-11-16 20:45:48 -05:00
|
|
|
Uri string `mapstructure:"uri"`
|
|
|
|
EntDb string `mapstructure:"ent_db"`
|
|
|
|
EntCol string `mapstructure:"ent_col"`
|
|
|
|
LockDb string `mapstructure:"lock_db"`
|
2023-11-07 22:34:57 -05:00
|
|
|
LockCol string `mapstructure:"lock_col"`
|
2023-10-28 14:42:29 -04:00
|
|
|
}
|
|
|
|
|
2023-11-04 18:52:41 -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"`
|
2023-11-07 22:34:57 -05:00
|
|
|
WebhookSecret string `mapstructure:"webhook_secret"`
|
2023-11-04 18:52:41 -04:00
|
|
|
scope string
|
2023-11-03 01:01:33 -04:00
|
|
|
}
|
|
|
|
|
2023-11-07 22:34:57 -05:00
|
|
|
func (vendor *VendorConfig) Scope() string {
|
|
|
|
if vendor.scope == "" {
|
|
|
|
vendor.scope = strings.Join(vendor.Scopes, " ")
|
|
|
|
}
|
|
|
|
return vendor.scope
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vendor *VendorConfig) OauthConfig() *oauth2.Config {
|
|
|
|
return &oauth2.Config{
|
|
|
|
ClientID: vendor.ClientId,
|
|
|
|
ClientSecret: vendor.ClientSecret,
|
2023-11-16 20:45:48 -05:00
|
|
|
Endpoint: oauth2.Endpoint{
|
|
|
|
AuthURL: vendor.AuthUri,
|
|
|
|
TokenURL: vendor.TokenUri,
|
|
|
|
AuthStyle: oauth2.AuthStyleInParams,
|
2023-11-07 22:34:57 -05:00
|
|
|
},
|
2023-11-16 20:45:48 -05:00
|
|
|
RedirectURL: "",
|
|
|
|
Scopes: vendor.Scopes,
|
2023-11-03 01:01:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-28 14:42:29 -04:00
|
|
|
var cfg *config
|
|
|
|
|
2023-10-28 17:50:44 -04:00
|
|
|
func Init() {
|
2023-11-01 22:40:50 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Config() *config {
|
|
|
|
return cfg
|
|
|
|
}
|