Load state from '-Persist' path if '-InitialData' not supplied

This commit is contained in:
Pijus Kamandulis 2024-02-27 22:11:33 +02:00
parent 3aeae98404
commit b9e38575bc
3 changed files with 28 additions and 7 deletions

View File

@ -23,9 +23,7 @@ Cosmium is available for the following platforms:
Once downloaded, you can launch Cosmium using the following command: Once downloaded, you can launch Cosmium using the following command:
```sh ```sh
./cosmium-linux-amd64 \ cosmium -Persist "./save.json"
-Persist "./save.json" \
-InitialData "./save.json"
``` ```
Connection String Example: Connection String Example:
@ -51,7 +49,7 @@ To disable SSL and run Cosmium on HTTP instead, you can use the `-DisableTls` fl
* **-DisableAuth**: Disable authentication * **-DisableAuth**: Disable authentication
* **-Host**: Hostname (default "localhost") * **-Host**: Hostname (default "localhost")
* **-InitialData**: Path to JSON containing initial state * **-InitialData**: Path to JSON containing initial state
* **-Persist**: Saves data to the given path on application exit * **-Persist**: Saves data to the given path on application exit (When `-InitialData` argument is not supplied, it will try to load data from path supplied in `-Persist`)
* **-Port**: Listen port (default 8081) * **-Port**: Listen port (default 8081)
These arguments allow you to configure various aspects of Cosmium's behavior according to your requirements. These arguments allow you to configure various aspects of Cosmium's behavior according to your requirements.

View File

@ -7,6 +7,7 @@ import (
"os" "os"
"reflect" "reflect"
"github.com/pikami/cosmium/api/config"
repositorymodels "github.com/pikami/cosmium/internal/repository_models" repositorymodels "github.com/pikami/cosmium/internal/repository_models"
) )
@ -19,15 +20,39 @@ var storeState = repositorymodels.State{
Documents: make(map[string]map[string]map[string]repositorymodels.Document), Documents: make(map[string]map[string]map[string]repositorymodels.Document),
} }
func InitializeRepository() {
if config.Config.InitialDataFilePath != "" {
LoadStateFS(config.Config.InitialDataFilePath)
return
}
if config.Config.PersistDataFilePath != "" {
stat, err := os.Stat(config.Config.PersistDataFilePath)
if err != nil {
return
}
if stat.IsDir() {
fmt.Println("Argument '-Persist' must be a path to file, not a directory.")
os.Exit(1)
}
LoadStateFS(config.Config.PersistDataFilePath)
return
}
}
func LoadStateFS(filePath string) { func LoadStateFS(filePath string) {
data, err := os.ReadFile(filePath) data, err := os.ReadFile(filePath)
if err != nil { if err != nil {
log.Fatalf("Error reading state JSON file: %v", err) log.Fatalf("Error reading state JSON file: %v", err)
return
} }
var state repositorymodels.State var state repositorymodels.State
if err := json.Unmarshal(data, &state); err != nil { if err := json.Unmarshal(data, &state); err != nil {
log.Fatalf("Error unmarshalling state JSON: %v", err) log.Fatalf("Error unmarshalling state JSON: %v", err)
return
} }
fmt.Println("Loaded state:") fmt.Println("Loaded state:")

View File

@ -13,9 +13,7 @@ import (
func main() { func main() {
config.ParseFlags() config.ParseFlags()
if config.Config.InitialDataFilePath != "" { repositories.InitializeRepository()
repositories.LoadStateFS(config.Config.InitialDataFilePath)
}
go api.StartAPI() go api.StartAPI()