github.com/goravel/framework@v1.13.9/config/application.go (about) 1 package config 2 3 import ( 4 "os" 5 6 "github.com/gookit/color" 7 "github.com/spf13/cast" 8 "github.com/spf13/viper" 9 10 "github.com/goravel/framework/contracts/config" 11 "github.com/goravel/framework/support" 12 "github.com/goravel/framework/support/file" 13 ) 14 15 var _ config.Config = &Application{} 16 17 type Application struct { 18 vip *viper.Viper 19 } 20 21 func NewApplication(envPath string) *Application { 22 app := &Application{} 23 app.vip = viper.New() 24 app.vip.AutomaticEnv() 25 26 if file.Exists(envPath) { 27 app.vip.SetConfigType("env") 28 app.vip.SetConfigFile(envPath) 29 30 if err := app.vip.ReadInConfig(); err != nil { 31 color.Redln("Invalid Config error: " + err.Error()) 32 os.Exit(0) 33 } 34 } 35 36 appKey := app.Env("APP_KEY") 37 if !support.IsKeyGenerateCommand { 38 if appKey == nil { 39 color.Redln("Please initialize APP_KEY first.") 40 color.Println("Create a .env file and run command: go run . artisan key:generate") 41 color.Println("Or set a system variable: APP_KEY={32-bit number} go run .") 42 os.Exit(0) 43 } 44 45 if len(appKey.(string)) != 32 { 46 color.Redln("Invalid APP_KEY, the length must be 32, please reset it.") 47 color.Warnln("Example command: \ngo run . artisan key:generate") 48 os.Exit(0) 49 } 50 } 51 52 return app 53 } 54 55 // Env Get config from env. 56 func (app *Application) Env(envName string, defaultValue ...any) any { 57 value := app.Get(envName, defaultValue...) 58 if cast.ToString(value) == "" { 59 if len(defaultValue) > 0 { 60 return defaultValue[0] 61 } 62 63 return nil 64 } 65 66 return value 67 } 68 69 // Add config to application. 70 func (app *Application) Add(name string, configuration any) { 71 app.vip.Set(name, configuration) 72 } 73 74 // Get config from application. 75 func (app *Application) Get(path string, defaultValue ...any) any { 76 if !app.vip.IsSet(path) { 77 if len(defaultValue) > 0 { 78 return defaultValue[0] 79 } 80 return nil 81 } 82 83 return app.vip.Get(path) 84 } 85 86 // GetString Get string type config from application. 87 func (app *Application) GetString(path string, defaultValue ...any) string { 88 value := cast.ToString(app.Get(path, defaultValue...)) 89 if value == "" { 90 if len(defaultValue) > 0 { 91 return defaultValue[0].(string) 92 } 93 94 return "" 95 } 96 97 return value 98 } 99 100 // GetInt Get int type config from application. 101 func (app *Application) GetInt(path string, defaultValue ...any) int { 102 value := app.Get(path, defaultValue...) 103 if cast.ToString(value) == "" { 104 if len(defaultValue) > 0 { 105 return defaultValue[0].(int) 106 } 107 108 return 0 109 } 110 111 return cast.ToInt(value) 112 } 113 114 // GetBool Get bool type config from application. 115 func (app *Application) GetBool(path string, defaultValue ...any) bool { 116 value := app.Get(path, defaultValue...) 117 if cast.ToString(value) == "" { 118 if len(defaultValue) > 0 { 119 return defaultValue[0].(bool) 120 } 121 122 return false 123 } 124 125 return cast.ToBool(value) 126 }