Browse Source

Merge branch 'main' into pr-feature/add-image-processing

feature/add-authorization
Roman Zipp 3 years ago
committed by Fabian Vowie
parent
commit
2841299689
No known key found for this signature in database GPG Key ID: C27317C33B27C410
  1. 3
      .gitignore
  2. 15
      main.go
  3. 61
      settings/settings.go
  4. 48
      settings/settings_test.go

3
.gitignore

@ -13,3 +13,6 @@
# Go workspace file
go.work
# Lithium specific
settings.json

15
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,8 +44,15 @@ func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline, stor
}
func main() {
storageProvider := storage.GetFileSystemStorageProvider("test", "")
storageProvider.StoreRaw("abc", "def.test", []byte{0x12, 0x10})
settings := settings.LoadSettings(afero.NewOsFs())
var storageProvider storage.IStorageProvider
if settings.StorageProvider.Type == 0 {
storageProvider = storage.GetFileSystemStorageProvider(settings.StorageProvider.BasePath, "")
} else {
panic("Invalid file system provided!")
}
pipes := pipelines.LoadPipelines()
@ -52,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)
}

61
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
}

48
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)
})
}
Loading…
Cancel
Save