package config import ( "fmt" "io/ioutil" "os" "github.com/dgrijalva/jwt-go" "github.com/mitchellh/mapstructure" "github.com/spf13/viper" ) var workDir = os.Getenv("GO_WORK_DIR_TMR_GROUP") func Initialize(path string, cfgStruct interface{}) error { if workDir == "" { workDir = "." } dir := fmt.Sprintf("%s/config/%s", workDir, path) viper.SetConfigType("yaml") viper.SetConfigFile(dir) if err := viper.ReadInConfig(); err != nil { return err } if err := viper.Unmarshal(&cfgStruct, func(c *mapstructure.DecoderConfig) { c.TagName = "yaml" }); err != nil { return err } return nil } func openPrivateKey() JwtTokenKeyConfig { pathList := []string{fmt.Sprintf("%s/config/private.key", workDir), fmt.Sprintf("%s/config/public.key", workDir)} res := JwtTokenKeyConfig{PublicKey: nil, PrivateKey: nil} for i, path := range pathList { keyConfig, err := os.Open(path) if err != nil { panic(err) } b, err := ioutil.ReadAll(keyConfig) if err != nil { panic(err) } if i == 0 { parivateKey, err := jwt.ParseRSAPrivateKeyFromPEM(b) if err != nil { panic(err) } res.PrivateKey = parivateKey } if i == 1 { publicKey, err := jwt.ParseRSAPublicKeyFromPEM(b) if err != nil { panic(err) } res.PublicKey = publicKey } } return res }