Add Postgres and MySQL connection strings (#429)

* Add ConnectionURI to Postgres

* Add ConnectionURI to MySQL

* Update Postgres sample db name
This commit is contained in:
Kalissaac
2022-08-14 22:48:29 -07:00
committed by GitHub
parent d27ddd4739
commit c38c925975
5 changed files with 64 additions and 18 deletions

View File

@@ -48,11 +48,23 @@ store := mysql.New(mysql.Config{
Reset: false, Reset: false,
GCInterval: 10 * time.Second, 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 ### Config
```go ```go
type Config struct { 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 // Host name where the DB is hosted
// //
// Optional. Default is "127.0.0.1" // Optional. Default is "127.0.0.1"
@@ -98,6 +110,7 @@ type Config struct {
### Default Config ### Default Config
```go ```go
var ConfigDefault = Config{ var ConfigDefault = Config{
ConnectionURI: "",
Host: "127.0.0.1", Host: "127.0.0.1",
Port: 3306, Port: 3306,
Database: "fiber", Database: "fiber",

View File

@@ -7,6 +7,11 @@ import (
// Config defines the config for storage. // Config defines the config for storage.
type Config struct { 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 // Host name where the DB is hosted
// //
// Optional. Default is "127.0.0.1" // Optional. Default is "127.0.0.1"
@@ -58,6 +63,7 @@ type Config struct {
// ConfigDefault is the default config // ConfigDefault is the default config
var ConfigDefault = Config{ var ConfigDefault = Config{
ConnectionURI: "",
Host: "127.0.0.1", Host: "127.0.0.1",
Port: 3306, Port: 3306,
Database: "fiber", Database: "fiber",
@@ -70,6 +76,9 @@ var ConfigDefault = Config{
} }
func (c Config) dsn() string { 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) return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", c.Username, c.Password, c.Host, c.Port, c.Database)
} }

View File

@@ -49,12 +49,24 @@ store := postgres.New(postgres.Config{
GCInterval: 10 * time.Second, GCInterval: 10 * time.Second,
SslMode: "disable", 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 ### Config
```go ```go
// Config defines the config for storage. // Config defines the config for storage.
type Config struct { 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 // Host name where the DB is hosted
// //
// Optional. Default is "127.0.0.1" // Optional. Default is "127.0.0.1"
@@ -105,6 +117,7 @@ type Config struct {
### Default Config ### Default Config
```go ```go
var ConfigDefault = Config{ var ConfigDefault = Config{
ConnectionURI: "",
Host: "127.0.0.1", Host: "127.0.0.1",
Port: 5432, Port: 5432,
Database: "fiber", Database: "fiber",

View File

@@ -6,6 +6,11 @@ import (
// Config defines the config for storage. // Config defines the config for storage.
type Config struct { 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 // Host name where the DB is hosted
// //
// Optional. Default is "127.0.0.1" // Optional. Default is "127.0.0.1"
@@ -90,6 +95,7 @@ type Config struct {
// ConfigDefault is the default config // ConfigDefault is the default config
var ConfigDefault = Config{ var ConfigDefault = Config{
ConnectionURI: "",
Host: "127.0.0.1", Host: "127.0.0.1",
Port: 5432, Port: 5432,
Database: "fiber", Database: "fiber",

View File

@@ -46,26 +46,31 @@ func New(config ...Config) *Storage {
cfg := configDefault(config...) cfg := configDefault(config...)
// Create data source name // Create data source name
var dsn string = "postgresql://" var dsn string
if cfg.Username != "" { if cfg.ConnectionURI != "" {
dsn += url.QueryEscape(cfg.Username) dsn = cfg.ConnectionURI
}
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 { } 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 // Create db
db, err := sql.Open("postgres", dsn) db, err := sql.Open("postgres", dsn)