Browse Source

repair config env error

main
SliverHorn 4 years ago
parent
commit
8475853b31
  1. 1
      server/config.yaml
  2. 1
      server/config/system.go
  3. 9
      server/core/config.go
  4. 6
      server/utils/constant.go

1
server/config.yaml

@ -42,7 +42,6 @@ system:
addr: 8888 addr: 8888
db-type: 'mysql' db-type: 'mysql'
oss-type: 'local' oss-type: 'local'
config-env: 'GVA_CONFIG'
need-init-data: false need-init-data: false
use-multipoint: false use-multipoint: false

1
server/config/system.go

@ -5,7 +5,6 @@ type System struct {
Addr int `mapstructure:"addr" json:"addr" yaml:"addr"` Addr int `mapstructure:"addr" json:"addr" yaml:"addr"`
DbType string `mapstructure:"db-type" json:"dbType" yaml:"db-type"` DbType string `mapstructure:"db-type" json:"dbType" yaml:"db-type"`
OssType string `mapstructure:"oss-type" json:"ossType" yaml:"oss-type"` OssType string `mapstructure:"oss-type" json:"ossType" yaml:"oss-type"`
ConfigEnv string `mapstructure:"config-env" json:"configEnv" yaml:"config-env"`
NeedInitData bool `mapstructure:"need-init-data" json:"needInitData" yaml:"need-init-data"` NeedInitData bool `mapstructure:"need-init-data" json:"needInitData" yaml:"need-init-data"`
UseMultipoint bool `mapstructure:"use-multipoint" json:"useMultipoint" yaml:"use-multipoint"` UseMultipoint bool `mapstructure:"use-multipoint" json:"useMultipoint" yaml:"use-multipoint"`
} }

9
server/core/config.go

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"gin-vue-admin/global" "gin-vue-admin/global"
_ "gin-vue-admin/packfile" _ "gin-vue-admin/packfile"
"gin-vue-admin/utils"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/spf13/viper" "github.com/spf13/viper"
"os" "os"
@ -12,15 +13,13 @@ import (
var config string var config string
const defaultConfigFile = "config.yaml"
func init() { func init() {
flag.StringVar(&config, "c", "", "choose config file.") flag.StringVar(&config, "c", "", "choose config file.")
flag.Parse() flag.Parse()
if config == "" { // 优先级: 命令行 > 环境变量 > 默认值 if config == "" { // 优先级: 命令行 > 环境变量 > 默认值
if configEnv := os.Getenv(global.GVA_CONFIG.System.ConfigEnv); configEnv == "" {
config = defaultConfigFile
fmt.Printf("您正在使用config的默认值,config的路径为%v\n", defaultConfigFile)
if configEnv := os.Getenv(utils.ConfigEnv); configEnv == "" {
config = utils.ConfigFile
fmt.Printf("您正在使用config的默认值,config的路径为%v\n", utils.ConfigFile)
} else { } else {
config = configEnv config = configEnv
fmt.Printf("您正在使用GVA_CONFIG环境变量,config的路径为%v\n", config) fmt.Printf("您正在使用GVA_CONFIG环境变量,config的路径为%v\n", config)

6
server/utils/constant.go

@ -0,0 +1,6 @@
package utils
const (
ConfigEnv = "GVA_CONFIG"
ConfigFile = "config.yaml"
)
Loading…
Cancel
Save