From b0c9197ce45cef0dc9ba211e50893ab4bfc8efab Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 8 Jan 2025 10:56:59 +0300 Subject: [PATCH] allow the BuildConnStr to be accessed by different packages --- internal/storage/storage_service.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/internal/storage/storage_service.go b/internal/storage/storage_service.go index 19b9f44..5fb73ce 100644 --- a/internal/storage/storage_service.go +++ b/internal/storage/storage_service.go @@ -36,7 +36,23 @@ type MenuStorageService struct { userDataStore db.Db } -func NewMenuStorageService(conn ConnData, resourceDir string) *MenuStorageService { +func BuildConnStr() string { + host := initializers.GetEnv("DB_HOST", "localhost") + user := initializers.GetEnv("DB_USER", "postgres") + password := initializers.GetEnv("DB_PASSWORD", "") + dbName := initializers.GetEnv("DB_NAME", "") + port := initializers.GetEnv("DB_PORT", "5432") + + connString := fmt.Sprintf( + "postgres://%s:%s@%s:%s/%s", + user, password, host, port, dbName, + ) + logg.Debugf("pg conn string", "conn", connString) + + return connString +} + +func NewMenuStorageService(dbDir string, resourceDir string) *MenuStorageService { return &MenuStorageService{ conn: conn, resourceDir: resourceDir, @@ -58,7 +74,7 @@ func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.D if database == "postgres" { - connStr := buildConnStr() + connStr := BuildConnStr() // Ensure the schema exists err = ensureSchemaExists(ctx, connStr, schema)