Skip to content

Commit

Permalink
feat with init db
Browse files Browse the repository at this point in the history
  • Loading branch information
dyaksa committed Jul 24, 2024
1 parent 987b813 commit c384006
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ func isValidKeySize(key []byte) bool {
return false
}

type Opts func(*Crypto) error

func WithInitHeapConnection() Opts {
return func(c *Crypto) error {
dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
*c.Host, *c.Port, *c.User, *c.Pass, *c.Name)
db, err := sql.Open("postgres", dsn)
if err != nil {
return err
}

if err := db.Ping(); err != nil {
return err
}

c.dbHeapPsql = db
return nil
}
}

type Crypto struct {
AESKey *string `env:"AES_KEY,expand" json:"aes_key"`
HMACKey *string `env:"HMAC_KEY,expand" json:"hmac_key"`
Expand All @@ -53,7 +73,7 @@ type Crypto struct {
keySize AesKeySize
}

func New(keySize AesKeySize) (c *Crypto, err error) {
func New(keySize AesKeySize, opts ...Opts) (c *Crypto, err error) {
c = &Crypto{
keySize: keySize,
}
Expand All @@ -62,6 +82,12 @@ func New(keySize AesKeySize) (c *Crypto, err error) {
return nil, err
}

for _, opt := range opts {
if err = opt(c); err != nil {
return nil, err
}
}

if c.AESKey == nil || c.HMACKey == nil {
return nil, errors.New("key is required")
}
Expand Down

0 comments on commit c384006

Please sign in to comment.