From 117ad922e21d5fbce9feadcef67dd73650134e97 Mon Sep 17 00:00:00 2001 From: bel Date: Mon, 13 Apr 2020 02:19:56 +0000 Subject: [PATCH] Create stubs and limit page size --- config/config.go | 3 +++ server/transaction/transaction.go | 4 +++- storage/account.go | 15 +++++++++++++++ storage/balance.go | 17 +++++++++++++++++ storage/mongo.go | 5 +++-- storage/mongo_test.go | 2 +- storage/storage.go | 2 +- storage/transaction.go | 15 +++++++++++++++ 8 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 storage/account.go create mode 100644 storage/balance.go create mode 100644 storage/transaction.go diff --git a/config/config.go b/config/config.go index eddf120..0f6e9e4 100644 --- a/config/config.go +++ b/config/config.go @@ -12,6 +12,7 @@ var ( Public string StoreAddr string StoreNS string + Page int ) func init() { @@ -24,6 +25,7 @@ func New() { } as := args.NewArgSet() as.Append(args.INT, "p", "port to listen on", 52222) + as.Append(args.INT, "page", "page size for requests", 20) as.Append(args.STRING, "d", "dir with public files", "./public") as.Append(args.STRING, "s", "mongodb address", "localhost:27017") as.Append(args.STRING, "ns", "mongodb database", "cheqbooq") @@ -31,6 +33,7 @@ func New() { panic(err) } Port = fmt.Sprintf(":%d", as.GetInt("p")) + Page = as.GetInt("page") Public = as.GetString("d") StoreAddr = as.GetString("s") StoreNS = as.GetString("ns") diff --git a/server/transaction/transaction.go b/server/transaction/transaction.go index 0b0e52c..3d74912 100644 --- a/server/transaction/transaction.go +++ b/server/transaction/transaction.go @@ -3,7 +3,9 @@ package transaction import "encoding/json" type Transaction struct { - Account string `json:"account"` + From string `json:"from"` + To string `json:"to"` + Category string `json:"category"` Amount float32 `json:"amount"` Timestamp int64 `json:"timestamp"` Meta json.RawMessage `json:"meta"` diff --git a/storage/account.go b/storage/account.go new file mode 100644 index 0000000..44da924 --- /dev/null +++ b/storage/account.go @@ -0,0 +1,15 @@ +package storage + +import "errors" + +type Account struct { + ID string `json:"_id"` +} + +func (s *Storage) Accounts(token string) ([]Account, error) { + return nil, errors.New("not impl") +} + +func (s *Storage) PrimaryAccounts(token string) ([]Account, error) { + return nil, errors.New("not impl") +} diff --git a/storage/balance.go b/storage/balance.go new file mode 100644 index 0000000..4e3081c --- /dev/null +++ b/storage/balance.go @@ -0,0 +1,17 @@ +package storage + +import "errors" + +type Balance struct { + ID string `json:"id"` + At int64 `json:"at"` + Is float32 `json:"is"` +} + +func (s *Storage) CurrentBalances(accounts ...Account) ([]Balance, error) { + return nil, errors.New("not impl") +} + +func (s *Storage) BalancesOverTime(from, to int64, accounts ...Account) ([]Balance, error) { + return nil, errors.New("not impl") +} diff --git a/storage/mongo.go b/storage/mongo.go index 2bf0bc5..3bc4b6f 100644 --- a/storage/mongo.go +++ b/storage/mongo.go @@ -13,6 +13,7 @@ import ( type Mongo struct { client *mongo.Client ns string + page int } func init() { @@ -44,7 +45,7 @@ func init() { }() } -func NewMongo(ns, addr string) (*Mongo, error) { +func NewMongo(page int, ns, addr string) (*Mongo, error) { ctx, can := context.WithTimeout(context.Background(), time.Second*5) defer can() @@ -82,7 +83,7 @@ func (m *Mongo) Find(c *mongo.Collection, where interface{}, next func() interfa ctx, can := context.WithCancel(context.TODO()) defer can() - cur, err := c.Find(ctx, where, options.Find()) + cur, err := c.Find(ctx, where, options.Find().SetLimit(int64(m.page))) if err != nil { return err } diff --git a/storage/mongo_test.go b/storage/mongo_test.go index 489a85f..8785e31 100644 --- a/storage/mongo_test.go +++ b/storage/mongo_test.go @@ -10,7 +10,7 @@ import ( func testMongoNew(t *testing.T) *Mongo { b := make([]byte, 5) rand.Read(b) - m, err := NewMongo(base64.URLEncoding.EncodeToString(b), "mongodb://localhost:27017") + m, err := NewMongo(10, base64.URLEncoding.EncodeToString(b), "mongodb://localhost:27017") if err != nil { t.Fatal(err) } diff --git a/storage/storage.go b/storage/storage.go index 752eb55..860e962 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -11,7 +11,7 @@ type Storage struct { } func New() (*Storage, error) { - mongo, err := NewMongo(config.StoreNS, fmt.Sprintf("mongodb://%s", strings.TrimPrefix(config.StoreAddr, "mongodb://"))) + mongo, err := NewMongo(config.Page, config.StoreNS, fmt.Sprintf("mongodb://%s", strings.TrimPrefix(config.StoreAddr, "mongodb://"))) return &Storage{ mongo: mongo, }, err diff --git a/storage/transaction.go b/storage/transaction.go new file mode 100644 index 0000000..bcb004d --- /dev/null +++ b/storage/transaction.go @@ -0,0 +1,15 @@ +package storage + +import "errors" + +type Transaction struct { + From string `json:"from"` + To string `json:"to"` + Category string `json:"category"` + Amount float32 `json:"amount"` + Timestamp int64 `json:"timestamp"` +} + +func (s *Storage) Transactions(from, to int64, accounts ...Account) ([]Transaction, error) { + return nil, errors.New("not impl") +}