123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- 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
- }
|