Skip to content

Commit 7838e9c

Browse files
author
Hugo Rojas
committed
Add mongoDB as docker container - Update readme.md
1 parent ef34f55 commit 7838e9c

File tree

6 files changed

+53
-25
lines changed

6 files changed

+53
-25
lines changed

Makefile

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
run-containers:
1+
init-containers:
22
docker compose up --build -d
33

44
start-zookeeper:
@@ -11,4 +11,13 @@ run-producer:
1111
go run cmd/producer/main.go
1212

1313
run-consumer:
14-
go run cmd/consumer/main.go
14+
go run cmd/consumer/main.go
15+
16+
stop-containers:
17+
docker compose down --remove-orphans
18+
19+
send-request:
20+
curl --header "Content-Type: application/json" \
21+
--request POST \
22+
--data '{"title": "Golang Boss Engineer in Docker","description": "This a Boss position for Docker","company": "Sarama Docker","salary": "70.000"}' \
23+
http://localhost:9600/jobs

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Phase I
2+
3+
# mark Segmentio messages
4+
5+
# update README.md
6+
7+
Phase II
8+
9+
# Add more features (channels)
10+
11+
1. run `make init-containers`
12+
2. Terminal 1: run `make run-producer`
13+
3. Terminal 1: run `make run-consumer`
14+
4. Terminal 3: run `make send-request`

docker-compose.yml

+22
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@ services:
3838
- zookeeper
3939
networks:
4040
- products_network
41+
mongodb:
42+
image: mongo:latest
43+
restart: always
44+
environment:
45+
MONGODB_DATABASE: dockerKafka
46+
ports:
47+
- 27018:27018
48+
volumes:
49+
- mongodb_data_container:/data/db
50+
networks:
51+
- products_network
52+
# mongo-express:
53+
# image: mongo-express
54+
# restart: always
55+
# ports:
56+
# - 8081:8081
57+
# environment:
58+
# ME_CONFIG_MONGODB_ADMINUSERNAME: root
59+
# ME_CONFIG_MONGODB_ADMINPASSWORD: example
60+
# ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27018/
61+
volumes:
62+
mongodb_data_container:
4163
networks:
4264
products_network:
4365
driver: bridge

pkg/config/env.example

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
PORT=9600
2+
SERVERTIMEOUT = 5
23
KAFKAURL="localhost:9092"
34
KAFKASERVER="bootstrap.servers"
4-
KAFKATOPIC="jobs-confluentic-1"
5+
KAFKATOPIC="jobs-1"
56
KAFKAGROUPID="group-id-1"
67
KAFKARESETPOLICY="earliest"
78
MONGODBURL="localhost:27017"
89
MONGODB="kafkamongo"
910
MONGOCOLLECTION="jobs"
10-
MONGOTIMEOUT=2
11+
MONGOTIMEOUT=5
12+
BROKER="sarama"

pkg/database/init.go

-18
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,9 @@ import (
1212

1313
type DB struct {
1414
client *mongo.Client
15-
// session *mgo.Session
16-
// collection *mgo.Collection
1715
}
1816

1917
func Connect(cf *config.Configuration) (*DB, error) {
20-
// settings := &mgo.DialInfo{
21-
// Addrs: []string{"localhost:27017"},
22-
// Timeout: 3 * time.Second,
23-
// Database: "animals",
24-
// Username: "",
25-
// Password: "",
26-
// }
27-
28-
// s, err := mgo.Dial("mongodb://localhost:27017")
29-
// if err != nil {
30-
// return nil, err
31-
// }
32-
// c := s.DB(cf.MONGODB).C(cf.MONGOCOLLECTION)
33-
34-
// return &DB{session: s, collection: c}, nil
35-
3618
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017"))
3719
if err != nil {
3820
log.Fatal(err)

pkg/database/mongo.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import (
88
)
99

1010
func (db *DB) SaveJob(job *model.Job) error {
11-
// return db.collection.Insert(job)
1211
log.Println("Saving job to MongoDB")
13-
col := db.client.Database("kafka").Collection("jobs")
14-
_, err := col.InsertOne(context.Background(), job)
12+
coll := db.client.Database("dockerKafka").Collection("jobs")
13+
_, err := coll.InsertOne(context.Background(), job)
1514
if err != nil {
1615
return err
1716
}

0 commit comments

Comments
 (0)