The following steps cover writing and running your application:
- From your Terminal or console application, create a new directory called ~/projects/go-programming-cookbook/chapter11/orchestrate and navigate to it.
- Run the following command:
$ go mod init github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter11/orchestrate
You should see a file called go.mod that contains the following content:
module github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter11/orchestrate
- Copy the tests from ~/projects/go-programming-cookbook-original/chapter11/orchestrate, or use this as an opportunity to write some of your own code!
- Create a file called Dockerfile with the following content:
FROM golang:1.12.4-alpine3.9
ENV GOPATH /code/
ADD . /code/src/github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter11/docker
WORKDIR /code/src/github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter11/docker/example
RUN GO111MODULE=on GOPROXY=off go build -mod=vendor
ENTRYPOINT /code/src/github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter11/docker/example/example
- Create a file called docker-compose.yml with the following content:
version: '2'
services:
app:
build: .
mongodb:
image: "mongo:latest"
- Create a file called config.go with the following content:
package mongodb
import (
"context"
"fmt"
"time"
"github.com/mongodb/mongo-go-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Setup initializes a mongo client
func Setup(ctx context.Context, address string) (*mongo.Client, error) {
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
fmt.Println(address)
client, err := mongo.NewClient(options.Client().ApplyURI(address))
if err != nil {
return nil, err
}
if err := client.Connect(ctx); err != nil {
return nil, err
}
return client, nil
}
- Create a file called exec.go with the following content:
package mongodb
import (
"context"
"fmt"
"github.com/mongodb/mongo-go-driver/bson"
)
// State is our data model
type State struct {
Name string `bson:"name"`
Population int `bson:"pop"`
}
// Exec creates then queries an Example
func Exec(address string) error {
ctx := context.Background()
db, err := Setup(ctx, address)
if err != nil {
return err
}
conn := db.Database("gocookbook").Collection("example")
vals := []interface{}{&State{"Washington", 7062000}, &State{"Oregon", 3970000}}
// we can inserts many rows at once
if _, err := conn.InsertMany(ctx, vals); err != nil {
return err
}
var s State
if err := conn.FindOne(ctx, bson.M{"name": "Washington"}).Decode(&s); err != nil {
return err
}
if err := conn.Drop(ctx); err != nil {
return err
}
fmt.Printf("State: %#v ", s)
return nil
}
- Create a new directory named example and navigate to it.
- Create a main.go file with the following content:
package main
import mongodb "github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter11/orchestrate"
func main() {
if err := mongodb.Exec("mongodb://mongodb:27017"); err != nil {
panic(err)
}
}
- Navigate back to the starting directory.
- Run the go mod vendor command.
- Run the docker-compose up -d command.
- Run the docker logs orchestrate_app_1 command. You should now see the following output:
$ docker logs orchestrate_app_1
State: docker.State{Name:"Washington", Population:7062000}
- The go.mod file may be updated and the go.sum file should now be present in the top-level recipe directory.
- If you copied or wrote your own tests, go up one directory and run go test. Ensure that all the tests pass.