diff --git a/config.go b/config.go index dea8184..4f6239d 100644 --- a/config.go +++ b/config.go @@ -1,7 +1,7 @@ package hime import ( - "os" + "io/ioutil" "time" yaml "gopkg.in/yaml.v2" @@ -42,7 +42,7 @@ func parseDuration(s string, t *time.Duration) { } } -// Load loads config +// Config merges config into app's config // // Example: // @@ -72,7 +72,7 @@ func parseDuration(s string, t *time.Duration) { // gracefulShutdown: // timeout: 1m // wait: 5s -func (app *App) Load(config Config) *App { +func (app *App) Config(config Config) *App { app.Globals(config.Globals) app.Routes(config.Routes) @@ -110,19 +110,22 @@ func (app *App) Load(config Config) *App { return app } -// LoadFromFile loads config from file -func (app *App) LoadFromFile(filename string) *App { - fs, err := os.Open(filename) +// ParseConfig parses config data +func (app *App) ParseConfig(data []byte) *App { + var config Config + err := yaml.Unmarshal(data, &config) if err != nil { panic(err) } - defer fs.Close() - var config Config - err = yaml.NewDecoder(fs).Decode(&config) + return app.Config(config) +} + +// ParseConfigFile parses config from file +func (app *App) ParseConfigFile(filename string) *App { + data, err := ioutil.ReadFile(filename) if err != nil { panic(err) } - - return app.Load(config) + return app.ParseConfig(data) } diff --git a/config_internal_test.go b/config_internal_test.go index 047dc5a..6334b18 100644 --- a/config_internal_test.go +++ b/config_internal_test.go @@ -12,7 +12,7 @@ func TestConfig(t *testing.T) { t.Run("Config1", func(t *testing.T) { assert.NotPanics(t, func() { - app := New().LoadFromFile("testdata/config1.yaml") + app := New().ParseConfigFile("testdata/config1.yaml") // globals assert.Len(t, app.globals, 1) @@ -44,7 +44,7 @@ func TestConfig(t *testing.T) { t.Run("Config2", func(t *testing.T) { assert.NotPanics(t, func() { - app := New().LoadFromFile("testdata/config2.yaml") + app := New().ParseConfigFile("testdata/config2.yaml") assert.Len(t, app.globals, 0) assert.Len(t, app.routes, 0) @@ -65,7 +65,7 @@ func TestConfig(t *testing.T) { t.Run("Config3", func(t *testing.T) { assert.NotPanics(t, func() { - app := New().LoadFromFile("testdata/config3.yaml") + app := New().ParseConfigFile("testdata/config3.yaml") assert.Len(t, app.globals, 0) assert.Len(t, app.routes, 0) @@ -84,19 +84,19 @@ func TestConfig(t *testing.T) { t.Run("ConfigNotFound", func(t *testing.T) { assert.Panics(t, func() { - New().LoadFromFile("testdata/notexists.yaml") + New().ParseConfigFile("testdata/notexists.yaml") }) }) t.Run("Invalid1", func(t *testing.T) { assert.Panics(t, func() { - New().LoadFromFile("testdata/invalid1.yaml") + New().ParseConfigFile("testdata/invalid1.yaml") }) }) t.Run("Invalid2", func(t *testing.T) { assert.Panics(t, func() { - New().LoadFromFile("testdata/invalid2.yaml") + New().ParseConfigFile("testdata/invalid2.yaml") }) }) }