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
59 lines
1.0 KiB
package settings
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
const (
|
|
Local FileSystemType = iota
|
|
)
|
|
|
|
type FileSystemType int
|
|
|
|
type Settings struct {
|
|
Endpoint string
|
|
Token string
|
|
FileSystem FileSystemSettings
|
|
}
|
|
|
|
type FileSystemSettings struct {
|
|
Type FileSystemType
|
|
BasePath string
|
|
}
|
|
|
|
func ParseSettings(data []byte) Settings {
|
|
settings := Settings{}
|
|
|
|
json.Unmarshal(data, &settings)
|
|
|
|
return settings
|
|
}
|
|
|
|
func LoadSettings() Settings {
|
|
workingDirectory, _ := os.Getwd()
|
|
path := filepath.Join(workingDirectory, "settings.json")
|
|
|
|
// Load file and parse file
|
|
data, err := os.ReadFile(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",
|
|
FileSystem: FileSystemSettings{
|
|
Type: Local,
|
|
BasePath: "assets",
|
|
},
|
|
}
|
|
|
|
serializedSettings, err := json.MarshalIndent(defaultSettings, "", "\t")
|
|
os.WriteFile(path, serializedSettings, os.ModePerm)
|
|
|
|
return defaultSettings
|
|
}
|