Skip to content

Commit

Permalink
Merge pull request #139 from containerish/read-config-from-env
Browse files Browse the repository at this point in the history
Add: read configuration from environment variable
  • Loading branch information
guacamole authored Apr 13, 2022
2 parents 791f26e + 3505b20 commit bd154f8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 53 deletions.
96 changes: 48 additions & 48 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,81 +10,81 @@ import (

type (
OpenRegistryConfig struct {
Registry *Registry `mapstructure:"registry"`
StoreConfig *Store `mapstructure:"database"`
AuthConfig *Auth `mapstructure:"auth"`
LogConfig *Log `mapstructure:"log_service"`
SkynetConfig *Skynet `mapstructure:"skynet"`
OAuth *OAuth `mapstructure:"oauth"`
Email *Email `mapstructure:"email"`
Environment string `mapstructure:"environment"`
WebAppEndpoint string `mapstructure:"web_app_url"`
WebAppRedirectURL string `mapstructure:"web_app_redirect_url"`
WebAppErrorRedirectPath string `mapstructure:"web_app_error_redirect_path"`
Debug bool `mapstructure:"debug"`
Registry *Registry `yaml:"registry" mapstructure:"registry"`
StoreConfig *Store `yaml:"database" mapstructure:"database"`
AuthConfig *Auth `yaml:"auth" mapstructure:"auth"`
LogConfig *Log `yaml:"log_service" mapstructure:"log_service"`
SkynetConfig *Skynet `yaml:"skynet" mapstructure:"skynet"`
OAuth *OAuth `yaml:"oauth" mapstructure:"oauth"`
Email *Email `yaml:"email" mapstructure:"email"`
Environment string `yaml:"environment" mapstructure:"environment"`
WebAppEndpoint string `yaml:"web_app_url" mapstructure:"web_app_url"`
WebAppRedirectURL string `yaml:"web_app_redirect_url" mapstructure:"web_app_redirect_url"`
WebAppErrorRedirectPath string `yaml:"web_app_error_redirect_path" mapstructure:"web_app_error_redirect_path"`
Debug bool `yaml:"debug" mapstructure:"debug"`
}

Registry struct {
TLS TLS `mapstructure:"tls"`
DNSAddress string `mapstructure:"dns_address"`
FQDN string `mapstructure:"fqdn"`
SigningSecret string `mapstructure:"jwt_signing_secret"`
Host string `mapstructure:"host"`
Services []string `mapstructure:"services"`
Port uint `mapstructure:"port"`
TLS TLS `yaml:"tls" mapstructure:"tls"`
DNSAddress string `yaml:"dns_address" mapstructure:"dns_address"`
FQDN string `yaml:"fqdn" mapstructure:"fqdn"`
SigningSecret string `yaml:"jwt_signing_secret" mapstructure:"jwt_signing_secret"`
Host string `yaml:"host" mapstructure:"host"`
Services []string `yaml:"services" mapstructure:"services"`
Port uint `yaml:"port" mapstructure:"port"`
}

TLS struct {
PrivateKey string `mapstructure:"priv_key"`
PubKey string `mapstructure:"pub_key"`
PrivateKey string `yaml:"priv_key" mapstructure:"priv_key"`
PubKey string `yaml:"pub_key" mapstructure:"pub_key"`
}

Auth struct {
SupportedServices map[string]bool `mapstructure:"supported_services"`
SupportedServices map[string]bool `yaml:"supported_services" mapstructure:"supported_services"`
}

Skynet struct {
SkynetPortalURL string `mapstructure:"portal_url"`
EndpointPath string `mapstructure:"endpoint_path"`
ApiKey string `mapstructure:"api_key"`
CustomUserAgent string `mapstructure:"custom_user_agent"`
CustomCookie string `mapstructure:"custom_cookie"`
SkynetPortalURL string `yaml:"portal_url" mapstructure:"portal_url"`
EndpointPath string `yaml:"endpoint_path" mapstructure:"endpoint_path"`
ApiKey string `yaml:"api_key" mapstructure:"api_key"`
CustomUserAgent string `yaml:"custom_user_agent" mapstructure:"custom_user_agent"`
CustomCookie string `yaml:"custom_cookie" mapstructure:"custom_cookie"`
}

Log struct {
Service string `mapstructure:"name"`
Endpoint string `mapstructure:"endpoint"`
AuthMethod string `mapstructure:"auth_method"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
Service string `yaml:"name" mapstructure:"name"`
Endpoint string `yaml:"endpoint" mapstructure:"endpoint"`
AuthMethod string `yaml:"auth_method" mapstructure:"auth_method"`
Username string `yaml:"username" mapstructure:"username"`
Password string `yaml:"password" mapstructure:"password"`
}

Store struct {
Kind string `mapstructure:"kind"`
User string `mapstructure:"username"`
Host string `mapstructure:"host"`
Password string `mapstructure:"password"`
Database string `mapstructure:"name"`
Port int `mapstructure:"port"`
Kind string `yaml:"kind" mapstructure:"kind"`
User string `yaml:"username" mapstructure:"username"`
Host string `yaml:"host" mapstructure:"host"`
Password string `yaml:"password" mapstructure:"password"`
Database string `yaml:"name" mapstructure:"name"`
Port int `yaml:"port" mapstructure:"port"`
}

GithubOAuth struct {
Provider string `mapstructure:"provider"`
ClientID string `mapstructure:"client_id"`
ClientSecret string `mapstructure:"client_secret"`
Provider string `yaml:"provider" mapstructure:"provider"`
ClientID string `yaml:"client_id" mapstructure:"client_id"`
ClientSecret string `yaml:"client_secret" mapstructure:"client_secret"`
}

OAuth struct {
Github GithubOAuth `mapstructure:"github"`
Github GithubOAuth `yaml:"github" mapstructure:"github"`
}

Email struct {
ApiKey string `mapstructure:"api_key"`
SendAs string `mapstructure:"send_as"`
VerifyEmailTemplateId string `mapstructure:"verify_template_id"`
ForgotPasswordTemplateId string `mapstructure:"forgot_password_template_id"`
WelcomeEmailTemplateId string `mapstructure:"welcome_template_id"`
Enabled bool `mapstructure:"enabled"`
ApiKey string `yaml:"api_key" mapstructure:"api_key"`
SendAs string `yaml:"send_as" mapstructure:"send_as"`
VerifyEmailTemplateId string `yaml:"verify_template_id" mapstructure:"verify_template_id"`
ForgotPasswordTemplateId string `yaml:"forgot_password_template_id" mapstructure:"forgot_password_template_id"`
WelcomeEmailTemplateId string `yaml:"welcome_template_id" mapstructure:"welcome_template_id"`
Enabled bool `yaml:"enabled" mapstructure:"enabled"`
}
)

Expand Down
21 changes: 19 additions & 2 deletions config/yaml.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
package config

import "github.com/spf13/viper"
import (
"os"

"github.com/fatih/color"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)

func ReadYamlConfig() (*OpenRegistryConfig, error) {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/.openregistry")

var registryConfig OpenRegistryConfig
// OPENREGISTRY_CONFIG env variable takes precedence over everything
if yamlConfigInEnv := os.Getenv("OPENREGISTRY_CONFIG"); yamlConfigInEnv != "" {
err := yaml.Unmarshal([]byte(yamlConfigInEnv), &registryConfig)
if err != nil {
return nil, err
}

color.Green("read configuration from environment variable")
return &registryConfig, nil
}

if err := viper.ReadInConfig(); err != nil {
return nil, err
}

var registryConfig OpenRegistryConfig
if err := viper.Unmarshal(&registryConfig); err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/google/go-github/v42 v42.0.0
github.com/google/uuid v1.3.0
github.com/hashicorp/go-multierror v1.0.0
github.com/jackc/pgx/v4 v4.13.0
github.com/labstack/echo-contrib v0.11.0
github.com/labstack/echo/v4 v4.5.0
github.com/rs/zerolog v1.24.0
Expand All @@ -18,6 +19,8 @@ require (
github.com/valyala/fasttemplate v1.2.1
github.com/whyrusleeping/tar-utils v0.0.0-20201201191210-20a61371de5b
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
gopkg.in/yaml.v2 v2.4.0
)

require (
Expand All @@ -37,7 +40,6 @@ require (
github.com/jackc/pgproto3/v2 v2.1.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.8.1 // indirect
github.com/jackc/pgx/v4 v4.13.0
github.com/jackc/puddle v1.1.3 // indirect
github.com/labstack/gommon v0.3.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
Expand All @@ -60,14 +62,12 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
gitlab.com/NebulousLabs/errors v0.0.0-20171229012116-7ead97ef90b8 // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

replace github.com/SkynetLabs/go-skynet/v2 => github.com/containerish/go-skynet/v2 v2.0.2-0.20220411175612-3c3d850b3a0c

0 comments on commit bd154f8

Please sign in to comment.