Skip to content

Commit e8ea4e6

Browse files
committed
add more code for rabbitmq #3
1 parent 8ed9baa commit e8ea4e6

File tree

12 files changed

+241
-75
lines changed

12 files changed

+241
-75
lines changed

cmd/barista/config.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
app:
2+
name: 'counter-service'
3+
version: '1.0.0'
4+
5+
http:
6+
host: '0.0.0.0'
7+
port: 5002
8+
9+
rabbit_mq:
10+
url: amqp://guest:[email protected]:5672/
11+
12+
logger:
13+
log_level: 'debug'
14+
rollbar_env: 'counter-service'

cmd/barista/config/config.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
8+
"github.com/ilyakaznacheev/cleanenv"
9+
configs "github.com/thangchung/go-coffeeshop/pkg/config"
10+
)
11+
12+
type (
13+
Config struct {
14+
configs.App `yaml:"app"`
15+
configs.HTTP `yaml:"http"`
16+
configs.Log `yaml:"logger"`
17+
RabbitMQ `yaml:"rabbit_mq"`
18+
}
19+
20+
RabbitMQ struct {
21+
URL string `env-required:"true" yaml:"url" env:"RABBITMQ_URL"`
22+
}
23+
)
24+
25+
func NewConfig() (*Config, error) {
26+
cfg := &Config{}
27+
28+
dir, err := os.Getwd()
29+
if err != nil {
30+
log.Fatal(err)
31+
}
32+
33+
// debug
34+
fmt.Println(dir)
35+
36+
err = cleanenv.ReadConfig(dir+"/config.yml", cfg)
37+
if err != nil {
38+
return nil, fmt.Errorf("config error: %w", err)
39+
}
40+
41+
err = cleanenv.ReadEnv(cfg)
42+
if err != nil {
43+
return nil, err
44+
}
45+
46+
return cfg, nil
47+
}

