added connection string to config

This commit is contained in:
Djeeberjr 2021-11-04 20:28:37 +01:00
parent 9be7b6c18f
commit 8725def3a1
5 changed files with 11 additions and 9 deletions

1
.env
View File

@ -6,3 +6,4 @@ S3_BUCKET=dev
S3_DISABLE_SSL=true S3_DISABLE_SSL=true
ADDRESS=:8080 ADDRESS=:8080
VERBOSE=true VERBOSE=true
DB_CONNECTION=s3Browser:hunter2@/s3Browser

View File

@ -18,6 +18,7 @@ type args struct {
CacheTTL int64 `arg:"--cache-ttl,env:CACHE_TTL" help:"Time in seconds" default:"30" placeholder:"TTL"` 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"` 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"` 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 { func (args) Version() string {
@ -35,6 +36,7 @@ func main() {
S3AccessKey: args.S3AccessKey, S3AccessKey: args.S3AccessKey,
S3SecretKey: args.S3SecretKey, S3SecretKey: args.S3SecretKey,
S3Bucket: args.S3Bucket, S3Bucket: args.S3Bucket,
DSN: args.DBConnection,
CacheTTL: time.Duration(args.CacheTTL) * time.Second, CacheTTL: time.Duration(args.CacheTTL) * time.Second,
CacheCleanup: time.Duration(args.CacheCleanup) * time.Second, CacheCleanup: time.Duration(args.CacheCleanup) * time.Second,
Address: args.Address, Address: args.Address,

View File

@ -19,18 +19,16 @@ type DB struct {
dbConn *sql.DB dbConn *sql.DB
} }
func NewDB(driver, dataSourceName string) (*DB, error) { func NewDB(dataSourceName string) (*DB, error) {
db, err := sql.Open(driver, dataSourceName) db, err := sql.Open("mysql", dataSourceName)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if driver == "mysql" {
db.SetConnMaxLifetime(time.Minute * 3) db.SetConnMaxLifetime(time.Minute * 3)
db.SetMaxOpenConns(10) db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10) db.SetMaxIdleConns(10)
}
return &DB{ return &DB{
dbConn: db, dbConn: db,

View File

@ -54,7 +54,7 @@ func Start(config types.AppConfig) {
return return
} }
dbStore, err := db.NewDB("mysql", "s3Browser:hunter2@/s3Browser") dbStore, err := db.NewDB(config.DSN)
if err != nil { if err != nil {
log.Error("Failed to connect DB: ", err.Error()) log.Error("Failed to connect DB: ", err.Error())
} }

View File

@ -13,6 +13,7 @@ type AppConfig struct {
S3SecretKey string S3SecretKey string
S3SSL bool S3SSL bool
S3Bucket string S3Bucket string
DSN string
CacheTTL time.Duration CacheTTL time.Duration
CacheCleanup time.Duration CacheCleanup time.Duration
Address string Address string