diff --git a/mysql/README.md b/mysql/README.md index 3dd33077..dc15f5a7 100644 --- a/mysql/README.md +++ b/mysql/README.md @@ -48,11 +48,23 @@ store := mysql.New(mysql.Config{ Reset: false, GCInterval: 10 * time.Second, }) + +// Initialize custom config using connection string +store := postgres.New(postgres.Config{ + ConnectionURI: "mysql://user:password@localhost:3306/fiber" + Reset: false, + GCInterval: 10 * time.Second, +}) ``` ### Config ```go type Config struct { + // 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" @@ -98,6 +110,7 @@ type Config struct { ### Default Config ```go var ConfigDefault = Config{ + ConnectionURI: "", Host: "127.0.0.1", Port: 3306, Database: "fiber", diff --git a/mysql/config.go b/mysql/config.go index fc97a733..ffdda9a7 100644 --- a/mysql/config.go +++ b/mysql/config.go @@ -7,6 +7,11 @@ import ( // Config defines the config for storage. type Config struct { + // 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" @@ -58,6 +63,7 @@ type Config struct { // ConfigDefault is the default config var ConfigDefault = Config{ + ConnectionURI: "", Host: "127.0.0.1", Port: 3306, Database: "fiber", @@ -70,6 +76,9 @@ var ConfigDefault = Config{ } func (c Config) dsn() string { + if c.ConnectionURI != "" { + return c.ConnectionURI + } return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", c.Username, c.Password, c.Host, c.Port, c.Database) } diff --git a/postgres/README.md b/postgres/README.md index feada1db..03342cd9 100644 --- a/postgres/README.md +++ b/postgres/README.md @@ -49,12 +49,24 @@ store := postgres.New(postgres.Config{ GCInterval: 10 * time.Second, SslMode: "disable", }) + +// Initialize custom config using connection string +store := postgres.New(postgres.Config{ + ConnectionURI: "postgresql://user:password@localhost:5432/fiber" + Reset: false, + GCInterval: 10 * time.Second, +}) ``` ### Config ```go // Config defines the config for storage. type Config struct { + // 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" @@ -105,6 +117,7 @@ type Config struct { ### Default Config ```go var ConfigDefault = Config{ + ConnectionURI: "", Host: "127.0.0.1", Port: 5432, Database: "fiber", diff --git a/postgres/config.go b/postgres/config.go index 02969125..c9354e4a 100644 --- a/postgres/config.go +++ b/postgres/config.go @@ -6,6 +6,11 @@ import ( // Config defines the config for storage. type Config struct { + // 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" @@ -90,6 +95,7 @@ type Config struct { // ConfigDefault is the default config var ConfigDefault = Config{ + ConnectionURI: "", Host: "127.0.0.1", Port: 5432, Database: "fiber", diff --git a/postgres/postgres.go b/postgres/postgres.go index 37f96117..fabfe196 100644 --- a/postgres/postgres.go +++ b/postgres/postgres.go @@ -46,26 +46,31 @@ func New(config ...Config) *Storage { cfg := configDefault(config...) // Create data source name - var dsn string = "postgresql://" - if cfg.Username != "" { - dsn += url.QueryEscape(cfg.Username) - } - if cfg.Password != "" { - dsn += ":" + cfg.Password - } - if cfg.Username != "" || cfg.Password != "" { - dsn += "@" - } - // unix socket host path - if strings.HasPrefix(cfg.Host, "/") { - dsn += fmt.Sprintf("%s:%d", cfg.Host, cfg.Port) + var dsn string + if cfg.ConnectionURI != "" { + dsn = cfg.ConnectionURI } else { - dsn += fmt.Sprintf("%s:%d", url.QueryEscape(cfg.Host), cfg.Port) + dsn = "postgresql://" + if cfg.Username != "" { + dsn += url.QueryEscape(cfg.Username) + } + if cfg.Password != "" { + dsn += ":" + cfg.Password + } + if cfg.Username != "" || cfg.Password != "" { + dsn += "@" + } + // unix socket host path + if strings.HasPrefix(cfg.Host, "/") { + dsn += fmt.Sprintf("%s:%d", cfg.Host, cfg.Port) + } else { + dsn += fmt.Sprintf("%s:%d", url.QueryEscape(cfg.Host), cfg.Port) + } + dsn += fmt.Sprintf("/%s?connect_timeout=%d&sslmode=%s", + url.QueryEscape(cfg.Database), + int64(cfg.timeout.Seconds()), + cfg.SslMode) } - dsn += fmt.Sprintf("/%s?connect_timeout=%d&sslmode=%s", - url.QueryEscape(cfg.Database), - int64(cfg.timeout.Seconds()), - cfg.SslMode) // Create db db, err := sql.Open("postgres", dsn)