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.

59 lines
1.0 KiB

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