Added ability to load initial storage data

This commit is contained in:
Pijus Kamandulis 2024-02-14 20:46:40 +02:00
parent a656f33009
commit 62ac9ddf6b
5 changed files with 45 additions and 0 deletions

View File

@ -13,6 +13,7 @@ func ParseFlags() {
explorerPath := flag.String("ExplorerDir", "/home/pk/pro/cosmos-explorer/dist", "Path to cosmos-explorer files") explorerPath := flag.String("ExplorerDir", "/home/pk/pro/cosmos-explorer/dist", "Path to cosmos-explorer files")
tlsCertificatePath := flag.String("Cert", "../example.crt", "Hostname") tlsCertificatePath := flag.String("Cert", "../example.crt", "Hostname")
tlsCertificateKey := flag.String("CertKey", "../example.key", "Hostname") tlsCertificateKey := flag.String("CertKey", "../example.key", "Hostname")
initialDataPath := flag.String("InitialData", "", "Path to JSON containing initial state")
flag.Parse() flag.Parse()
@ -21,6 +22,7 @@ func ParseFlags() {
Config.ExplorerPath = *explorerPath Config.ExplorerPath = *explorerPath
Config.TLS_CertificatePath = *tlsCertificatePath Config.TLS_CertificatePath = *tlsCertificatePath
Config.TLS_CertificateKey = *tlsCertificateKey Config.TLS_CertificateKey = *tlsCertificateKey
Config.DataFilePath = *initialDataPath
Config.DatabaseAccount = Config.Host Config.DatabaseAccount = Config.Host
Config.DatabaseDomain = Config.Host Config.DatabaseDomain = Config.Host

View File

@ -10,4 +10,5 @@ type ServerConfig struct {
Host string Host string
TLS_CertificatePath string TLS_CertificatePath string
TLS_CertificateKey string TLS_CertificateKey string
DataFilePath string
} }

View File

@ -0,0 +1,31 @@
package repositories
import (
"encoding/json"
"fmt"
"log"
"os"
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
)
func LoadStateFS(filePath string) {
data, err := os.ReadFile(filePath)
if err != nil {
log.Fatalf("Error reading state JSON file: %v", err)
}
var state repositorymodels.State
if err := json.Unmarshal(data, &state); err != nil {
log.Fatalf("Error unmarshalling state JSON: %v", err)
}
fmt.Println("Loaded state:")
fmt.Printf("Databases: %d\n", len(state.Databases))
fmt.Printf("Collections: %d\n", len(state.Collections))
fmt.Printf("Documents: %d\n", len(state.Documents))
databases = state.Databases
collections = state.Collections
documents = state.Documents
}

View File

@ -110,3 +110,9 @@ type PartitionKeyRange struct {
Ts int `json:"_ts"` Ts int `json:"_ts"`
Lsn int `json:"lsn"` Lsn int `json:"lsn"`
} }
type State struct {
Databases []Database `json:"databases"`
Collections []Collection `json:"collections"`
Documents []Document `json:"documents"`
}

View File

@ -5,11 +5,16 @@ import (
"github.com/pikami/cosmium/api" "github.com/pikami/cosmium/api"
"github.com/pikami/cosmium/api/config" "github.com/pikami/cosmium/api/config"
"github.com/pikami/cosmium/internal/repositories"
) )
func main() { func main() {
config.ParseFlags() config.ParseFlags()
if config.Config.DataFilePath != "" {
repositories.LoadStateFS(config.Config.DataFilePath)
}
router := api.CreateRouter() router := api.CreateRouter()
router.RunTLS( router.RunTLS(
fmt.Sprintf(":%d", config.Config.Port), fmt.Sprintf(":%d", config.Config.Port),