Skip to content

Commit a97aa7d

Browse files
committed
Switched to official mongo driver
1 parent 164fd82 commit a97aa7d

File tree

7 files changed

+435
-47
lines changed

7 files changed

+435
-47
lines changed

Gopkg.lock

Lines changed: 257 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Gopkg.toml example
2+
#
3+
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
4+
# for detailed Gopkg.toml documentation.
5+
#
6+
# required = ["github.com/user/thing/cmd/thing"]
7+
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
8+
#
9+
# [[constraint]]
10+
# name = "github.com/user/project"
11+
# version = "1.0.0"
12+
#
13+
# [[constraint]]
14+
# name = "github.com/user/project2"
15+
# branch = "dev"
16+
# source = "github.com/myfork/project2"
17+
#
18+
# [[override]]
19+
# name = "github.com/x/y"
20+
# version = "2.4.0"
21+
#
22+
# [prune]
23+
# non-go = false
24+
# go-tests = true
25+
# unused-packages = true
26+
27+
28+
[[constraint]]
29+
name = "github.com/onsi/ginkgo"
30+
version = "1.7.0"
31+
32+
[[constraint]]
33+
name = "github.com/onsi/gomega"
34+
version = "1.4.3"
35+
36+
[[constraint]]
37+
branch = "master"
38+
name = "github.com/streadway/amqp"
39+
40+
[prune]
41+
go-tests = true
42+
unused-packages = true
43+
44+
[[constraint]]
45+
name = "github.com/gofrs/uuid"
46+
version = "3.2.0"
47+
48+
[[constraint]]
49+
name = "github.com/mongodb/mongo-go-driver"
50+
version = "0.2.0"

cmd/goengine/main.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,55 @@
11
package main
22

33
import (
4+
"context"
45
"os"
6+
"time"
57

68
"github.com/hellofresh/goengine"
79
"github.com/hellofresh/goengine/mongodb"
810
"github.com/hellofresh/goengine/rabbit"
9-
"gopkg.in/mgo.v2"
11+
"github.com/mongodb/mongo-go-driver/mongo"
12+
"github.com/mongodb/mongo-go-driver/mongo/options"
13+
"github.com/mongodb/mongo-go-driver/mongo/readconcern"
14+
"github.com/mongodb/mongo-go-driver/mongo/readpref"
15+
"github.com/mongodb/mongo-go-driver/mongo/writeconcern"
1016
)
1117

1218
func main() {
1319
var streamName goengine.StreamName = "test"
1420

1521
mongoDSN := os.Getenv("STORAGE_DSN")
22+
1623
goengine.Log("Connecting to the database", map[string]interface{}{"dsn": mongoDSN}, nil)
17-
session, err := mgo.Dial(mongoDSN)
24+
mongoClient, err := mongo.NewClientWithOptions(
25+
mongoDSN,
26+
options.Client().
27+
SetAppName("goengine").
28+
SetReadConcern(readconcern.Linearizable()).
29+
SetReadPreference(readpref.Nearest()).
30+
SetWriteConcern(writeconcern.New(writeconcern.WMajority())),
31+
)
32+
if err != nil {
33+
goengine.Log("Failed to create new Mongo mongoClient", nil, err)
34+
panic(err)
35+
}
36+
37+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
38+
defer cancel()
39+
err = mongoClient.Connect(ctx)
1840
if err != nil {
1941
goengine.Log("Failed to connect to Mongo", nil, err)
2042
panic(err)
2143
}
22-
defer session.Close()
2344

24-
// Optional. Switch the session to a monotonic behavior.
25-
session.SetMode(mgo.Monotonic, true)
45+
defer func() {
46+
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
47+
defer cancel()
48+
if err := mongoClient.Disconnect(ctx); err != nil {
49+
goengine.Log("Failed to close connection to Mongo", nil, err)
50+
panic(err)
51+
}
52+
}()
2653

2754
goengine.Log("Setting up the registry", nil, nil)
2855
registry := goengine.NewInMemoryTypeRegistry()
@@ -35,7 +62,7 @@ func main() {
3562
bus := rabbit.NewEventBus(brokerDSN, "events", "events")
3663

3764
goengine.Log("Setting up the event store", nil, nil)
38-
es := mongodb.NewEventStore(session, registry)
65+
es := mongodb.NewEventStore(mongoClient.Database("event_store"), registry)
3966

4067
eventDispatcher := goengine.NewVersionedEventDispatchManager(bus, registry)
4168
eventDispatcher.RegisterEventHandler(&RecipeCreated{}, func(event *goengine.DomainMessage) error {
@@ -47,7 +74,7 @@ func main() {
4774
go eventDispatcher.Listen(stopChannel, false)
4875

4976
goengine.Log("Creating a recipe", nil, nil)
50-
aggregateRoot := CreateScenario(streamName)
77+
aggregateRoot := CreateScenario()
5178

5279
repository := goengine.NewPublisherRepository(es, bus)
5380
repository.Save(aggregateRoot, streamName)
@@ -62,7 +89,7 @@ func main() {
6289
stopChannel <- true
6390
}
6491

65-
func CreateScenario(streamName goengine.StreamName) *Recipe {
92+
func CreateScenario() *Recipe {
6693
recipe := NewRecipe("Test Recipe")
6794
recipe.Rate(4)
6895
return recipe

0 commit comments

Comments
 (0)