Skip to content

Commit 0c4ba6b

Browse files
authored
Merge pull request #9 from Zondax/ezequiel/refactor
Code Refactor
2 parents a6b4d6b + fdc3ba8 commit 0c4ba6b

16 files changed

Lines changed: 655 additions & 78 deletions

File tree

.circleci/config.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version: 2.1
33
jobs:
44
build:
55
docker:
6-
- image: golang:1.15
6+
- image: golang:1.16
77
working_directory: /project
88
steps:
99
- checkout
@@ -12,7 +12,7 @@ jobs:
1212

1313
checks:
1414
docker:
15-
- image: golang:1.15
15+
- image: golang:1.16
1616
steps:
1717
- checkout
1818
- run: make install_lint
@@ -22,7 +22,7 @@ jobs:
2222

2323
integration:
2424
docker:
25-
- image: golang:1.15
25+
- image: golang:1.16
2626
steps:
2727
- checkout
2828
- run:

connections/data_source.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package connections
2+
3+
import (
4+
"context"
5+
"fmt"
6+
ds "github.com/Zondax/zindexer/connections/data_store"
7+
"github.com/coinbase/rosetta-sdk-go/client"
8+
"gorm.io/gorm"
9+
"time"
10+
)
11+
12+
const (
13+
defaultRetryDelay = 30 * time.Second
14+
)
15+
16+
type DataSource struct {
17+
// data sources
18+
DbConn *gorm.DB
19+
RosettaClient *client.APIClient
20+
NodeClient interface{}
21+
DataStore ds.DataStoreClient
22+
// common
23+
Ctx context.Context
24+
RetryDelay time.Duration
25+
}
26+
27+
type SourceOption func(*DataSource)
28+
29+
func NewDataSource(opts ...SourceOption) DataSource {
30+
d := DataSource{
31+
Ctx: context.Background(),
32+
RetryDelay: defaultRetryDelay,
33+
}
34+
for _, opt := range opts {
35+
opt(&d)
36+
}
37+
38+
return d
39+
}
40+
41+
func WithContext(ctx context.Context) SourceOption {
42+
return func(w *DataSource) {
43+
w.Ctx = ctx
44+
}
45+
}
46+
47+
func WithRetryDelay(delay time.Duration) SourceOption {
48+
return func(w *DataSource) {
49+
w.RetryDelay = delay
50+
}
51+
}
52+
53+
func WithDBConnection(dbConn *gorm.DB) SourceOption {
54+
return func(w *DataSource) {
55+
w.DbConn = dbConn
56+
}
57+
}
58+
59+
func WithRosettaClient(client *client.APIClient) SourceOption {
60+
return func(w *DataSource) {
61+
w.RosettaClient = client
62+
}
63+
}
64+
65+
func WithNodeClient(node interface{}) SourceOption {
66+
return func(w *DataSource) {
67+
w.NodeClient = node
68+
}
69+
}
70+
71+
func WithDataStore(cfg ds.DataStoreConfig) SourceOption {
72+
return func(w *DataSource) {
73+
switch cfg.Service {
74+
case ds.MinIOStorage:
75+
c, _ := ds.NewMinioClient(cfg)
76+
w.DataStore = ds.DataStoreClient{Client: c}
77+
return
78+
default:
79+
panic(fmt.Errorf("DataStore with service %s, is not available", cfg.Service))
80+
}
81+
}
82+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package data_store
2+
3+
type IDataStoreClient interface {
4+
GetFile(name string, location string) (*[]byte, error)
5+
UploadFile(name string, dest string) error
6+
List(dir string, prefix string) *[]string
7+
}
8+
9+
type DataStoreClient struct {
10+
Client IDataStoreClient
11+
}
12+
13+
type DataStoreConfig struct {
14+
Url string
15+
User string
16+
Password string
17+
Service string
18+
}

connections/data_store/minio.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package data_store
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"github.com/minio/minio-go/v7"
7+
"github.com/minio/minio-go/v7/pkg/credentials"
8+
"go.uber.org/zap"
9+
"os"
10+
)
11+
12+
const MinIOStorage = "minio"
13+
14+
type MinioClient struct {
15+
client *minio.Client
16+
}
17+
18+
func NewMinioClient(config DataStoreConfig) (MinioClient, error) {
19+
minioClient, err := minio.New(config.Url, &minio.Options{
20+
Creds: credentials.NewStaticV4(config.User, config.Password, ""),
21+
Secure: true,
22+
})
23+
if err != nil {
24+
zap.S().Error(err.Error())
25+
return MinioClient{}, err
26+
}
27+
28+
return MinioClient{client: minioClient}, nil
29+
}
30+
31+
func (c MinioClient) GetClient() *minio.Client {
32+
return c.client
33+
}
34+
35+
func (c MinioClient) GetFile(object string, bucket string) (*[]byte, error) {
36+
obj, err := c.GetClient().GetObject(context.Background(), bucket, object, minio.GetObjectOptions{})
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
buf := new(bytes.Buffer)
42+
_, err = buf.ReadFrom(obj)
43+
if err != nil {
44+
return nil, err
45+
}
46+
47+
data := buf.Bytes()
48+
return &data, nil
49+
}
50+
51+
func (c MinioClient) List(bucket string, prefix string) *[]string {
52+
var list []string
53+
for object := range c.client.ListObjects(context.Background(), bucket, minio.ListObjectsOptions{Prefix: prefix}) {
54+
list = append(list, object.Key)
55+
}
56+
57+
return &list
58+
}
59+
60+
func (c MinioClient) UploadFile(name string, dest string) error {
61+
file, err := os.Open(name)
62+
if err != nil {
63+
return err
64+
}
65+
defer file.Close()
66+
67+
fileStat, err := file.Stat()
68+
if err != nil {
69+
return err
70+
}
71+
72+
_, err = c.client.PutObject(context.Background(), dest, file.Name(), file,
73+
fileStat.Size(), minio.PutObjectOptions{ContentType: "application/octet-stream"})
74+
if err != nil {
75+
return err
76+
}
77+
return nil
78+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package connections
1+
package database
22

33
import (
44
"context"

config.go renamed to connections/database/interface.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
package zindexer
1+
package database
22

33
import (
44
"fmt"
55
"gorm.io/gorm"
66
)
77

8+
type DBConnection interface {
9+
GetDB() *gorm.DB
10+
}
11+
12+
type IDBQueryClient interface {
13+
Connect() error
14+
GetDB() interface{}
15+
}
16+
17+
type DBQueryClient struct {
18+
Client IDBQueryClient
19+
}
20+
821
type DBConnectionParams struct {
922
User string
1023
Password string
@@ -18,10 +31,6 @@ type GraphqlClientParams struct {
1831
Token string
1932
}
2033

21-
type DBConnectionConfig struct {
22-
Gorm *gorm.Config
23-
}
24-
2534
func (p *DBConnectionParams) GetDSN() (string, error) {
2635
return fmt.Sprintf(
2736
"user=%s password=%s dbname=%s host=%s port=%s sslmode=disable",
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
package connections
1+
package database
22

33
import (
44
"fmt"
5-
"github.com/Zondax/zindexer"
65
"gorm.io/driver/postgres"
76
"gorm.io/gorm"
87
)
98

10-
func NewPostgresConnection(params *zindexer.DBConnectionParams, config *zindexer.DBConnectionConfig) (*GormConnection, error) {
9+
type GormConnection struct {
10+
db *gorm.DB
11+
}
12+
13+
type DBConnectionConfig struct {
14+
Gorm *gorm.Config
15+
}
16+
17+
func NewPostgresConnection(params *DBConnectionParams, config *DBConnectionConfig) (*GormConnection, error) {
1118
dsn, err := params.GetDSN()
1219
if err != nil {
1320
return nil, fmt.Errorf("failed to retrieve dsn")
@@ -28,7 +35,7 @@ func (c *GormConnection) GetDB() *gorm.DB {
2835
return c.db
2936
}
3037

31-
func ConnectDB(params zindexer.DBConnectionParams, config zindexer.DBConnectionConfig) (*gorm.DB, error) {
38+
func ConnectDB(params DBConnectionParams, config DBConnectionConfig) (*gorm.DB, error) {
3239
conn, err := NewPostgresConnection(&params, &config)
3340
if err != nil {
3441
return nil, err

connections/pub_sub/interface.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package pub_sub
2+
3+
import (
4+
"encoding/json"
5+
"github.com/ThreeDotsLabs/watermill/message"
6+
)
7+
8+
type IDBSubscriptionClient interface {
9+
Subscribe(query interface{}, handler func(message *json.RawMessage, err error) error) error
10+
Unsubscribe() error
11+
Start() error
12+
Stop() error
13+
}
14+
15+
type ITopicPubSubClient interface {
16+
Subscribe(string, func(messages <-chan *message.Message)) error
17+
Publish(string, *message.Message) error
18+
}
19+
20+
type DBSubscriptionClient struct {
21+
Client IDBSubscriptionClient
22+
}
23+
24+
type TopicPubSubClient struct {
25+
Client ITopicPubSubClient
26+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package connections
1+
package pub_sub
22

33
import (
44
"context"

connections/types.go

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)