Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions config/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,26 @@ type Application struct {
}

func NewApplication(envPath string) *Application {
if !file.Exists(envPath) {
color.Redln("Please create " + envPath + " and initialize it first.")
color.Warnln("Example command: \ncp .env.example .env && go run . artisan key:generate")
os.Exit(0)
}

app := &Application{}
app.vip = viper.New()
app.vip.SetConfigType("env")
app.vip.SetConfigFile(envPath)
app.vip.AutomaticEnv()

if err := app.vip.ReadInConfig(); err != nil {
color.Redln("Invalid Config error: " + err.Error())
os.Exit(0)
if file.Exists(envPath) {
app.vip.SetConfigType("env")
app.vip.SetConfigFile(envPath)

if err := app.vip.ReadInConfig(); err != nil {
color.Redln("Invalid Config error: " + err.Error())
os.Exit(0)
}
}

appKey := app.Env("APP_KEY")
if support.Env != support.EnvArtisan {
if !support.IsKeyGenerateCommand {
if appKey == nil {
color.Redln("Please initialize APP_KEY first.")
color.Warnln("Example command: \ngo run . artisan key:generate")
color.Println("Create a .env file and run command: go run . artisan key:generate")
color.Println("Or set a system variable: APP_KEY={32-bit number} go run .")
os.Exit(0)
}

Expand Down
32 changes: 16 additions & 16 deletions config/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ DB_PORT=3306
`))
temp, err := os.CreateTemp("", "goravel.env")
assert.Nil(t, err)
defer temp.Close()
defer os.Remove(temp.Name())

_, err = temp.Write([]byte(`
APP_KEY=12345678901234567890123456789012
APP_DEBUG=true
Expand All @@ -45,22 +47,6 @@ func (s *ApplicationTestSuite) SetupTest() {

}

func (s *ApplicationTestSuite) TestOsVariables() {
s.Nil(os.Setenv("APP_KEY", "12345678901234567890123456789013"))
s.Nil(os.Setenv("OS_APP_NAME", "goravel"))
s.Nil(os.Setenv("OS_APP_PORT", "3306"))
s.Nil(os.Setenv("OS_APP_DEBUG", "true"))

s.Equal("12345678901234567890123456789013", s.config.GetString("APP_KEY"))
s.Equal("12345678901234567890123456789013", s.customConfig.GetString("APP_KEY"))
s.Equal("goravel", s.config.GetString("OS_APP_NAME"))
s.Equal("goravel", s.customConfig.GetString("OS_APP_NAME"))
s.Equal(3306, s.config.GetInt("OS_APP_PORT"))
s.Equal(3306, s.customConfig.GetInt("OS_APP_PORT"))
s.True(s.config.GetBool("OS_APP_DEBUG"))
s.True(s.customConfig.GetBool("OS_APP_DEBUG"))
}

func (s *ApplicationTestSuite) TestEnv() {
s.Equal("12345678901234567890123456789012", s.config.Env("APP_KEY").(string))
s.Equal("goravel", s.config.Env("APP_NAME", "goravel").(string))
Expand Down Expand Up @@ -137,3 +123,17 @@ func (s *ApplicationTestSuite) TestGetBool() {
s.Equal(true, s.config.GetBool("APP_DEBUG"))
s.Equal(true, s.customConfig.GetBool("APP_DEBUG"))
}

func TestOsVariables(t *testing.T) {
assert.Nil(t, os.Setenv("APP_KEY", "12345678901234567890123456789013"))
assert.Nil(t, os.Setenv("APP_NAME", "goravel"))
assert.Nil(t, os.Setenv("APP_PORT", "3306"))
assert.Nil(t, os.Setenv("APP_DEBUG", "true"))

config := NewApplication(".env")

assert.Equal(t, "12345678901234567890123456789013", config.GetString("APP_KEY"))
assert.Equal(t, "goravel", config.GetString("APP_NAME"))
assert.Equal(t, 3306, config.GetInt("APP_PORT"))
assert.True(t, config.GetBool("APP_DEBUG"))
}
4 changes: 3 additions & 1 deletion foundation/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ func setEnv() {
for _, arg := range args[1:] {
if arg == "artisan" {
support.Env = support.EnvArtisan
break
}
if arg == "key:generate" {
support.IsKeyGenerateCommand = true
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions support/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ const (
)

var (
Env = EnvRuntime
EnvPath = ".env"
RelativePath string
RootPath string
Env = EnvRuntime
EnvPath = ".env"
IsKeyGenerateCommand = false
RelativePath string
RootPath string
)