ai优化,删除未使用代码,提取重复代码。
This commit is contained in:
+1
-16
@@ -2,8 +2,6 @@ package config
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/spf13/viper"
|
||||
@@ -82,18 +80,13 @@ func setDefaults() {
|
||||
|
||||
// parseConfig 解析配置到结构体
|
||||
func parseConfig() *Config {
|
||||
port := viper.GetInt("server.port")
|
||||
if port == 0 {
|
||||
port, _ = strconv.Atoi(getEnv("SERVER_PORT", "8080"))
|
||||
}
|
||||
|
||||
return &Config{
|
||||
Database: DatabaseConfig{
|
||||
Driver: viper.GetString("database.driver"),
|
||||
DSN: viper.GetString("database.dsn"),
|
||||
},
|
||||
Server: ServerConfig{
|
||||
Port: port,
|
||||
Port: viper.GetInt("server.port"),
|
||||
Mode: viper.GetString("server.mode"),
|
||||
},
|
||||
App: AppConfig{
|
||||
@@ -124,11 +117,3 @@ func watchConfig() {
|
||||
func GetGlobalConfig() *Config {
|
||||
return globalConfig
|
||||
}
|
||||
|
||||
// getEnv 获取环境变量
|
||||
func getEnv(key, defaultValue string) string {
|
||||
if value := os.Getenv(key); value != "" {
|
||||
return value
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user