From 3a8af0551e4bfa87483e94b80502bf8e1f79b8b8 Mon Sep 17 00:00:00 2001 From: bel Date: Sun, 12 Apr 2020 20:06:42 +0000 Subject: [PATCH] Set up mongodb c --- .gitignore | 1 + config/config.go | 10 ++++-- storage/mongo.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++ storage/storage.go | 7 +++++ 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 storage/mongo.go create mode 100644 storage/storage.go diff --git a/.gitignore b/.gitignore index bf7d2fa..7cbd210 100755 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ cheqbooq exec-cheqbooq **/*.sw* vendor +testdata diff --git a/config/config.go b/config/config.go index 148ded6..691a704 100644 --- a/config/config.go +++ b/config/config.go @@ -6,8 +6,10 @@ import ( ) var ( - Port string - Public string + Port string + Public string + StoreAddr string + StoreNS string ) func init() { @@ -18,9 +20,13 @@ func New() { as := args.NewArgSet() as.Append(args.INT, "p", "port to listen on", 52222) 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") if err := as.Parse(); err != nil { panic(err) } Port = fmt.Sprintf(":%d", as.GetInt("p")) Public = as.GetString("d") + StoreAddr = as.GetString("s") + StoreNS = as.GetString("ns") } diff --git a/storage/mongo.go b/storage/mongo.go new file mode 100644 index 0000000..6f0c036 --- /dev/null +++ b/storage/mongo.go @@ -0,0 +1,77 @@ +package storage + +import ( + "context" + "fmt" + "local/cheqbooq/config" + "strings" + "time" + + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" +) + +type Mongo struct { + client *mongo.Client +} + +func NewMongo() (*Mongo, error) { + ctx, can := context.WithTimeout(context.Background(), time.Second*5) + defer can() + + opt := options.Client() + opt.ApplyURI(fmt.Sprintf("mongodb://%s", strings.TrimPrefix(config.StoreAddr, "mongodb://"))) + + client, err := mongo.Connect(ctx, opt) + if err != nil { + return nil, err + } + + return &Mongo{ + client: client, + }, client.Ping(ctx, nil) +} + +func (m *Mongo) Close() error { + return m.client.Disconnect(context.TODO()) +} + +func (m *Mongo) Account() *mongo.Collection { + return m.Database(config.StoreNS).Collection("account") +} + +func (m *Mongo) Balance() *mongo.Collection { + return m.Database(config.StoreNS).Collection("balance") +} + +func (m *Mongo) Transaction() *mongo.Collection { + return m.Database(config.StoreNS).Collection("transaction") +} + +func (m *Mongo) Find(c *mongo.Collection, where interface{}, next func() interface{}) error { + ctx, can := context.WithCancel(context.TODO()) + defer can() + + cur, err := c.Find(ctx, where, options.Find()) + if err != nil { + return err + } + defer cur.Close(ctx) + + for cur.Next(ctx) { + ptr := next() + if err := cur.Decode(ptr); err != nil { + return err + } + } + + return cur.Err() +} + +func (m *Mongo) Upsert(c *mongo.Collection, where, op interface{}) error { + ctx, can := context.WithCancel(context.TODO()) + defer can() + + _, err := c.UpdateMany(ctx, where, op, options.Update().SetUpsert(true)) + return err +} diff --git a/storage/storage.go b/storage/storage.go new file mode 100644 index 0000000..16f9ebf --- /dev/null +++ b/storage/storage.go @@ -0,0 +1,7 @@ +package storage + +type Storage struct { +} + +func New() (*Storage, error) { +}