Browse Source

Load settings on program start or create default if none exist

feature/add-settings
Fabian Vowie 3 years ago
parent
commit
583103c384
No known key found for this signature in database GPG Key ID: C27317C33B27C410
  1. 12
      main.go
  2. 33
      settings/settings.go

12
main.go

@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"github.com/geplauder/lithium/pipelines" "github.com/geplauder/lithium/pipelines"
"github.com/geplauder/lithium/settings"
"github.com/geplauder/lithium/storage" "github.com/geplauder/lithium/storage"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@ -42,10 +43,17 @@ func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline, stor
} }
func main() { func main() {
storageProvider := storage.GetFileSystemStorageProvider("test")
settings := settings.LoadSettings()
var storageProvider storage.IStorageProvider
storageProvider.StoreRaw("abc", "def.test", []byte{0x12, 0x10}) storageProvider.StoreRaw("abc", "def.test", []byte{0x12, 0x10})
if settings.FileSystem.Type == 0 {
storageProvider = storage.GetFileSystemStorageProvider(settings.FileSystem.BasePath)
} else {
panic("Invalid file system provided!")
}
pipes := pipelines.LoadPipelines() pipes := pipelines.LoadPipelines()
r := mux.NewRouter() r := mux.NewRouter()
@ -53,7 +61,7 @@ func main() {
RegisterPipelineRoutes(r, pipes, storageProvider) RegisterPipelineRoutes(r, pipes, storageProvider)
err := http.ListenAndServe(":8000", r)
err := http.ListenAndServe(settings.Endpoint, r)
if err != nil { if err != nil {
panic(err) panic(err)
} }

33
settings/settings.go

@ -1,6 +1,10 @@
package settings package settings
import "encoding/json"
import (
"encoding/json"
"os"
"path/filepath"
)
const ( const (
Local FileSystemType = iota Local FileSystemType = iota
@ -26,3 +30,30 @@ func ParseSettings(data []byte) Settings {
return 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.Marshal(defaultSettings)
os.WriteFile(path, serializedSettings, os.ModePerm)
return defaultSettings
}
Loading…
Cancel
Save