v1.0.4 env

main v1.0.4
parent 09cec9c71e
commit edf6ee66e5

@ -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
}

Loading…
Cancel
Save