mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-24 13:48:49 +08:00

* Initial Commit: Sync Env Veriables With Docker Deployment Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Sync Env Vars With Master Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Remove Altprefix Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Rename HttpPort to HTTPPort Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix envPrefixDB After Rebase Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Remove Server Parse Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Add Provision For TLS on CoAP Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Fix Exit After Defer Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Remove Unused Function Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Document Undocumentated Env Variables Signed-off-by: rodneyosodo <blackd0t@protonmail.com> --------- Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Co-authored-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>
90 lines
3.1 KiB
Go
90 lines
3.1 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package postgres
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
_ "github.com/jackc/pgx/v5/stdlib" // required for SQL access
|
|
"github.com/jmoiron/sqlx"
|
|
"github.com/mainflux/mainflux/internal/env"
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
|
migrate "github.com/rubenv/sql-migrate"
|
|
)
|
|
|
|
var (
|
|
errConfig = errors.New("failed to load postgresql configuration")
|
|
errConnect = errors.New("failed to connect to postgresql server")
|
|
errMigration = errors.New("failed to apply migrations")
|
|
)
|
|
|
|
type Config struct {
|
|
Host string `env:"HOST,notEmpty" envDefault:"localhost"`
|
|
Port string `env:"PORT,notEmpty" envDefault:"5432"`
|
|
User string `env:"USER,notEmpty" envDefault:"mainflux"`
|
|
Pass string `env:"PASS,notEmpty" envDefault:"mainflux"`
|
|
Name string `env:"NAME" envDefault:""`
|
|
SSLMode string `env:"SSL_MODE,notEmpty" envDefault:"disable"`
|
|
SSLCert string `env:"SSL_CERT" envDefault:""`
|
|
SSLKey string `env:"SSL_KEY" envDefault:""`
|
|
SSLRootCert string `env:"SSL_ROOT_CERT" envDefault:""`
|
|
}
|
|
|
|
// Setup creates a connection to the PostgreSQL instance and applies any
|
|
// unapplied database migrations. A non-nil error is returned to indicate failure.
|
|
func Setup(prefix string, migrations migrate.MemoryMigrationSource) (*sqlx.DB, error) {
|
|
return SetupWithConfig(prefix, migrations, Config{})
|
|
}
|
|
|
|
// SetupWithConfig creates a connection to the PostgreSQL instance and applies any
|
|
// unapplied database migrations. A non-nil error is returned to indicate failure.
|
|
func SetupWithConfig(prefix string, migrations migrate.MemoryMigrationSource, defConfig Config) (*sqlx.DB, error) {
|
|
cfg := defConfig
|
|
if err := env.Parse(&cfg, env.Options{Prefix: prefix}); err != nil {
|
|
return nil, errors.Wrap(errConfig, err)
|
|
}
|
|
return SetupDB(cfg, migrations)
|
|
}
|
|
|
|
// SetupDB creates a connection to the PostgreSQL instance and applies any
|
|
// unapplied database migrations. A non-nil error is returned to indicate failure.
|
|
func SetupDB(cfg Config, migrations migrate.MemoryMigrationSource) (*sqlx.DB, error) {
|
|
db, err := Connect(cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := MigrateDB(db, migrations); err != nil {
|
|
return nil, err
|
|
}
|
|
return db, nil
|
|
}
|
|
|
|
// Connect creates a connection to the PostgreSQL instance.
|
|
func Connect(cfg Config) (*sqlx.DB, error) {
|
|
url := fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=%s sslcert=%s sslkey=%s sslrootcert=%s", cfg.Host, cfg.Port, cfg.User, cfg.Name, cfg.Pass, cfg.SSLMode, cfg.SSLCert, cfg.SSLKey, cfg.SSLRootCert)
|
|
|
|
db, err := sqlx.Open("pgx", url)
|
|
if err != nil {
|
|
return nil, errors.Wrap(errConnect, err)
|
|
}
|
|
|
|
return db, nil
|
|
}
|
|
|
|
// MigrateDB applies any unapplied database migrations.
|
|
func MigrateDB(db *sqlx.DB, migrations migrate.MemoryMigrationSource) error {
|
|
_, err := migrate.Exec(db.DB, "postgres", migrations, migrate.Up)
|
|
if err != nil {
|
|
return errors.Wrap(errMigration, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Config) LoadEnv(prefix string) error {
|
|
if err := env.Parse(c, env.Options{Prefix: prefix}); err != nil {
|
|
return errors.Wrap(errConfig, err)
|
|
}
|
|
return nil
|
|
}
|