Browse Source

Add LoadSettings function error handling

Roman Zipp 3 years ago
committed by Fabian Vowie
parent
commit
e64581e537
No known key found for this signature in database GPG Key ID: C27317C33B27C410
  1. 18
      main.go
  2. 20
      settings/settings.go
  3. 3
      settings/settings_test.go

18
main.go

@ -6,8 +6,10 @@ import (
"github.com/geplauder/lithium/auth" "github.com/geplauder/lithium/auth"
"github.com/geplauder/lithium/pipelines" "github.com/geplauder/lithium/pipelines"
"github.com/geplauder/lithium/settings"
"github.com/geplauder/lithium/storage" "github.com/geplauder/lithium/storage"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/spf13/afero"
) )
const Name string = "Lithium" const Name string = "Lithium"
@ -43,8 +45,18 @@ func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline, stor
} }
func main() { func main() {
storageProvider := storage.GetFileSystemStorageProvider("test", "")
storageProvider.StoreRaw("abc", "def.test", []byte{0x12, 0x10})
settings, err := settings.LoadSettings(afero.NewOsFs())
if err != nil {
panic(err)
}
var storageProvider storage.IStorageProvider
if settings.StorageProvider.Type == 0 {
storageProvider = storage.GetFileSystemStorageProvider(settings.StorageProvider.BasePath, "")
} else {
panic("Invalid file system provided!")
}
pipes := pipelines.LoadPipelines() pipes := pipelines.LoadPipelines()
@ -56,7 +68,7 @@ func main() {
RegisterPipelineRoutes(r, pipes, storageProvider) RegisterPipelineRoutes(r, pipes, storageProvider)
err := http.ListenAndServe(":8000", r)
err = http.ListenAndServe(settings.Endpoint, r)
if err != nil { if err != nil {
panic(err) panic(err)
} }

20
settings/settings.go

@ -28,12 +28,15 @@ type StorageSettings struct {
func parseSettings(data []byte) Settings { func parseSettings(data []byte) Settings {
settings := Settings{} settings := Settings{}
json.Unmarshal(data, &settings)
err := json.Unmarshal(data, &settings)
if err != nil {
return Settings{}
}
return settings return settings
} }
func LoadSettings(fileSystem afero.Fs) Settings {
func LoadSettings(fileSystem afero.Fs) (Settings, error) {
workingDirectory, _ := os.Getwd() workingDirectory, _ := os.Getwd()
path := filepath.Join(workingDirectory, "settings.json") path := filepath.Join(workingDirectory, "settings.json")
@ -41,7 +44,7 @@ func LoadSettings(fileSystem afero.Fs) Settings {
data, err := afero.ReadFile(fileSystem, path) data, err := afero.ReadFile(fileSystem, path)
if err == nil { if err == nil {
return parseSettings(data)
return parseSettings(data), nil
} }
// If file does not exist, create default settings // If file does not exist, create default settings
@ -55,7 +58,14 @@ func LoadSettings(fileSystem afero.Fs) Settings {
} }
serializedSettings, err := json.MarshalIndent(defaultSettings, "", "\t") serializedSettings, err := json.MarshalIndent(defaultSettings, "", "\t")
afero.WriteFile(fileSystem, path, serializedSettings, os.ModePerm)
if err != nil {
return Settings{}, err
}
err = afero.WriteFile(fileSystem, path, serializedSettings, os.ModePerm)
if err != nil {
return Settings{}, err
}
return defaultSettings
return defaultSettings, nil
} }

3
settings/settings_test.go

@ -39,7 +39,8 @@ func TestSettingsLoading(t *testing.T) {
doesFileExist, _ := afero.Exists(fileSystem, path) doesFileExist, _ := afero.Exists(fileSystem, path)
assert.False(t, doesFileExist) assert.False(t, doesFileExist)
LoadSettings(fileSystem)
_, err := LoadSettings(fileSystem)
assert.Nil(t, err)
// Settings file should be present after calling LoadSettings // Settings file should be present after calling LoadSettings
doesFileExist, _ = afero.Exists(fileSystem, path) doesFileExist, _ = afero.Exists(fileSystem, path)

Loading…
Cancel
Save