feat(config): 支持多种 profile config

This commit is contained in:
Liam Chan 2024-08-22 11:35:48 +08:00
parent 167c2c873e
commit 6b1cc58445
2 changed files with 20 additions and 2 deletions

3
.gitignore vendored
View File

@ -169,3 +169,6 @@ Network Trash Folder
Temporary Items
.apdisk
# Application
config.dev.yaml

View File

@ -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(".")