Skip to content

Commit f4ff057

Browse files
author
Ezequiel Raynaudo
authored
Merge pull request #21 from Zondax/ezequiel/local_data
Add local backend for data store
2 parents 8f1e41d + e2703f1 commit f4ff057

9 files changed

Lines changed: 184 additions & 91 deletions

File tree

.circleci/config.yml

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

.github/workflows/main.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Build jobs
2+
3+
on:
4+
push:
5+
pull_request:
6+
branches:
7+
- main
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v2
15+
with:
16+
submodules: true
17+
- uses: actions/setup-go@v2
18+
with:
19+
go-version: '^1.16'
20+
- name: Build
21+
run: |
22+
make build
23+
24+
checks:
25+
runs-on: ubuntu-latest
26+
steps:
27+
- name: Checkout
28+
uses: actions/checkout@v2
29+
with:
30+
submodules: true
31+
- uses: actions/setup-go@v2
32+
with:
33+
go-version: '^1.16'
34+
- run: make build
35+
- name: ModTidy check
36+
run: make check-modtidy
37+
- name: Lint check
38+
run: |
39+
make install_lint
40+
make lint

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# zindexer - Common Components
22

33
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
4-
[![CircleCI](https://circleci.com/gh/Zondax/zindexer/tree/main.svg?style=shield&circle-token=82a6e7f3ee0cfe2c1438e1f3dc25d337951e9402)](https://circleci.com/gh/Zondax/zindexer/tree/main)
4+
![example workflow](https://github.com/Zondax/zindexer/actions/workflows/main.yml/badge.svg?branch=main)

connections/data_store/builder.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@ package data_store
22

33
import (
44
"fmt"
5+
"go.uber.org/zap"
6+
)
7+
8+
const (
9+
MinIOStorage = "minio"
10+
LocalStorage = "local"
511
)
612

713
func NewDataStoreClient(config DataStoreConfig) (DataStoreClient, error) {
14+
zap.S().Infof("[DataStore] - Creating client for service '%s'", config.Service)
815
switch config.Service {
916
case MinIOStorage:
1017
client, err := newMinioClient(config)
1118
return DataStoreClient{client}, err
19+
case LocalStorage:
20+
client, err := newLocalClient(config)
21+
return DataStoreClient{client}, err
1222
default:
1323
return DataStoreClient{}, fmt.Errorf("unrecognized data store service '%s'", config.Service)
1424
}

connections/data_store/interface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ type IDataStoreClient interface {
44
GetFile(name string, location string) (*[]byte, error)
55
UploadFile(name string, dest string) error
66
List(dir string, prefix string) *[]string
7+
StorageType() string
78
}
89

910
type DataStoreClient struct {
@@ -15,4 +16,5 @@ type DataStoreConfig struct {
1516
User string
1617
Password string
1718
Service string
19+
DataPath string
1820
}

connections/data_store/local.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package data_store
2+
3+
import (
4+
"fmt"
5+
"go.uber.org/zap"
6+
"io"
7+
"io/ioutil"
8+
"os"
9+
"strings"
10+
)
11+
12+
type LocalClient struct {
13+
DataPath string
14+
}
15+
16+
func newLocalClient(config DataStoreConfig) (LocalClient, error) {
17+
return LocalClient{DataPath: config.DataPath}, nil
18+
}
19+
20+
func (c LocalClient) GetFile(object string, bucket string) (*[]byte, error) {
21+
targetObject := fmt.Sprintf("%s/%s/%s", c.DataPath, bucket, object)
22+
data, err := ioutil.ReadFile(targetObject)
23+
if err != nil {
24+
zap.S().Errorf("err when getting object from store: %v", err.Error())
25+
return nil, err
26+
}
27+
28+
return &data, nil
29+
}
30+
31+
func (c LocalClient) List(bucket string, prefix string) *[]string {
32+
var list []string
33+
files, err := os.ReadDir(fmt.Sprintf("%s/%s", c.DataPath, bucket))
34+
if err != nil {
35+
zap.S().Errorf("could not read directory '%s': %v", bucket, err)
36+
return &list
37+
}
38+
39+
for _, file := range files {
40+
fileName := file.Name()
41+
if strings.Contains(fileName, prefix) {
42+
list = append(list, fileName)
43+
}
44+
}
45+
46+
return &list
47+
}
48+
49+
func (c LocalClient) UploadFile(name string, dest string) error {
50+
file, err := os.Open(name)
51+
if err != nil {
52+
return err
53+
}
54+
defer file.Close()
55+
56+
fileStat, err := file.Stat()
57+
if err != nil {
58+
return err
59+
}
60+
61+
if !fileStat.Mode().IsRegular() {
62+
return fmt.Errorf("%s is not a regular file", name)
63+
}
64+
65+
out, err := os.Create(fmt.Sprintf("%s/%s/%s", c.DataPath, dest, name))
66+
if err != nil {
67+
return err
68+
}
69+
defer out.Close()
70+
71+
_, err = io.Copy(out, file)
72+
return err
73+
}
74+
75+
func (c LocalClient) StorageType() string {
76+
return LocalStorage
77+
}

connections/data_store/minio.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"os"
1010
)
1111

12-
const MinIOStorage = "minio"
13-
1412
type MinioClient struct {
1513
client *minio.Client
1614
}
@@ -76,3 +74,7 @@ func (c MinioClient) UploadFile(name string, dest string) error {
7674
}
7775
return nil
7876
}
77+
78+
func (c MinioClient) StorageType() string {
79+
return MinIOStorage
80+
}

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ require (
88
github.com/eapache/queue v1.1.0
99
github.com/gorilla/mux v1.8.0
1010
github.com/hasura/go-graphql-client v0.2.0
11-
github.com/minio/minio-go/v7 v7.0.14
11+
github.com/minio/minio-go/v7 v7.0.15
1212
github.com/prometheus/client_golang v1.11.0
1313
github.com/spf13/cobra v1.1.1
1414
github.com/spf13/viper v1.7.1
1515
go.mongodb.org/mongo-driver v1.7.2
1616
go.uber.org/zap v1.19.1
17-
gorm.io/driver/postgres v1.0.6
18-
gorm.io/gorm v1.21.15
17+
gorm.io/driver/postgres v1.1.2
18+
gorm.io/gorm v1.21.16
1919
)

0 commit comments

Comments
 (0)