From 904fdf698ee3c6ce81e410a7f5facd8ddc091f2a Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Mon, 17 Jan 2022 11:53:08 +0100 Subject: [PATCH 1/8] Add settings parsing --- settings/settings.go | 28 ++++++++++++++++++++++++++++ settings/settings_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 settings/settings.go create mode 100644 settings/settings_test.go diff --git a/settings/settings.go b/settings/settings.go new file mode 100644 index 0000000..6e88584 --- /dev/null +++ b/settings/settings.go @@ -0,0 +1,28 @@ +package settings + +import "encoding/json" + +const ( + Local FileSystemType = iota +) + +type FileSystemType int + +type Settings struct { + Endpoint string + Token string + FileSystem FileSystemSettings +} + +type FileSystemSettings struct { + Type FileSystemType + BasePath string +} + +func ParseSettings(data []byte) Settings { + settings := Settings{} + + json.Unmarshal(data, &settings) + + return settings +} diff --git a/settings/settings_test.go b/settings/settings_test.go new file mode 100644 index 0000000..3a1f44e --- /dev/null +++ b/settings/settings_test.go @@ -0,0 +1,26 @@ +package settings + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSettingsLoading(t *testing.T) { + const file string = `{ + "endpoint": "0.0.0.0:8000", + "token": "foobar", + "fileSystem": { + "type": 0, + "basePath": "assets" + } + }` + + t.Run("Settings parsing is successful", func(t *testing.T) { + settings := ParseSettings([]byte(file)) + + assert.Equal(t, "0.0.0.0:8000", settings.Endpoint) + assert.Equal(t, "foobar", settings.Token) + assert.Equal(t, "assets", settings.FileSystem.BasePath) + }) +} From dbd19d590c5b321060ad1aeaa127ac003f10eacc Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Mon, 17 Jan 2022 12:07:38 +0100 Subject: [PATCH 2/8] Load settings on program start or create default if none exist --- main.go | 12 ++++++++++-- settings/settings.go | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index aef8790..94a9017 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/geplauder/lithium/pipelines" + "github.com/geplauder/lithium/settings" "github.com/geplauder/lithium/storage" "github.com/gorilla/mux" ) @@ -42,10 +43,17 @@ func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline, stor } func main() { - storageProvider := storage.GetFileSystemStorageProvider("test") + settings := settings.LoadSettings() + var storageProvider storage.IStorageProvider storageProvider.StoreRaw("abc", "def.test", []byte{0x12, 0x10}) + if settings.FileSystem.Type == 0 { + storageProvider = storage.GetFileSystemStorageProvider(settings.FileSystem.BasePath) + } else { + panic("Invalid file system provided!") + } + pipes := pipelines.LoadPipelines() r := mux.NewRouter() @@ -53,7 +61,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 6e88584..6239703 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -1,6 +1,10 @@ package settings -import "encoding/json" +import ( + "encoding/json" + "os" + "path/filepath" +) const ( Local FileSystemType = iota @@ -26,3 +30,30 @@ func ParseSettings(data []byte) Settings { return settings } + +func LoadSettings() Settings { + workingDirectory, _ := os.Getwd() + path := filepath.Join(workingDirectory, "settings.json") + + // Load file and parse file + data, err := os.ReadFile(path) + + if err == nil { + return ParseSettings(data) + } + + // If file does not exist, create default settings + defaultSettings := Settings{ + Endpoint: "127.0.0.1:8000", + Token: "changeme", + FileSystem: FileSystemSettings{ + Type: Local, + BasePath: "assets", + }, + } + + serializedSettings, err := json.Marshal(defaultSettings) + os.WriteFile(path, serializedSettings, os.ModePerm) + + return defaultSettings +} From ec2f376508861ffa61cd29c4fba52bba1da6d33c Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Mon, 17 Jan 2022 12:08:00 +0100 Subject: [PATCH 3/8] Remove leftover debug code --- main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/main.go b/main.go index 94a9017..42c4945 100644 --- a/main.go +++ b/main.go @@ -46,7 +46,6 @@ func main() { settings := settings.LoadSettings() var storageProvider storage.IStorageProvider - storageProvider.StoreRaw("abc", "def.test", []byte{0x12, 0x10}) if settings.FileSystem.Type == 0 { storageProvider = storage.GetFileSystemStorageProvider(settings.FileSystem.BasePath) From ebb6ec88b3fdb7c030ab646eb1f93ac4f79e9d81 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Mon, 17 Jan 2022 12:09:39 +0100 Subject: [PATCH 4/8] Pretty-print default settings json --- settings/settings.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/settings/settings.go b/settings/settings.go index 6239703..c4186fc 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -52,7 +52,7 @@ func LoadSettings() Settings { }, } - serializedSettings, err := json.Marshal(defaultSettings) + serializedSettings, err := json.MarshalIndent(defaultSettings, "", "\t") os.WriteFile(path, serializedSettings, os.ModePerm) return defaultSettings From d87ae291e4d80b325a5a6cb220a74fe7b9cb5c43 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Mon, 17 Jan 2022 12:10:48 +0100 Subject: [PATCH 5/8] Rename FileSystem in settings to StorageProvider --- main.go | 4 ++-- settings/settings.go | 14 +++++++------- settings/settings_test.go | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index 42c4945..e8bf674 100644 --- a/main.go +++ b/main.go @@ -47,8 +47,8 @@ func main() { var storageProvider storage.IStorageProvider - if settings.FileSystem.Type == 0 { - storageProvider = storage.GetFileSystemStorageProvider(settings.FileSystem.BasePath) + if settings.StorageProvider.Type == 0 { + storageProvider = storage.GetFileSystemStorageProvider(settings.StorageProvider.BasePath) } else { panic("Invalid file system provided!") } diff --git a/settings/settings.go b/settings/settings.go index c4186fc..35e960d 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -13,17 +13,17 @@ const ( type FileSystemType int type Settings struct { - Endpoint string - Token string - FileSystem FileSystemSettings + Endpoint string + Token string + StorageProvider StorageSettings } -type FileSystemSettings struct { +type StorageSettings struct { Type FileSystemType BasePath string } -func ParseSettings(data []byte) Settings { +func parseSettings(data []byte) Settings { settings := Settings{} json.Unmarshal(data, &settings) @@ -39,14 +39,14 @@ func LoadSettings() Settings { data, err := os.ReadFile(path) if err == nil { - return ParseSettings(data) + return parseSettings(data) } // If file does not exist, create default settings defaultSettings := Settings{ Endpoint: "127.0.0.1:8000", Token: "changeme", - FileSystem: FileSystemSettings{ + StorageProvider: StorageSettings{ Type: Local, BasePath: "assets", }, diff --git a/settings/settings_test.go b/settings/settings_test.go index 3a1f44e..96e667a 100644 --- a/settings/settings_test.go +++ b/settings/settings_test.go @@ -10,17 +10,17 @@ func TestSettingsLoading(t *testing.T) { const file string = `{ "endpoint": "0.0.0.0:8000", "token": "foobar", - "fileSystem": { + "storageProvider": { "type": 0, "basePath": "assets" } }` t.Run("Settings parsing is successful", func(t *testing.T) { - settings := ParseSettings([]byte(file)) + settings := parseSettings([]byte(file)) assert.Equal(t, "0.0.0.0:8000", settings.Endpoint) assert.Equal(t, "foobar", settings.Token) - assert.Equal(t, "assets", settings.FileSystem.BasePath) + assert.Equal(t, "assets", settings.StorageProvider.BasePath) }) } From c28d828991c68d7a89b4fc70f52d4bf1a49f12b3 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Mon, 17 Jan 2022 12:11:16 +0100 Subject: [PATCH 6/8] Add settings.json to .gitignore --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5bbec02..a6482a3 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,7 @@ *.out # Go workspace file -go.work \ No newline at end of file +go.work + +# Lithium specific +settings.json \ No newline at end of file From 3e7f1a0a7f1858f280af35419eef36c1730daddf Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Mon, 17 Jan 2022 17:01:54 +0100 Subject: [PATCH 7/8] Improve testability for LoadSettings function --- main.go | 3 ++- settings/settings.go | 8 +++++--- settings/settings_test.go | 24 +++++++++++++++++++++++- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index e8bf674..ea60a8f 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "github.com/geplauder/lithium/settings" "github.com/geplauder/lithium/storage" "github.com/gorilla/mux" + "github.com/spf13/afero" ) const Name string = "Lithium" @@ -43,7 +44,7 @@ func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline, stor } func main() { - settings := settings.LoadSettings() + settings := settings.LoadSettings(afero.NewOsFs()) var storageProvider storage.IStorageProvider diff --git a/settings/settings.go b/settings/settings.go index 35e960d..b0006b1 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -4,6 +4,8 @@ import ( "encoding/json" "os" "path/filepath" + + "github.com/spf13/afero" ) const ( @@ -31,12 +33,12 @@ func parseSettings(data []byte) Settings { return settings } -func LoadSettings() Settings { +func LoadSettings(fileSystem afero.Fs) Settings { workingDirectory, _ := os.Getwd() path := filepath.Join(workingDirectory, "settings.json") // Load file and parse file - data, err := os.ReadFile(path) + data, err := afero.ReadFile(fileSystem, path) if err == nil { return parseSettings(data) @@ -53,7 +55,7 @@ func LoadSettings() Settings { } serializedSettings, err := json.MarshalIndent(defaultSettings, "", "\t") - os.WriteFile(path, serializedSettings, os.ModePerm) + afero.WriteFile(fileSystem, path, serializedSettings, os.ModePerm) return defaultSettings } diff --git a/settings/settings_test.go b/settings/settings_test.go index 96e667a..2610eab 100644 --- a/settings/settings_test.go +++ b/settings/settings_test.go @@ -1,12 +1,15 @@ package settings import ( + "os" + "path/filepath" "testing" + "github.com/spf13/afero" "github.com/stretchr/testify/assert" ) -func TestSettingsLoading(t *testing.T) { +func TestSettingsParsing(t *testing.T) { const file string = `{ "endpoint": "0.0.0.0:8000", "token": "foobar", @@ -24,3 +27,22 @@ func TestSettingsLoading(t *testing.T) { assert.Equal(t, "assets", settings.StorageProvider.BasePath) }) } + +func TestSettingsLoading(t *testing.T) { + t.Run("Settings loading creates default settings.json when none is present", func(t *testing.T) { + fileSystem := afero.NewMemMapFs() + workingDirectory, _ := os.Getwd() + + path := filepath.Join(workingDirectory, "settings.json") + + // Settings file does not exist in the beginning + doesFileExist, _ := afero.Exists(fileSystem, path) + assert.False(t, doesFileExist) + + LoadSettings(fileSystem) + + // Settings file should be present after calling LoadSettings + doesFileExist, _ = afero.Exists(fileSystem, path) + assert.True(t, doesFileExist) + }) +} From 6f72597699c16af013e495c03798b49249c568ba Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Mon, 17 Jan 2022 17:05:59 +0100 Subject: [PATCH 8/8] Add tags for json serialization to settings structs --- settings/settings.go | 10 +++++----- settings/settings_test.go | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/settings/settings.go b/settings/settings.go index b0006b1..f6ae397 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -15,14 +15,14 @@ const ( type FileSystemType int type Settings struct { - Endpoint string - Token string - StorageProvider StorageSettings + Endpoint string `json:"endpoint"` + Token string `json:"token"` + StorageProvider StorageSettings `json:"storage_provider"` } type StorageSettings struct { - Type FileSystemType - BasePath string + Type FileSystemType `json:"type"` + BasePath string `json:"base_path"` } func parseSettings(data []byte) Settings { diff --git a/settings/settings_test.go b/settings/settings_test.go index 2610eab..30f6908 100644 --- a/settings/settings_test.go +++ b/settings/settings_test.go @@ -13,9 +13,9 @@ func TestSettingsParsing(t *testing.T) { const file string = `{ "endpoint": "0.0.0.0:8000", "token": "foobar", - "storageProvider": { + "storage_provider": { "type": 0, - "basePath": "assets" + "base_path": "assets" } }`