diff --git a/postgres/v2/config.go b/postgres/v2/config.go index 296f706c..e00cb5b4 100644 --- a/postgres/v2/config.go +++ b/postgres/v2/config.go @@ -1,6 +1,7 @@ package v2 import ( + "fmt" "time" "github.com/jackc/pgx/v5/pgxpool" @@ -8,10 +9,40 @@ import ( // Config defines the config for storage. type Config struct { - // Db pgxpool.Pool object + // DB pgxpool.Pool object will override connection uri and other connection fields // - // Required - Db pgxpool.Pool + // Optional. Default is nil + DB *pgxpool.Pool + + // Connection string to use for DB. Will override all other authentication values if used + // + // Optional. Default is "" + ConnectionURI string + + // Host name where the DB is hosted + // + // Optional. Default is "127.0.0.1" + Host string + + // Port where the DB is listening on + // + // Optional. Default is 5432 + Port int + + // Server username + // + // Optional. Default is "" + Username string + + // Server password + // + // Optional. Default is "" + Password string + + // Database name + // + // Optional. Default is "fiber" + Database string // Table name // @@ -31,9 +62,20 @@ type Config struct { // ConfigDefault is the default config var ConfigDefault = Config{ - Table: "fiber_storage", - Reset: false, - GCInterval: 10 * time.Second, + ConnectionURI: "", + Host: "127.0.0.1", + Port: 3306, + Database: "fiber", + Table: "fiber_storage", + Reset: false, + GCInterval: 10 * time.Second, +} + +func (c Config) dsn() string { + if c.ConnectionURI != "" { + return c.ConnectionURI + } + return fmt.Sprintf("postgres://%s:%s@%s:%d/%s", c.Username, c.Password, c.Host, c.Port, c.Database) } // Helper function to set default values @@ -46,6 +88,18 @@ func configDefault(config ...Config) Config { cfg := config[0] // Set default values + if cfg.Host == "" { + cfg.Host = ConfigDefault.Host + } + if cfg.Port <= 0 { + cfg.Port = ConfigDefault.Port + } + if cfg.Database == "" { + cfg.Database = ConfigDefault.Database + } + if cfg.Table == "" { + cfg.Table = ConfigDefault.Table + } if cfg.Table == "" { cfg.Table = ConfigDefault.Table } diff --git a/postgres/v2/postgres.go b/postgres/v2/postgres.go index ab1afed5..e19da885 100644 --- a/postgres/v2/postgres.go +++ b/postgres/v2/postgres.go @@ -3,17 +3,17 @@ package v2 import ( "context" "database/sql" - "errors" "fmt" - "github.com/jackc/pgx/v5/pgxpool" - "reflect" + "os" "strings" "time" + + "github.com/jackc/pgx/v5/pgxpool" ) // Storage interface that is implemented by storage providers type Storage struct { - db pgxpool.Pool + db *pgxpool.Pool gcInterval time.Duration done chan struct{} @@ -45,10 +45,14 @@ func New(config ...Config) *Storage { // Set default config cfg := configDefault(config...) - db := cfg.Db - - if reflect.ValueOf(db).IsNil() { - panic(errors.New("db pool instance must be passed into the config")) + // Select db connection + var err error + db := cfg.DB + if db == nil { + db, err = pgxpool.New(context.Background(), cfg.dsn()) + if err != nil { + fmt.Fprintf(os.Stderr, "Unable to create connection pool: %v\n", err) + } } // Ping database