feat(config): 支持多种 profile config
This commit is contained in:
parent
167c2c873e
commit
6b1cc58445
3
.gitignore
vendored
3
.gitignore
vendored
@ -169,3 +169,6 @@ Network Trash Folder
|
||||
Temporary Items
|
||||
.apdisk
|
||||
|
||||
# Application
|
||||
config.dev.yaml
|
||||
|
||||
|
@ -1,6 +1,11 @@
|
||||
package config
|
||||
|
||||
import "github.com/spf13/viper"
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/viper"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Server ServerConfig `mapstructure:"server"`
|
||||
@ -14,7 +19,17 @@ type ServerConfig struct {
|
||||
var AppConfig Config
|
||||
|
||||
func LoadConfig() error {
|
||||
viper.SetConfigName("config")
|
||||
env := os.Getenv("PROFILE")
|
||||
var configName string
|
||||
if env == "" {
|
||||
configName = "config"
|
||||
} else {
|
||||
configName = fmt.Sprint("config.", env)
|
||||
}
|
||||
|
||||
log.Println("Loading config: ", configName)
|
||||
|
||||
viper.SetConfigName(configName)
|
||||
viper.SetConfigType("yaml")
|
||||
viper.AddConfigPath(".")
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user