我试图让 Viper 读取我的环境变量,但它不起作用。这是我的配置:
# app.yaml
dsn: RESTFUL_APP_DSN
jwt_verification_key: RESTFUL_APP_JWT_VERIFICATION_KEY
jwt_signing_key: RESTFUL_APP_JWT_SIGNING_KEY
jwt_signing_method: "HS256"
还有我的config.go
文件:
package config
import (
"fmt"
"strings"
"github.com/go-ozzo/ozzo-validation"
"github.com/spf13/viper"
)
// Config stores the application-wide configurations
var Config appConfig
type appConfig struct {
// the path to the error message file. Defaults to "config/errors.yaml"
ErrorFile string `mapstructure:"error_file"`
// the server port. Defaults to 8080
ServerPort int `mapstructure:"server_port"`
// the data source name (DSN) for connecting to the database. required.
DSN string `mapstructure:"dsn"`
// the signing method for JWT. Defaults to "HS256"
JWTSigningMethod string `mapstructure:"jwt_signing_method"`
// JWT signing key. required.
JWTSigningKey string `mapstructure:"jwt_signing_key"`
// JWT verification key. required.
JWTVerificationKey string `mapstructure:"jwt_verification_key"`
}
func (config appConfig) Validate() error {
return validation.ValidateStruct(&config,
validation.Field(&config.DSN, validation.Required),
validation.Field(&config.JWTSigningKey, validation.Required),
validation.Field(&config.JWTVerificationKey, validation.Required),
)
}
func LoadConfig(configpaths ...string) error {
v := viper.New()
v.SetConfigName("app")
v.SetConfigType("yaml")
v.SetEnvPrefix("restful")
v.AutomaticEnv()
v.SetDefault("error_file", "config/errors.yaml")
v.SetDefault("server_port", 1530)
v.SetDefault("jwt_signing_method", "HS256")
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
for _, path := range configpaths {
v.AddConfigPath(path)
}
if err := v.ReadInConfig(); err != nil {
return fmt.Errorf("Failed to read the configuration file: %s", err)
}
if err := v.Unmarshal(&Config); err != nil {
return err
}
// Checking with this line. This is what I get:
// RESTFUL_JWT_SIGNING_KEY
fmt.Println("Sign Key: ", v.GetString("jwt_signing_key"))
return Config.Validate()
}
这一行fmt.Println("Sign Key: ", v.GetString("jwt_signing_key"))
只给了我在 yaml 文件中传递的密钥RESTFUL_JWT_SIGNING_KEY
。我不知道我做错了什么。
根据文档:
AutomaticEnv 是一个强大的助手,尤其是与 SetEnvPrefix 结合使用时。调用时,Viper 将在任何时候发出 viper.Get 请求时检查环境变量。它将应用以下规则。它将检查名称与大写键匹配的环境变量,如果设置了 EnvPrefix 前缀。
那么,为什么不读取环境变量呢?
使用JSON
{
"dsn": "RESTFUL_APP_DSN",
"jwt_verification_key": "RESTFUL_APP_JWT_VERIFICATION_KEY",
"jwt_signing_key": "RESTFUL_APP_JWT_SIGNING_KEY",
"jwt_signing_method": "HS256"
}
我的解析器看起来像这样:
// LoadConfigEnv loads configuration from the given list of paths and populates it into the Config variable.
// Environment variables with the prefix "RESTFUL_" in their names are also read automatically.
func LoadConfigEnv(environment string, configpaths ...string) error {
v := viper.New()
v.SetConfigName(environment)
v.SetConfigType("json")
v.SetEnvPrefix("restful")
v.AutomaticEnv()
v.SetDefault("jwt_signing_method", "HS256")
v.SetDefault("error_file", "config/errors.yaml")
v.SetDefault("server_port", 1530)
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
for _, path := range configpaths {
v.AddConfigPath(path)
}
if err := v.ReadInConfig(); err != nil {
return fmt.Errorf("Failed to read the configuration file: %s", err)
}
if err := v.Unmarshal(&Config); err != nil {
return err
}
return Config.Validate()
}
在Validate
函数中,我决定检查Config
结构,这就是我得到的:
Config: {config/errors.yaml 1530 RESTFUL_APP_DSN HS256 RESTFUL_APP_JWT_SIGNING_KEY RESTFUL_APP_JWT_VERIFICATION_KEY}