Skip to content

Commit

Permalink
chore: add db connection config
Browse files Browse the repository at this point in the history
  • Loading branch information
singhvikash11 committed Aug 11, 2022
1 parent a755ad1 commit 6b4d7ba
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion core/appeal/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func validateAppealDurationConfig(appeal *domain.Appeal, policy *domain.Policy)
}
}

return fmt.Errorf("%s - %v", appeal.Options.Duration, ErrOptionsDurationNotFound)
return ErrOptionsDurationNotFound
}

// MakeAction Approve an approval step
Expand Down
20 changes: 13 additions & 7 deletions internal/store/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package store

import "time"

// Config for database connection
type Config struct {
Host string `mapstructure:"host" default:"localhost"`
User string `mapstructure:"user" default:"postgres"`
Password string `mapstructure:"password" default:""`
Name string `mapstructure:"name" default:"postgres"`
Port string `mapstructure:"port" default:"5432"`
SslMode string `mapstructure:"sslmode" default:"disable"`
LogLevel string `mapstructure:"log_level" default:"info"`
Host string `mapstructure:"host" default:"localhost"`
User string `mapstructure:"user" default:"postgres"`
Password string `mapstructure:"password" default:""`
Name string `mapstructure:"name" default:"postgres"`
Port string `mapstructure:"port" default:"5432"`
SslMode string `mapstructure:"sslmode" default:"disable"`
MaxIdleConns int `yaml:"max_idle_conns" mapstructure:"max_idle_conns" default:"3"`
MaxOpenConns int `yaml:"max_open_conns" mapstructure:"max_open_conns" default:"10"`
ConnMaxLifeTime time.Duration `yaml:"conn_max_life_time" mapstructure:"conn_max_life_time" default:"10ms"`
MaxIdleLifeTime time.Duration `yaml:"idle_max_life_time" mapstructure:"idle_max_life_time" default:"100ms"`
LogLevel string `mapstructure:"log_level" default:"info"`
}
11 changes: 10 additions & 1 deletion internal/store/postgres/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"embed"
"errors"
"fmt"
pg "gorm.io/driver/postgres"
"log"
"net"
"net/url"
Expand All @@ -12,7 +13,6 @@ import (
_ "github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/odpf/guardian/internal/store"
pg "gorm.io/driver/postgres"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -41,6 +41,15 @@ func NewStore(c *store.Config) (*Store, error) {
log.Panic(err)
}

db, err := gormDB.DB()
if err != nil {
log.Panic(err)
}
db.SetConnMaxLifetime(c.ConnMaxLifeTime)
db.SetMaxIdleConns(c.MaxIdleConns)
db.SetMaxOpenConns(c.MaxOpenConns)
db.SetConnMaxIdleTime(c.MaxIdleLifeTime)

return &Store{gormDB, c}, nil
}

Expand Down

0 comments on commit 6b4d7ba

Please sign in to comment.