From e64581e537c92d5e365a8c24b908ee7731e41539 Mon Sep 17 00:00:00 2001 From: Roman Zipp Date: Sun, 23 Jan 2022 16:28:27 +0100 Subject: [PATCH] Add LoadSettings function error handling --- main.go | 18 +++++++++++++++--- settings/settings.go | 20 +++++++++++++++----- settings/settings_test.go | 3 ++- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index bac331a..b9dd294 100644 --- a/main.go +++ b/main.go @@ -6,8 +6,10 @@ import ( "github.com/geplauder/lithium/auth" "github.com/geplauder/lithium/pipelines" + "github.com/geplauder/lithium/settings" "github.com/geplauder/lithium/storage" "github.com/gorilla/mux" + "github.com/spf13/afero" ) const Name string = "Lithium" @@ -43,8 +45,18 @@ func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline, stor } 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() @@ -56,7 +68,7 @@ func main() { RegisterPipelineRoutes(r, pipes, storageProvider) - err := http.ListenAndServe(":8000", r) + err = http.ListenAndServe(settings.Endpoint, r) if err != nil { panic(err) } diff --git a/settings/settings.go b/settings/settings.go index f6ae397..1716c1e 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -28,12 +28,15 @@ type StorageSettings struct { func parseSettings(data []byte) Settings { settings := Settings{} - json.Unmarshal(data, &settings) + err := json.Unmarshal(data, &settings) + if err != nil { + return Settings{} + } return settings } -func LoadSettings(fileSystem afero.Fs) Settings { +func LoadSettings(fileSystem afero.Fs) (Settings, error) { workingDirectory, _ := os.Getwd() path := filepath.Join(workingDirectory, "settings.json") @@ -41,7 +44,7 @@ func LoadSettings(fileSystem afero.Fs) Settings { data, err := afero.ReadFile(fileSystem, path) if err == nil { - return parseSettings(data) + return parseSettings(data), nil } // If file does not exist, create default settings @@ -55,7 +58,14 @@ func LoadSettings(fileSystem afero.Fs) Settings { } 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 } diff --git a/settings/settings_test.go b/settings/settings_test.go index 30f6908..d151b92 100644 --- a/settings/settings_test.go +++ b/settings/settings_test.go @@ -39,7 +39,8 @@ func TestSettingsLoading(t *testing.T) { doesFileExist, _ := afero.Exists(fileSystem, path) assert.False(t, doesFileExist) - LoadSettings(fileSystem) + _, err := LoadSettings(fileSystem) + assert.Nil(t, err) // Settings file should be present after calling LoadSettings doesFileExist, _ = afero.Exists(fileSystem, path)