From 49b17e604138fada7e9c71676a2d5b8ec9872bdd Mon Sep 17 00:00:00 2001 From: Roman Zipp Date: Sun, 23 Jan 2022 16:28:27 +0100 Subject: [PATCH 1/3] Add LoadSettings function error handling --- main.go | 7 +++++-- settings/settings.go | 20 +++++++++++++++----- settings/settings_test.go | 3 ++- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index c20d909..b9dd294 100644 --- a/main.go +++ b/main.go @@ -45,7 +45,10 @@ func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline, stor } func main() { - settings := settings.LoadSettings(afero.NewOsFs()) + settings, err := settings.LoadSettings(afero.NewOsFs()) + if err != nil { + panic(err) + } var storageProvider storage.IStorageProvider @@ -65,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) From 31e848e61a6a1fd692edcb83ca1f80c5fe1e083f Mon Sep 17 00:00:00 2001 From: Roman Zipp Date: Sun, 23 Jan 2022 16:29:19 +0100 Subject: [PATCH 2/3] Fix main app settings variable name collision --- main.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index b9dd294..33cf1f7 100644 --- a/main.go +++ b/main.go @@ -45,22 +45,22 @@ func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline, stor } func main() { - settings, err := settings.LoadSettings(afero.NewOsFs()) + appSettings, 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, "") + if appSettings.StorageProvider.Type == 0 { + storageProvider = storage.GetFileSystemStorageProvider(appSettings.StorageProvider.BasePath, "") } else { panic("Invalid file system provided!") } pipes := pipelines.LoadPipelines() - authMiddleware := auth.CreateAuthenticationMiddleware(settings.Token) + authMiddleware := auth.CreateAuthenticationMiddleware(appSettings.Token) r := mux.NewRouter() r.Use(authMiddleware.Middleware) @@ -68,7 +68,7 @@ func main() { RegisterPipelineRoutes(r, pipes, storageProvider) - err = http.ListenAndServe(settings.Endpoint, r) + err = http.ListenAndServe(appSettings.Endpoint, r) if err != nil { panic(err) } From 80a945d3c9d6b00400ac4095b918eba8d8a283fd Mon Sep 17 00:00:00 2001 From: Roman Zipp Date: Sun, 23 Jan 2022 16:30:12 +0100 Subject: [PATCH 3/3] Remove print debug statements --- main_test.go | 5 +---- pipelines/pipeline.go | 3 --- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/main_test.go b/main_test.go index 1c0feeb..0e2941d 100644 --- a/main_test.go +++ b/main_test.go @@ -2,7 +2,6 @@ package main import ( "encoding/json" - "fmt" "net/http" "net/http/httptest" "testing" @@ -29,9 +28,7 @@ func TestIndexRoute(t *testing.T) { func TestEndpointRoute(t *testing.T) { data := pipelines.Pipeline{} err := faker.FakeData(&data) - if err != nil { - fmt.Println(err) - } + assert.Nil(t, err) t.Run("Registered pipelines are valid routes", func(t *testing.T) { router := mux.NewRouter() diff --git a/pipelines/pipeline.go b/pipelines/pipeline.go index c5cc9b8..36540c4 100644 --- a/pipelines/pipeline.go +++ b/pipelines/pipeline.go @@ -44,8 +44,6 @@ type Pipeline struct { } func (p Pipeline) Run(srcPath, bucketName string, storageProvider storage.IStorageProvider) (string, error) { - fmt.Println("path: ", storageProvider.GetPath(bucketName, srcPath)) - src, err := imaging.Open(storageProvider.GetPath(bucketName, srcPath)) if err != nil { return "", errors.New(fmt.Sprintf("error opening file for processing: %s", err)) @@ -134,7 +132,6 @@ func LoadPipelines() []IPipeline { err := filepath.Walk(path+"/config", func(path string, info fs.FileInfo, err error) error { if err == nil && info.IsDir() == false { - fmt.Println(path) data, _ := os.ReadFile(path) files = append(files, data) }