Micro-service for file storage and processing written in Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.1 KiB

  1. package settings
  2. import (
  3. "encoding/json"
  4. "os"
  5. "path/filepath"
  6. "github.com/spf13/afero"
  7. )
  8. const (
  9. Local FileSystemType = iota
  10. )
  11. type FileSystemType int
  12. type Settings struct {
  13. Endpoint string
  14. Token string
  15. StorageProvider StorageSettings
  16. }
  17. type StorageSettings struct {
  18. Type FileSystemType
  19. BasePath string
  20. }
  21. func parseSettings(data []byte) Settings {
  22. settings := Settings{}
  23. json.Unmarshal(data, &settings)
  24. return settings
  25. }
  26. func LoadSettings(fileSystem afero.Fs) Settings {
  27. workingDirectory, _ := os.Getwd()
  28. path := filepath.Join(workingDirectory, "settings.json")
  29. // Load file and parse file
  30. data, err := afero.ReadFile(fileSystem, path)
  31. if err == nil {
  32. return parseSettings(data)
  33. }
  34. // If file does not exist, create default settings
  35. defaultSettings := Settings{
  36. Endpoint: "127.0.0.1:8000",
  37. Token: "changeme",
  38. StorageProvider: StorageSettings{
  39. Type: Local,
  40. BasePath: "assets",
  41. },
  42. }
  43. serializedSettings, err := json.MarshalIndent(defaultSettings, "", "\t")
  44. afero.WriteFile(fileSystem, path, serializedSettings, os.ModePerm)
  45. return defaultSettings
  46. }