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
ADDRESS=:8080
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"`
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,

View File

@ -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)
}
return &DB{
dbConn: db,

View File

@ -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())
}

View File

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