From edf6ee66e54acda3b7d4a1b629d1ce276fa7fa62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=A2?= =?UTF-8?q?=D1=83=D1=85=D1=82=D0=B0=D1=80=D0=BE=D0=B2?= Date: Sun, 4 Feb 2024 17:14:06 +0500 Subject: [PATCH] v1.0.4 env --- sea.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/sea.go b/sea.go index a272690..0db2e39 100644 --- a/sea.go +++ b/sea.go @@ -1,8 +1,11 @@ package sea import ( - // "fmt" - _ "github.com/go-sql-driver/mysql" // для связи с mysql + "os" + "fmt" + "errors" + + _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" ) @@ -13,7 +16,17 @@ type Sea struct { } func Connect() (*Sea, error) { - db, err := openDB() + user := os.Getenv("SEA_USER") + if user == "" { + return nil, errors.New("Не установлена переменная SEA_USER") + } + + password := os.Getenv("SEA_PASSWORD") + if password == "" { + return nil, errors.New("Не установлена переменная SEA_PASSWORD") + } + + db, err := openDB(user, password) if err != nil { return nil, err } @@ -21,8 +34,9 @@ func Connect() (*Sea, error) { return &sea, nil } -func openDB() (*sqlx.DB, error) { - db, err := sqlx.Open("mysql", "itman:X753951x@(xigmanas:3306)/sea") +func openDB(user, password string) (*sqlx.DB, error) { + conn := fmt.Sprintf("%s:%s@(xigmanas:3306)/sea", user, password) + db, err := sqlx.Open("mysql", conn) if err != nil { return nil, err }