From 8725def3a138324764c3675f940fe96121c2d350 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Thu, 4 Nov 2021 20:28:37 +0100 Subject: [PATCH] added connection string to config --- .env | 3 ++- cmd/s3Browser.go | 2 ++ internal/db/db.go | 12 +++++------- internal/s3Broswer.go | 2 +- internal/types/types.go | 1 + 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/.env b/.env index 5e98cd0..72e6f2c 100644 --- a/.env +++ b/.env @@ -5,4 +5,5 @@ S3_SECRET_KEY=testotesto S3_BUCKET=dev S3_DISABLE_SSL=true ADDRESS=:8080 -VERBOSE=true \ No newline at end of file +VERBOSE=true +DB_CONNECTION=s3Browser:hunter2@/s3Browser \ No newline at end of file diff --git a/cmd/s3Browser.go b/cmd/s3Browser.go index 3ae0ecb..3699e93 100644 --- a/cmd/s3Browser.go +++ b/cmd/s3Browser.go @@ -18,6 +18,7 @@ type args struct { CacheTTL int64 `arg:"--cache-ttl,env:CACHE_TTL" help:"Time in seconds" default:"30" placeholder:"TTL"` CacheCleanup int64 `arg:"--cache-cleanup,env:CACHE_CLEANUP" help:"Time in seconds" default:"60" placeholder:"CLEANUP"` Verbose bool `arg:"-v,--verbose,env:VERBOSE" help:"verbosity level" default:"false"` + DBConnection string `arg:"--db,required,env:DB_CONNECTION" help:"DSN in format: https://github.com/go-sql-driver/mysql#dsn-data-source-name"` } func (args) Version() string { @@ -35,6 +36,7 @@ func main() { S3AccessKey: args.S3AccessKey, S3SecretKey: args.S3SecretKey, S3Bucket: args.S3Bucket, + DSN: args.DBConnection, CacheTTL: time.Duration(args.CacheTTL) * time.Second, CacheCleanup: time.Duration(args.CacheCleanup) * time.Second, Address: args.Address, diff --git a/internal/db/db.go b/internal/db/db.go index 8b11e1f..d1b7810 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -19,18 +19,16 @@ type DB struct { dbConn *sql.DB } -func NewDB(driver, dataSourceName string) (*DB, error) { - db, err := sql.Open(driver, dataSourceName) +func NewDB(dataSourceName string) (*DB, error) { + db, err := sql.Open("mysql", dataSourceName) if err != nil { return nil, err } - if driver == "mysql" { - db.SetConnMaxLifetime(time.Minute * 3) - db.SetMaxOpenConns(10) - db.SetMaxIdleConns(10) - } + db.SetConnMaxLifetime(time.Minute * 3) + db.SetMaxOpenConns(10) + db.SetMaxIdleConns(10) return &DB{ dbConn: db, diff --git a/internal/s3Broswer.go b/internal/s3Broswer.go index 7a1e32f..c4dfdb4 100644 --- a/internal/s3Broswer.go +++ b/internal/s3Broswer.go @@ -54,7 +54,7 @@ func Start(config types.AppConfig) { return } - dbStore, err := db.NewDB("mysql", "s3Browser:hunter2@/s3Browser") + dbStore, err := db.NewDB(config.DSN) if err != nil { log.Error("Failed to connect DB: ", err.Error()) } diff --git a/internal/types/types.go b/internal/types/types.go index 0cdaba6..d76df5f 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -13,6 +13,7 @@ type AppConfig struct { S3SecretKey string S3SSL bool S3Bucket string + DSN string CacheTTL time.Duration CacheCleanup time.Duration Address string