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 diff --git a/main.go b/main.go index aef8790..ea60a8f 100644 --- a/main.go +++ b/main.go @@ -5,8 +5,10 @@ import ( "net/http" "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" @@ -42,9 +44,15 @@ func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline, stor } func main() { - storageProvider := storage.GetFileSystemStorageProvider("test") + settings := settings.LoadSettings(afero.NewOsFs()) - storageProvider.StoreRaw("abc", "def.test", []byte{0x12, 0x10}) + var storageProvider storage.IStorageProvider + + if settings.StorageProvider.Type == 0 { + storageProvider = storage.GetFileSystemStorageProvider(settings.StorageProvider.BasePath) + } else { + panic("Invalid file system provided!") + } pipes := pipelines.LoadPipelines() @@ -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 new file mode 100644 index 0000000..f6ae397 --- /dev/null +++ b/settings/settings.go @@ -0,0 +1,61 @@ +package settings + +import ( + "encoding/json" + "os" + "path/filepath" + + "github.com/spf13/afero" +) + +const ( + Local FileSystemType = iota +) + +type FileSystemType int + +type Settings struct { + Endpoint string `json:"endpoint"` + Token string `json:"token"` + StorageProvider StorageSettings `json:"storage_provider"` +} + +type StorageSettings struct { + Type FileSystemType `json:"type"` + BasePath string `json:"base_path"` +} + +func parseSettings(data []byte) Settings { + settings := Settings{} + + json.Unmarshal(data, &settings) + + return settings +} + +func LoadSettings(fileSystem afero.Fs) Settings { + workingDirectory, _ := os.Getwd() + path := filepath.Join(workingDirectory, "settings.json") + + // Load file and parse file + data, err := afero.ReadFile(fileSystem, 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", + StorageProvider: StorageSettings{ + Type: Local, + BasePath: "assets", + }, + } + + serializedSettings, err := json.MarshalIndent(defaultSettings, "", "\t") + afero.WriteFile(fileSystem, path, serializedSettings, os.ModePerm) + + return defaultSettings +} diff --git a/settings/settings_test.go b/settings/settings_test.go new file mode 100644 index 0000000..30f6908 --- /dev/null +++ b/settings/settings_test.go @@ -0,0 +1,48 @@ +package settings + +import ( + "os" + "path/filepath" + "testing" + + "github.com/spf13/afero" + "github.com/stretchr/testify/assert" +) + +func TestSettingsParsing(t *testing.T) { + const file string = `{ + "endpoint": "0.0.0.0:8000", + "token": "foobar", + "storage_provider": { + "type": 0, + "base_path": "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.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) + }) +}