Compare commits
merge into: FabianVowie:main
FabianVowie:feature/add-authorization
FabianVowie:feature/add-commit-hash-to-index-metadata
FabianVowie:feature/add-executable-steps
FabianVowie:feature/add-get-files-endpoints
FabianVowie:feature/add-github-ci
FabianVowie:feature/add-http-image-upload
FabianVowie:feature/add-image-processing
FabianVowie:feature/add-logging
FabianVowie:feature/add-metadata-endpoint
FabianVowie:feature/add-more-image-steps
FabianVowie:feature/add-pipeline-endpoints
FabianVowie:feature/add-pipeline-loading
FabianVowie:feature/add-pipelines-to-metadata
FabianVowie:feature/add-rate-limiting
FabianVowie:feature/add-rate-limiting-to-readme
FabianVowie:feature/add-settings
FabianVowie:feature/add-storage-layer
FabianVowie:feature/fix-routes
FabianVowie:feature/general-cleanup
FabianVowie:feature/improve-code-stability
FabianVowie:feature/improve-pipeline-abstraction
FabianVowie:feature/make-middlewares-optional
FabianVowie:feature/restructure-pipelines
FabianVowie:feature/start-readme
FabianVowie:feature/update-config-output-format-options
FabianVowie:feature/update-gitignore
FabianVowie:feature/update-route-registration
FabianVowie:main
FabianVowie:pr-feature/add-authorization
FabianVowie:pr-feature/add-commit-hash-to-index-metadata
FabianVowie:pr-feature/add-executable-steps
FabianVowie:pr-feature/add-get-files-endpoints
FabianVowie:pr-feature/add-github-ci
FabianVowie:pr-feature/add-http-image-upload
FabianVowie:pr-feature/add-image-processing
FabianVowie:pr-feature/add-logging
FabianVowie:pr-feature/add-metadata-endpoint
FabianVowie:pr-feature/add-more-image-steps
FabianVowie:pr-feature/add-pipeline-endpoints
FabianVowie:pr-feature/add-pipeline-loading
FabianVowie:pr-feature/add-pipelines-to-metadata
FabianVowie:pr-feature/add-rate-limiting
FabianVowie:pr-feature/add-rate-limiting-to-readme
FabianVowie:pr-feature/add-settings
FabianVowie:pr-feature/add-storage-layer
FabianVowie:pr-feature/enhance-api-responses
FabianVowie:pr-feature/fix-routes
FabianVowie:pr-feature/general-cleanup
FabianVowie:pr-feature/improve-code-stability
FabianVowie:pr-feature/improve-pipeline-abstraction
FabianVowie:pr-feature/make-middlewares-optional
FabianVowie:pr-feature/restructure-pipelines
FabianVowie:pr-feature/start-readme
FabianVowie:pr-feature/update-config-output-format-options
FabianVowie:pr-feature/update-gitignore
FabianVowie:pr-feature/update-route-registration
pull from: FabianVowie:feature/add-settings
FabianVowie:feature/add-authorization
FabianVowie:feature/add-commit-hash-to-index-metadata
FabianVowie:feature/add-executable-steps
FabianVowie:feature/add-get-files-endpoints
FabianVowie:feature/add-github-ci
FabianVowie:feature/add-http-image-upload
FabianVowie:feature/add-image-processing
FabianVowie:feature/add-logging
FabianVowie:feature/add-metadata-endpoint
FabianVowie:feature/add-more-image-steps
FabianVowie:feature/add-pipeline-endpoints
FabianVowie:feature/add-pipeline-loading
FabianVowie:feature/add-pipelines-to-metadata
FabianVowie:feature/add-rate-limiting
FabianVowie:feature/add-rate-limiting-to-readme
FabianVowie:feature/add-settings
FabianVowie:feature/add-storage-layer
FabianVowie:feature/fix-routes
FabianVowie:feature/general-cleanup
FabianVowie:feature/improve-code-stability
FabianVowie:feature/improve-pipeline-abstraction
FabianVowie:feature/make-middlewares-optional
FabianVowie:feature/restructure-pipelines
FabianVowie:feature/start-readme
FabianVowie:feature/update-config-output-format-options
FabianVowie:feature/update-gitignore
FabianVowie:feature/update-route-registration
FabianVowie:main
FabianVowie:pr-feature/add-authorization
FabianVowie:pr-feature/add-commit-hash-to-index-metadata
FabianVowie:pr-feature/add-executable-steps
FabianVowie:pr-feature/add-get-files-endpoints
FabianVowie:pr-feature/add-github-ci
FabianVowie:pr-feature/add-http-image-upload
FabianVowie:pr-feature/add-image-processing
FabianVowie:pr-feature/add-logging
FabianVowie:pr-feature/add-metadata-endpoint
FabianVowie:pr-feature/add-more-image-steps
FabianVowie:pr-feature/add-pipeline-endpoints
FabianVowie:pr-feature/add-pipeline-loading
FabianVowie:pr-feature/add-pipelines-to-metadata
FabianVowie:pr-feature/add-rate-limiting
FabianVowie:pr-feature/add-rate-limiting-to-readme
FabianVowie:pr-feature/add-settings
FabianVowie:pr-feature/add-storage-layer
FabianVowie:pr-feature/enhance-api-responses
FabianVowie:pr-feature/fix-routes
FabianVowie:pr-feature/general-cleanup
FabianVowie:pr-feature/improve-code-stability
FabianVowie:pr-feature/improve-pipeline-abstraction
FabianVowie:pr-feature/make-middlewares-optional
FabianVowie:pr-feature/restructure-pipelines
FabianVowie:pr-feature/start-readme
FabianVowie:pr-feature/update-config-output-format-options
FabianVowie:pr-feature/update-gitignore
FabianVowie:pr-feature/update-route-registration
11 Commits
main
...
feature/ad
Author | SHA1 | Message | Date |
---|---|---|---|
Fabian Vowie |
994677cf34
|
Add tags for json serialization to settings structs
|
3 years ago |
Fabian Vowie |
96436acd89
|
Improve testability for LoadSettings function
|
3 years ago |
Fabian Vowie |
0f16daa99b
|
Add settings.json to .gitignore
|
3 years ago |
Fabian Vowie |
07ae302df9
|
Rename FileSystem in settings to StorageProvider
|
3 years ago |
Fabian Vowie |
48f82f3891
|
Pretty-print default settings json
|
3 years ago |
Fabian Vowie |
9a6bdc2552
|
Remove leftover debug code
|
3 years ago |
Fabian Vowie |
583103c384
|
Load settings on program start or create default if none exist
|
3 years ago |
Fabian Vowie |
fdb4d59448
|
Add settings parsing
|
3 years ago |
Jenkins | f36fb2725c |
Merge commit '08ca3c5b2c25af6eb9e278991e4ea9e7bffb839d' into HEAD
|
3 years ago |
Fabian Vowie |
08ca3c5b2c
|
Temporarily hardcode filesystem storage provider
|
3 years ago |
Fabian Vowie |
44038e1626
|
Fix store methods not being public
|
3 years ago |
7 changed files with 161 additions and 16 deletions
-
5.gitignore
-
25main.go
-
7main_test.go
-
61settings/settings.go
-
48settings/settings_test.go
-
27storage/storage.go
-
4storage/storage_test.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 |
|||
} |
@ -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) |
|||
}) |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue