Files
easywish/backend/config/config.go
Nikolai Papin 0a38267cb0 feat: configuration parameters for minio host, port, timeout;
refactor: renamed buckets;
fix: corrected Host header changing behavior in minio gin endpoint;
feat: function to convert local minio url to /s3/ path url with the backend host
2025-07-30 14:26:27 +03:00

181 lines
5.5 KiB
Go

// Copyright (c) 2025 Nikolai Papin
//
// This file is part of Easywish
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
// the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package config
import (
"fmt"
"strings"
"time"
"github.com/spf13/viper"
)
type Config struct {
Hostname string `mapstructure:"HOSTNAME"`
Port uint16 `mapstructure:"PORT"`
DatabaseUrl string `mapstructure:"POSTGRES_URL"`
RedisUrl string `mapstructure:"REDIS_URL"`
MinioUrl string `mapstructure:"MINIO_URL"`
MinioHost string `mapstructure:"MINIO_HOST"`
MinioPort uint16 `mapstructure:"MINIO_PORT"`
MinioTimeout int64 `mapstructure:"MINIO_TIMEOUT"`
JwtAlgorithm string `mapstructure:"JWT_ALGORITHM"`
JwtSecret string `mapstructure:"JWT_SECRET"`
JwtIssuer string `mapstructure:"JWT_ISSUER"`
JwtAudience string `mapstructure:"JWT_AUDIENCE"`
JwtExpAccess int `mapstructure:"JWT_EXP_ACCESS"`
JwtExpRefresh int `mapstructure:"JWT_EXP_REFRESH"`
SmtpEnabled bool `mapstructure:"SMTP_ENABLED"`
SmtpServer string `mapstructure:"SMTP_SERVER"`
SmtpPort uint16 `mapstructure:"SMTP_PORT"`
SmtpUser string `mapstructure:"SMTP_USER"`
SmtpPassword string `mapstructure:"SMTP_PASSWORD"`
SmtpFrom string `mapstructure:"SMTP_FROM"`
SmtpUseTLS bool `mapstructure:"SMTP_USE_TLS"`
SmtpUseSSL bool `mapstructure:"SMTP_USE_SSL"`
SmtpTimeout uint `mapstructure:"SMTP_TIMEOUT"`
PasswordMinLength int `mapstructure:"PASSWORD_MIN_LENGTH"`
PasswordMaxLength int `mapstructure:"PASSWORD_MAX_LENGTH"`
PasswordCheckNumbers bool `mapstructure:"PASSWORD_CHECK_NUMBERS"`
PasswordCheckCharacters bool `mapstructure:"PASSWORD_CHECK_CHARACTERS"`
PasswordCheckCases bool `mapstructure:"PASSWORD_CHECK_CASES"`
PasswordCheckSymbols bool `mapstructure:"PASSWORD_CHECK_SYMBOLS"`
PasswordCheckLeaked bool `mapstructure:"PASSWORD_CHECK_LEAKED"`
Environment string `mapstructure:"ENVIRONMENT"`
}
func Load() (*Config, error) {
viper.SetDefault("HOSTNAME", "localhost")
viper.SetDefault("PORT", "8080")
viper.SetDefault("MINIO_TIMEOUT", 90 * time.Second)
viper.SetDefault("JWT_ALGORITHM", "HS256")
viper.SetDefault("JWT_SECRET", "default_jwt_secret_please_change")
viper.SetDefault("JWT_EXP_ACCESS", 5)
viper.SetDefault("JWT_EXP_REFRESH", 10080)
viper.SetDefault("JWT_AUDIENCE", "easywish")
viper.SetDefault("JWT_ISSUER", "easywish")
viper.SetDefault("SMTP_ENABLED", false)
viper.SetDefault("SMTP_USE_TLS", false)
viper.SetDefault("SMTP_USE_SSL", false)
viper.SetDefault("SMTP_FROM", "An Easywish instance")
viper.SetDefault("PASSWORD_MIN_LENGTH", 6)
viper.SetDefault("PASSWORD_MAX_LENGTH", 100)
viper.SetDefault("PASSWORD_CHECK_NUMBERS", false)
viper.SetDefault("PASSWORD_CHECK_CHARACTERS", false)
viper.SetDefault("PASSWORD_CHECK_CASES", false)
viper.SetDefault("PASSWORD_CHECK_SYMBOLS", false)
viper.SetDefault("PASSWORD_CHECK_LEAKED", false)
viper.SetDefault("ENVIRONMENT", "production")
viper.AutomaticEnv()
// Viper's AutomaticEnv() expects lowercase keys for unmarshalling into structs by default,
// while the environment variables and struct tags are in uppercase.
// Here's the stupidity we have to do to fix it:
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.BindEnv("HOSTNAME")
viper.BindEnv("PORT")
viper.BindEnv("MINIO_TIMEOUT")
viper.BindEnv("POSTGRES_URL")
viper.BindEnv("REDIS_URL")
viper.BindEnv("MINIO_URL")
viper.BindEnv("MINIO_HOST")
viper.BindEnv("MINIO_PORT")
viper.BindEnv("JWT_ALGORITHM")
viper.BindEnv("JWT_SECRET")
viper.BindEnv("JWT_ISSUER")
viper.BindEnv("JWT_AUDIENCE")
viper.BindEnv("JWT_EXP_ACCESS")
viper.BindEnv("JWT_EXP_REFRESH")
viper.BindEnv("SMTP_ENABLED")
viper.BindEnv("SMTP_SERVER")
viper.BindEnv("SMTP_PORT")
viper.BindEnv("SMTP_USER")
viper.BindEnv("SMTP_PASSWORD")
viper.BindEnv("SMTP_FROM")
viper.BindEnv("SMTP_USE_TLS")
viper.BindEnv("SMTP_USE_SSL")
viper.BindEnv("SMTP_TIMEOUT")
viper.BindEnv("PASSWORD_MIN_LENGTH")
viper.BindEnv("PASSWORD_MAX_LENGTH")
viper.BindEnv("PASSWORD_CHECK_NUMBERS")
viper.BindEnv("PASSWORD_CHECK_CHARACTERS")
viper.BindEnv("PASSWORD_CHECK_CASES")
viper.BindEnv("PASSWORD_CHECK_SYMBOLS")
viper.BindEnv("PASSWORD_CHECK_LEAKED")
viper.BindEnv("ENVIRONMENT")
required := []string{
"POSTGRES_URL",
"REDIS_URL",
"MINIO_URL",
"MINIO_HOST",
"MINIO_PORT",
}
var missing []string
for _, key := range required {
if !viper.IsSet(key) {
missing = append(missing, key)
}
}
if len(missing) > 0 {
return nil, fmt.Errorf("missing required environment variables: %s", strings.Join(missing, ", "))
}
var cfg Config
if err := viper.Unmarshal(&cfg); err != nil {
return nil, fmt.Errorf("failed to unmarshal config: %w", err)
}
config = &cfg
return &cfg, nil
}
func GetConfig() Config {
if config == nil {
if _, err := Load(); err != nil {
panic(err)
}
}
return *config
}
var config *Config