2024-02-15 22:47:56 +00:00
|
|
|
package config
|
2024-02-11 19:28:00 +00:00
|
|
|
|
|
|
|
import (
|
2024-02-15 17:43:35 +00:00
|
|
|
"log"
|
2024-02-11 19:28:00 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2024-02-15 17:43:35 +00:00
|
|
|
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
"github.com/spf13/viper"
|
2024-02-11 19:28:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2024-02-15 17:43:35 +00:00
|
|
|
Port string `validate:"required"`
|
|
|
|
RootUrl string `validate:"required,url"`
|
|
|
|
DatabasePath string `validate:"required"`
|
|
|
|
SessionSecret string `validate:"required"`
|
2024-02-11 19:28:00 +00:00
|
|
|
|
2024-02-18 21:37:17 +00:00
|
|
|
Jwt Jwt `validate:"required"`
|
2024-02-15 17:43:35 +00:00
|
|
|
OAuth2 OAuth2 `validate:"required"`
|
2024-02-11 19:28:00 +00:00
|
|
|
|
2024-02-15 17:43:35 +00:00
|
|
|
Temporal Temporal `validate:"required"`
|
2024-02-13 20:52:10 +00:00
|
|
|
|
2024-02-18 21:37:17 +00:00
|
|
|
Worker Worker `validate:"required"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Jwt struct {
|
|
|
|
PrivateKey string `validate:"required"`
|
|
|
|
PublicKey string `validate:"required"`
|
2024-02-15 17:43:35 +00:00
|
|
|
}
|
2024-02-13 20:52:10 +00:00
|
|
|
|
2024-02-15 17:43:35 +00:00
|
|
|
type OAuth2 struct {
|
|
|
|
ClientID string `validate:"required"`
|
|
|
|
ClientSecret string `validate:"required"`
|
|
|
|
Scopes []string `validate:"required"`
|
|
|
|
EndpointTokenURL string `validate:"required"`
|
|
|
|
EndpointAuthURL string `validate:"required"`
|
|
|
|
EndpointUserInfoURL string `validate:"required"`
|
2024-02-16 12:52:27 +00:00
|
|
|
EndpointLogoutURL string // Optional as not all SSO support this.
|
2024-02-11 19:28:00 +00:00
|
|
|
}
|
|
|
|
|
2024-02-15 17:43:35 +00:00
|
|
|
type Temporal struct {
|
|
|
|
DatabasePath string `validate:"required"`
|
|
|
|
ListenAddress string `validate:"required"`
|
|
|
|
UIHost string `validate:"required"`
|
|
|
|
ServerHost string `validate:"required"`
|
2024-02-11 19:28:00 +00:00
|
|
|
}
|
|
|
|
|
2024-02-18 21:37:17 +00:00
|
|
|
type Worker struct {
|
|
|
|
Token string `validate:"required"`
|
2024-02-11 19:28:00 +00:00
|
|
|
}
|
|
|
|
|
2024-02-16 13:21:13 +00:00
|
|
|
func GetEnvOrDefault(key, def string) string {
|
|
|
|
value := os.Getenv(key)
|
|
|
|
if value == "" {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2024-02-11 19:28:00 +00:00
|
|
|
func NewConfig() *Config {
|
2024-02-15 17:43:35 +00:00
|
|
|
viper.SetConfigName("zdravko")
|
|
|
|
viper.SetConfigType("yaml")
|
|
|
|
viper.AddConfigPath("/etc/zdravko/")
|
|
|
|
viper.AddConfigPath("$HOME/.zdravko")
|
|
|
|
viper.AddConfigPath("$HOME/.config/zdravko")
|
|
|
|
viper.AddConfigPath("$XDG_CONFIG_HOME/zdravko")
|
|
|
|
viper.AddConfigPath(".")
|
|
|
|
|
|
|
|
// Set defaults
|
2024-02-16 13:21:13 +00:00
|
|
|
viper.SetDefault("port", GetEnvOrDefault("PORT", "8000"))
|
|
|
|
viper.SetDefault("rooturl", GetEnvOrDefault("ROOT_URL", "http://localhost:8000"))
|
|
|
|
viper.SetDefault("databasepath", GetEnvOrDefault("DATABASE_PATH", "zdravko.db"))
|
2024-02-15 17:43:35 +00:00
|
|
|
viper.SetDefault("sessionsecret", os.Getenv("SESSION_SECRET"))
|
2024-02-16 13:21:13 +00:00
|
|
|
viper.SetDefault("temporal.databasepath", GetEnvOrDefault("TEMPORAL_DATABASE_PATH", "temporal.db"))
|
|
|
|
viper.SetDefault("temporal.listenaddress", GetEnvOrDefault("TEMPORAL_LISTEN_ADDRESS", "0.0.0.0"))
|
|
|
|
viper.SetDefault("temporal.uihost", GetEnvOrDefault("TEMPORAL_UI_HOST", "127.0.0.1:8223"))
|
|
|
|
viper.SetDefault("temporal.serverhost", GetEnvOrDefault("TEMPORAL_SERVER_HOST", "127.0.0.1:7233"))
|
2024-02-18 21:37:17 +00:00
|
|
|
viper.SetDefault("jwt.privatekey", os.Getenv("JWT_PRIVATE_KEY"))
|
|
|
|
viper.SetDefault("jwt.publickey", os.Getenv("JWT_PUBLIC_KEY"))
|
2024-02-15 17:43:35 +00:00
|
|
|
viper.SetDefault("oauth2.clientid", os.Getenv("OAUTH2_CLIENT_ID"))
|
|
|
|
viper.SetDefault("oauth2.clientsecret", os.Getenv("OAUTH2_CLIENT_SECRET"))
|
2024-02-16 13:23:07 +00:00
|
|
|
viper.SetDefault("oauth2.scopes", GetEnvOrDefault("OAUTH2_ENDPOINT_SCOPES", "openid profile email"))
|
2024-02-15 17:43:35 +00:00
|
|
|
viper.SetDefault("oauth2.endpointtokenurl", os.Getenv("OAUTH2_ENDPOINT_TOKEN_URL"))
|
|
|
|
viper.SetDefault("oauth2.endpointauthurl", os.Getenv("OAUTH2_ENDPOINT_AUTH_URL"))
|
|
|
|
viper.SetDefault("oauth2.endpointuserinfourl", os.Getenv("OAUTH2_ENDPOINT_USER_INFO_URL"))
|
2024-02-16 13:21:13 +00:00
|
|
|
viper.SetDefault("oauth2.endpointlogouturl", GetEnvOrDefault("OAUTH2_ENDPOINT_LOGOUT_URL", ""))
|
2024-02-18 21:37:17 +00:00
|
|
|
viper.SetDefault("worker.token", os.Getenv("WORKER_TOKEN"))
|
2024-02-15 17:43:35 +00:00
|
|
|
|
|
|
|
err := viper.ReadInConfig()
|
|
|
|
if err != nil {
|
2024-02-15 22:47:56 +00:00
|
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
|
|
|
// ignore
|
|
|
|
} else {
|
|
|
|
log.Fatalf("Error reading config file, %s", err)
|
|
|
|
}
|
2024-02-15 17:43:35 +00:00
|
|
|
}
|
|
|
|
log.Println("Config file used: ", viper.ConfigFileUsed())
|
|
|
|
|
|
|
|
config := &Config{}
|
|
|
|
err = viper.Unmarshal(config)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error unmarshalling config, %s", err)
|
2024-02-11 19:28:00 +00:00
|
|
|
}
|
2024-02-15 17:43:35 +00:00
|
|
|
|
|
|
|
// OAuth2 scopes are space separated
|
|
|
|
config.OAuth2.Scopes = strings.Split(viper.GetString("oauth2.scopes"), " ")
|
|
|
|
|
|
|
|
// Validate config
|
|
|
|
validate := validator.New(validator.WithRequiredStructEnabled())
|
|
|
|
err = validate.Struct(config)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error validating config, %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
2024-02-11 19:28:00 +00:00
|
|
|
}
|