v1.0.4 env

main v1.0.4
parent 09cec9c71e
commit edf6ee66e5

@ -1,8 +1,11 @@
package sea package sea
import ( import (
// "fmt" "os"
_ "github.com/go-sql-driver/mysql" // для связи с mysql "fmt"
"errors"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
) )
@ -13,7 +16,17 @@ type Sea struct {
} }
func Connect() (*Sea, error) { 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 { if err != nil {
return nil, err return nil, err
} }
@ -21,8 +34,9 @@ func Connect() (*Sea, error) {
return &sea, nil return &sea, nil
} }
func openDB() (*sqlx.DB, error) { func openDB(user, password string) (*sqlx.DB, error) {
db, err := sqlx.Open("mysql", "itman:X753951x@(xigmanas:3306)/sea") conn := fmt.Sprintf("%s:%s@(xigmanas:3306)/sea", user, password)
db, err := sqlx.Open("mysql", conn)
if err != nil { if err != nil {
return nil, err return nil, err
} }

Loading…
Cancel
Save