Skip to content

Commit

Permalink
Merge pull request #35 from hidevopsio/refactor/decrypt
Browse files Browse the repository at this point in the history
Refactor/decrypt
  • Loading branch information
john-deng authored Aug 20, 2024
2 parents 3cf86aa + e6b6a01 commit 98ca992
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 37 deletions.
2 changes: 1 addition & 1 deletion examples/gorm/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ app:
- actuator
- locale
- logging
- gorm
- redis
- gorm

logging:
level: info
Expand Down
14 changes: 7 additions & 7 deletions starter/bolt/autoconfigure.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ func init() {
}

func (c *boltConfiguration) dataSource() DataSource {
dataSource := GetDataSource()
if !dataSource.IsOpened() {
err := dataSource.Open(c.Properties)
source := GetDataSource()
if !source.IsOpened() {
err := source.Open(c.Properties)
if err != nil {
log.Error(err.Error())
}
}
return dataSource
return source
}

func (c *boltConfiguration) Repository() Repository {
repository := GetRepository()
repository.SetDataSource(c.dataSource())
return repository
r := GetRepository()
r.SetDataSource(c.dataSource())
return r
}
4 changes: 2 additions & 2 deletions starter/etcd/autoconfigure.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func init() {
app.Register(newEtcdConfiguration, new(Properties))
}

// EtcdClient create instance named etcdClient
// Client create instance named etcdClient
func (c *etcdConfiguration) Client() (cli *Client) {
cli = new(Client)
var err error
Expand All @@ -73,7 +73,7 @@ func (c *etcdConfiguration) Client() (cli *Client) {
return
}

// EtcdRepository create instance named etcdRepository
// Repository create instance named etcdRepository
func (c *etcdConfiguration) Repository(cli *Client) Repository {
if cli == nil {
return nil
Expand Down
21 changes: 11 additions & 10 deletions starter/gorm/autoconfigure.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ package gorm
import (
"database/sql"
"fmt"
"github.com/hidevopsio/hiboot-data/utils"
"github.com/hidevopsio/hiboot/pkg/app"
"github.com/hidevopsio/hiboot/pkg/at"
"github.com/hidevopsio/hiboot/pkg/log"
"github.com/hidevopsio/hiboot/pkg/utils/crypto/rsa"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"strings"
Expand Down Expand Up @@ -63,15 +63,13 @@ func (c *configuration) DB() (db *DB, err error) {
}

// create new connection if it is unhealthy
log.Infof("create a new database connection to %v@%v:%v", c.prop.Username, c.prop.Host, c.prop.Port)
var report string
report = fmt.Sprintf("database %v@%v:%v", c.prop.Username, c.prop.Host, c.prop.Port)
log.Infof("create new connection to %v", report)
db = new(DB)
password := c.prop.Password
if c.prop.Config.Decrypt {
var pwd []byte
pwd, err = rsa.DecryptBase64([]byte(password), []byte(c.prop.Config.DecryptKey))
if err == nil {
password = string(pwd)
}
password = utils.Decrypt(password, c.prop.Config.DecryptKey)
}
loc := strings.Replace(c.prop.Loc, "/", "%2F", -1)
databaseName := strings.Replace(c.prop.Database, "-", "_", -1)
Expand All @@ -89,11 +87,14 @@ func (c *configuration) DB() (db *DB, err error) {
)
db.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
log.Errorf("failed to connect db: %v", err)
log.Errorf("failed to connect %v, err: %v", report, err)
return
}
log.Infof("database %v@%v:%v is connected", c.prop.Username, c.prop.Host, c.prop.Port)

c.db = db
if sqlDB.Ping() == nil {
// If the connection is alive, assign the new connection
c.db = db
log.Infof("%v is connected", report)
}
return
}
17 changes: 8 additions & 9 deletions starter/redis/autoconfigure.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ package redis
import (
"context"
"fmt"
"github.com/hidevopsio/hiboot-data/utils"
"github.com/hidevopsio/hiboot/pkg/app"
"github.com/hidevopsio/hiboot/pkg/at"
"github.com/hidevopsio/hiboot/pkg/log"
"github.com/hidevopsio/hiboot/pkg/utils/crypto/rsa"
"github.com/redis/go-redis/v9"
"time"
)
Expand Down Expand Up @@ -69,11 +69,7 @@ func (c *configuration) Client() (cli *Client, err error) {
cli = new(Client)
password := c.prop.Password
if c.prop.Config.Decrypt {
var pwd []byte
pwd, err = rsa.DecryptBase64([]byte(password), []byte(c.prop.Config.DecryptKey))
if err == nil {
password = string(pwd)
}
password = utils.Decrypt(password, c.prop.Config.DecryptKey)
}
redisCli = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%v:%v", c.prop.Host, c.prop.Port),
Expand All @@ -84,9 +80,12 @@ func (c *configuration) Client() (cli *Client, err error) {
log.Errorf("failed to connect to redis server: %v", err)
return
}
log.Infof("redis %v:%v is connected", c.prop.Host, c.prop.Port)
cli.Client = redisCli
c.client = cli
_, err = redisCli.Ping(ctx).Result()
if err == nil {
log.Infof("redis %v:%v is connected", c.prop.Host, c.prop.Port)
cli.Client = redisCli
c.client = cli
}

return
}
16 changes: 8 additions & 8 deletions starter/sqlx/autoconfigure.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ package sqlx
import (
"fmt"
_ "github.com/go-sql-driver/mysql" // MySQL 驱动
"github.com/hidevopsio/hiboot-data/utils"
"github.com/hidevopsio/hiboot/pkg/app"
"github.com/hidevopsio/hiboot/pkg/at"
"github.com/hidevopsio/hiboot/pkg/log"
"github.com/hidevopsio/hiboot/pkg/utils/crypto/rsa"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3" // SQLite 驱动
)
Expand Down Expand Up @@ -71,11 +71,7 @@ func (c *configuration) DB() (db *DB, err error) {
} else {
password := c.prop.Password
if c.prop.Config.Decrypt {
var pwd []byte
pwd, err = rsa.DecryptBase64([]byte(password), []byte(c.prop.Config.DecryptKey))
if err == nil {
password = string(pwd)
}
password = utils.Decrypt(password, c.prop.Config.DecryptKey)
}

dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true",
Expand All @@ -91,8 +87,12 @@ func (c *configuration) DB() (db *DB, err error) {
log.Errorf("failed to connect db: %v", err)
return
}
log.Infof("%v is connected", report)

c.db = db
if sqlDB.Ping() == nil {
// If the connection is alive, assign the new connection
c.db = db
log.Infof("%v is connected", report)
}

return
}
13 changes: 13 additions & 0 deletions utils/decrypt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package utils

import "github.com/hidevopsio/hiboot/pkg/utils/crypto/rsa"

func Decrypt(param string, pk string) (retVal string) {
if len(param) != 0 {
b, err := rsa.DecryptBase64([]byte(param), []byte(pk))
if err == nil {
retVal = string(b)
}
}
return
}

0 comments on commit 98ca992

Please sign in to comment.