cmd/barista/event/consumer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (c *Consumer) Listen(topics []string) error {
9999
go func() {
100100
switch payload.Name {
101101
case "drink_made":
102-
fmt.Println("Got it")
102+
fmt.Println(payload)
103103
default:
104104
fmt.Println("default")
105105
}

cmd/barista/main.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,24 @@ import (
77
"os"
88
"time"
99

10+
"github.com/golang/glog"
1011
amqp "github.com/rabbitmq/amqp091-go"
12+
"github.com/thangchung/go-coffeeshop/cmd/barista/config"
1113
"github.com/thangchung/go-coffeeshop/cmd/barista/event"
1214
)
1315

1416
const (
15-
RetryTimes = 5
16-
PowOf = 2
17+
RetryTimes = 5
18+
BackOffSeconds = 2
1719
)
1820

1921
func main() {
20-
rabbitConn, err := connect()
22+
cfg, err := config.NewConfig()
23+
if err != nil {
24+
glog.Fatal(err)
25+
}
26+
27+
rabbitConn, err := connect(cfg)
2128
if err != nil {
2229
log.Println(err)
2330
os.Exit(1)
@@ -38,12 +45,12 @@ func main() {
3845
}
3946
}
4047

41-
func connect() (*amqp.Connection, error) {
48+
func connect(cfg *config.Config) (*amqp.Connection, error) {
4249
var (
4350
counts int64
4451
backOff = 1 * time.Second
4552
connection *amqp.Connection
46-
rabbitURL = "amqp://guest:[email protected]:5672/"
53+
rabbitURL = cfg.RabbitMQ.URL
4754
)
4855

4956
for {
@@ -64,8 +71,8 @@ func connect() (*amqp.Connection, error) {
6471
return nil, err
6572
}
6673

67-
fmt.Printf("Backing off for %d seconds...\n", int(math.Pow(float64(counts), PowOf)))
68-
backOff = time.Duration(math.Pow(float64(counts), PowOf)) * time.Second
74+
fmt.Printf("Backing off for %d seconds...\n", int(math.Pow(float64(counts), BackOffSeconds)))
75+
backOff = time.Duration(math.Pow(float64(counts), BackOffSeconds)) * time.Second
6976
time.Sleep(backOff)
7077

7178
continue

cmd/counter/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ http:
66
host: '0.0.0.0'
77
port: 5002
88

9+
rabbit_mq:
10+
url: amqp://guest:[email protected]:5672/
11+
912
logger:
1013
log_level: 'debug'
1114
rollbar_env: 'counter-service'

cmd/counter/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ type (
1414
configs.App `yaml:"app"`
1515
configs.HTTP `yaml:"http"`
1616
configs.Log `yaml:"logger"`
17+
RabbitMQ `yaml:"rabbit_mq"`
18+
}
19+
20+
RabbitMQ struct {
21+
URL string `env-required:"true" yaml:"url" env:"RABBITMQ_URL"`
1722
}
1823
)
1924

cmd/counter/main.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package main
22

33
import (
44
"context"
5-
"fmt"
65
"os"
7-
"reflect"
86

97
"github.com/golang/glog"
108
"github.com/thangchung/go-coffeeshop/cmd/counter/config"
@@ -18,8 +16,6 @@ func main() {
1816
glog.Fatal(err)
1917
}
2018

21-
fmt.Println(reflect.TypeOf(struct{}{}))
22-
2319
mylog := mylogger.New(cfg.Level)
2420

2521
a := app.New(mylog, cfg)

docker-compose.yaml

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,49 +23,66 @@ services:
2323
networks:
2424
- coffeeshop-network
2525

26-
# proxy:
27-
# build:
28-
# context: .
29-
# dockerfile: ./docker/Dockerfile-proxy
30-
# image: go-coffeeshop-proxy
31-
# environment:
32-
# APP_NAME: 'proxy-service in docker'
33-
# GRPC_PRODUCT_HOST: 'product'
34-
# GRPC_PRODUCT_PORT: 5001
35-
# ports:
36-
# - 5000:5000
37-
# depends_on:
38-
# - product
39-
# - counter
40-
# networks:
41-
# - coffeeshop-network
26+
proxy:
27+
build:
28+
context: .
29+
dockerfile: ./docker/Dockerfile-proxy
30+
image: go-coffeeshop-proxy
31+
environment:
32+
APP_NAME: 'proxy-service in docker'
33+
GRPC_PRODUCT_HOST: 'product'
34+
GRPC_PRODUCT_PORT: 5001
35+
GRPC_COUNTER_HOST: 'counter'
36+
GRPC_COUNTER_PORT: 5002
37+
ports:
38+
- 5000:5000
39+
depends_on:
40+
- product
41+
- counter
42+
networks:
43+
- coffeeshop-network
44+
45+
product:
46+
build:
47+
context: .
48+
dockerfile: ./docker/Dockerfile-product
49+
image: go-coffeeshop-product
50+
environment:
51+
APP_NAME: 'product-service in docker'
52+
ports:
53+
- 5001:5001
54+
networks:
55+
- coffeeshop-network
4256

43-
# product:
44-
# build:
45-
# context: .
46-
# dockerfile: ./docker/Dockerfile-product
47-
# image: go-coffeeshop-product
48-
# environment:
49-
# APP_NAME: 'product-service in docker'
50-
# ports:
51-
# - 5001:5001
52-
# networks:
53-
# - coffeeshop-network
57+
counter:
58+
build:
59+
context: .
60+
dockerfile: ./docker/Dockerfile-counter
61+
image: go-coffeeshop-counter
62+
environment:
63+
APP_NAME: 'counter-service in docker'
64+
RABBITMQ_URL: amqp://guest:guest@rabbitmq:5672/
65+
ports:
66+
- 5002:5002
67+
depends_on:
68+
- postgres
69+
- rabbitmq
70+
networks:
71+
- coffeeshop-network
5472

55-
# counter:
56-
# build:
57-
# context: .
58-
# dockerfile: ./docker/Dockerfile-counter
59-
# image: go-coffeeshop-counter
60-
# environment:
61-
# APP_NAME: 'counter-service in docker'
62-
# ports:
63-
# - 5002:5002
64-
# depends_on:
65-
# - postgres
66-
# - rabbitmq
67-
# networks:
68-
# - coffeeshop-network
73+
barista:
74+
build:
75+
context: .
76+
dockerfile: ./docker/Dockerfile-barista
77+
image: go-coffeeshop-barista
78+
environment:
79+
APP_NAME: 'barista-service in docker'
80+
RABBITMQ_URL: amqp://guest:guest@rabbitmq:5672/
81+
depends_on:
82+
- postgres
83+
- rabbitmq
84+
networks:
85+
- coffeeshop-network
6986

7087
networks:
7188
coffeeshop-network:

docker/Dockerfile-barista

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Step 1: Modules caching
2+
FROM golang:1.19.2-alpine3.16 as modules
3+
COPY go.mod go.sum /modules/
4+
WORKDIR /modules
5+
RUN go mod download
6+
7+
# Step 2: Builder
8+
FROM golang:1.19.2-alpine3.16 as builder
9+
COPY --from=modules /go/pkg /go/pkg
10+
COPY . /app
11+
WORKDIR /app
12+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
13+
go build -tags migrate -o /bin/app ./cmd/barista
14+
15+
# Step 3: Final
16+
FROM scratch
17+
18+
# COPY --from=builder /app/config /config
19+
# COPY --from=builder /app/migrations /migrations
20+
21+
# GOPATH for scratch images is /
22+
COPY --from=builder /app/cmd/barista/config.yml /
23+
COPY --from=builder /bin/app /app
24+
CMD ["/app"]

docker/Dockerfile-counter

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Step 1: Modules caching
2+
FROM golang:1.19.2-alpine3.16 as modules
3+
COPY go.mod go.sum /modules/
4+
WORKDIR /modules
5+
RUN go mod download
6+
7+
# Step 2: Builder
8+
FROM golang:1.19.2-alpine3.16 as builder
9+
COPY --from=modules /go/pkg /go/pkg
10+
COPY . /app
11+
WORKDIR /app
12+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
13+
go build -tags migrate -o /bin/app ./cmd/counter
14+
15+
# Step 3: Final
16+
FROM scratch
17+
18+
EXPOSE 5002
19+
20+
# COPY --from=builder /app/config /config
21+
# COPY --from=builder /app/migrations /migrations
22+
23+
# GOPATH for scratch images is /
24+
COPY --from=builder /app/cmd/counter/config.yml /
25+
COPY --from=builder /bin/app /app
26+
CMD ["/app"]

0 commit comments

Comments
 (0)