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
|
Temporary Items
|
||||||
.apdisk
|
.apdisk
|
||||||
|
|
||||||
|
# Application
|
||||||
|
config.dev.yaml
|
||||||
|
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import "github.com/spf13/viper"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Server ServerConfig `mapstructure:"server"`
|
Server ServerConfig `mapstructure:"server"`
|
||||||
@ -14,7 +19,17 @@ type ServerConfig struct {
|
|||||||
var AppConfig Config
|
var AppConfig Config
|
||||||
|
|
||||||
func LoadConfig() error {
|
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.SetConfigType("yaml")
|
||||||
viper.AddConfigPath(".")
|
viper.AddConfigPath(".")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user