connect.go 469 B

12345678910111213141516171819202122232425262728293031
  1. package conn
  2. import (
  3. "context"
  4. "fmt"
  5. "git.giaever.org/bnb.hosting/database/config"
  6. _ "github.com/go-sql-driver/mysql"
  7. "github.com/jmoiron/sqlx"
  8. )
  9. type DB struct {
  10. *sqlx.DB
  11. }
  12. func Connect(ctx context.Context) (*DB, error) {
  13. db, err := sqlx.ConnectContext(ctx, "mysql", fmt.Sprintf(
  14. "%s:%s@%s(%s:%d)/%s%s",
  15. config.USER,
  16. config.PASS,
  17. config.PROTOCOL,
  18. config.HOST,
  19. config.PORT,
  20. config.DATABASE,
  21. config.OPTIONS,
  22. ))
  23. return &DB{
  24. db,
  25. }, err
  26. }