From c6fc0070cf851bb6db01fb9685884b3511a77529 Mon Sep 17 00:00:00 2001 From: pigwin Date: Mon, 6 Jan 2025 20:47:55 +0000 Subject: [PATCH] add Valkey configuration and connection management --- config/conf.json | 6 +++ config/config.go | 26 ++++++++++- config/valkey.go | 93 +++++++++++++++++++++++++++++++++++++++ database/EstimatedCall.go | 4 ++ go.mod | 5 +++ go.sum | 4 ++ valkey/commands.go | 26 +++++++++++ 7 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 config/valkey.go create mode 100644 valkey/commands.go diff --git a/config/conf.json b/config/conf.json index eefb956..f1b5b1f 100644 --- a/config/conf.json +++ b/config/conf.json @@ -7,5 +7,11 @@ "dbname": "ti1", "sslmode": "disable" }, + "valkey": { + "host": "127.0.0.1", + "port": "6379", + "max_conns": 50, + "timeout_ms": 5000 + }, "temp": "value" } \ No newline at end of file diff --git a/config/config.go b/config/config.go index f59e215..1561e79 100644 --- a/config/config.go +++ b/config/config.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "os" + "strconv" ) type Config struct { @@ -15,6 +16,12 @@ type Config struct { DBName string `json:"dbname"` SSLMode string `json:"sslmode"` } `json:"database"` + Valkey struct { + Host string `json:"host"` + Port string `json:"port"` + MaxConns int `json:"max_conns"` + TimeoutMs int `json:"timeout_ms"` + } `json:"valkey"` Temp string `json:"temp"` } @@ -53,7 +60,24 @@ func LoadConfig(file string) (Config, error) { if temp := os.Getenv("TEMP"); temp != "" { config.Temp = temp } - //log.Println("Temp value:", config.Temp) + + // Override Valkey settings with environment variables + if valkeyHost := os.Getenv("VALKEY_HOST"); valkeyHost != "" { + config.Valkey.Host = valkeyHost + } + if valkeyPort := os.Getenv("VALKEY_PORT"); valkeyPort != "" { + config.Valkey.Port = valkeyPort + } + if maxConns := os.Getenv("VALKEY_MAX_CONNS"); maxConns != "" { + if val, err := strconv.Atoi(maxConns); err == nil { + config.Valkey.MaxConns = val + } + } + if timeoutMs := os.Getenv("VALKEY_TIMEOUT_MS"); timeoutMs != "" { + if val, err := strconv.Atoi(timeoutMs); err == nil { + config.Valkey.TimeoutMs = val + } + } return config, nil } diff --git a/config/valkey.go b/config/valkey.go new file mode 100644 index 0000000..343e2b6 --- /dev/null +++ b/config/valkey.go @@ -0,0 +1,93 @@ +package config + +import ( + "context" + "encoding/json" + "fmt" + "log" + "os" + "strconv" + "time" + + "github.com/valkey-io/valkey-go" +) + +type ValkeyConfig struct { + Host string `json:"host"` + Port string `json:"port"` + MaxConns int `json:"max_conns"` + TimeoutMs int `json:"timeout_ms"` +} + +func LoadValkeyConfig(file string) (ValkeyConfig, error) { + var config ValkeyConfig + configFile, err := os.Open(file) + if err != nil { + return config, fmt.Errorf("failed to open config file: %w", err) + } + defer configFile.Close() + + if err := json.NewDecoder(configFile).Decode(&config); err != nil { + return config, fmt.Errorf("failed to parse Valkey config: %w", err) + } + + // Override with environment variables if set + if host := os.Getenv("VALKEY_HOST"); host != "" { + config.Host = host + } + if port := os.Getenv("VALKEY_PORT"); port != "" { + config.Port = port + } + if maxConns := os.Getenv("VALKEY_MAX_CONNS"); maxConns != "" { + if val, err := strconv.Atoi(maxConns); err == nil { + config.MaxConns = val + } + } + if timeoutMs := os.Getenv("VALKEY_TIMEOUT_MS"); timeoutMs != "" { + if val, err := strconv.Atoi(timeoutMs); err == nil { + config.TimeoutMs = val + } + } + + return config, nil +} + +func ConnectToValkey(configPath string) (valkey.Client, error) { + fmt.Println("Loading Valkey configuration...") + valkeyConfig, err := LoadValkeyConfig(configPath) + if err != nil { + return nil, fmt.Errorf("failed to load Valkey config: %v", err) + } + fmt.Println("Valkey configuration loaded successfully!") + + // Setup Valkey client options + options := valkey.ClientOption{ + InitAddress: []string{fmt.Sprintf("%s:%s", valkeyConfig.Host, valkeyConfig.Port)}, + // Additional options can be added here if required + } + + fmt.Printf("Connecting to Valkey at %s:%s...\n", valkeyConfig.Host, valkeyConfig.Port) + client, err := valkey.NewClient(options) + if err != nil { + return nil, fmt.Errorf("failed to connect to Valkey: %v", err) + } + + // Optionally, perform a ping to validate the connection + ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(valkeyConfig.TimeoutMs)) + defer cancel() + + if err := client.Do(ctx, client.B().Ping().Build()).Error(); err != nil { + client.Close() + return nil, fmt.Errorf("failed to ping Valkey: %v", err) + } + + log.Println("Connected to Valkey successfully!") + return client, nil +} + +func DisconnectFromValkey(client valkey.Client) error { + fmt.Println("Disconnecting from Valkey...") + client.Close() + log.Println("Disconnected from Valkey successfully!") + return nil +} diff --git a/database/EstimatedCall.go b/database/EstimatedCall.go index 56c797b..6cd627c 100644 --- a/database/EstimatedCall.go +++ b/database/EstimatedCall.go @@ -26,6 +26,10 @@ func InsertOrUpdateEstimatedCall(db *sql.DB, values []interface{}) (int, string, hashString := hex.EncodeToString(hash[:]) println(hashString) + estimatedVehicleJourneyID := values[0] + orderID := values[1] + fmt.Printf("Estimated Vehicle Journey ID: %v, Order ID: %v\n", estimatedVehicleJourneyID, orderID) + query := ` INSERT INTO calls ( estimatedvehiclejourney, "order", stoppointref, diff --git a/go.mod b/go.mod index 2e16502..4db7bea 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,8 @@ module ti1 go 1.23.4 require github.com/lib/pq v1.10.9 + +require ( + github.com/valkey-io/valkey-go v1.0.52 // indirect + golang.org/x/sys v0.24.0 // indirect +) diff --git a/go.sum b/go.sum index aeddeae..4ba6c43 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,6 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/valkey-io/valkey-go v1.0.52 h1:ojrR736satGucqpllYzal8fUrNNROc11V10zokAyIYg= +github.com/valkey-io/valkey-go v1.0.52/go.mod h1:BXlVAPIL9rFQinSFM+N32JfWzfCaUAqBpZkc4vPY6fM= +golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= +golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/valkey/commands.go b/valkey/commands.go new file mode 100644 index 0000000..97e1126 --- /dev/null +++ b/valkey/commands.go @@ -0,0 +1,26 @@ +package valkey + +import ( + "context" + "fmt" + + "github.com/valkey-io/valkey-go" +) + +func SetValkeyValue(client valkey.Client, key, value string) error { + ctx := context.Background() + err := client.Do(ctx, client.B().Set().Key(key).Value(value).Build()).Error() + if err != nil { + return fmt.Errorf("failed to set value in Valkey: %v", err) + } + return nil +} + +func GetValkeyValue(client valkey.Client, key string) (string, error) { + ctx := context.Background() + value, err := client.Do(ctx, client.B().Get().Key(key).Build()).ToString() + if err != nil { + return "", fmt.Errorf("failed to get value from Valkey: %v", err) + } + return value, nil +}