Skip to content

Commit c565ee6

Browse files
committed
finish refactor code
1 parent 5e28066 commit c565ee6

File tree

26 files changed

+4367
-154
lines changed

26 files changed

+4367
-154
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ docker-compose-build:
6464
wire:
6565
cd internal/barista/app && wire && cd - && \
6666
cd internal/counter/app && wire && cd - && \
67-
cd internal/barista/app && wire && cd - && \
67+
cd internal/kitchen/app && wire && cd - && \
6868
cd internal/product/app && wire && cd -
6969
.PHONY: wire
7070

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# go-coffeeshop
22

3-
A event-driven microservices coffee shop application has been written in Golang, and deployment using Nomad, Consul Connect, Vault, and Terraform.
3+
An event-driven microservices coffee shop application has been written in Golang and deployed using Nomad, Consul Connect, Vault, and Terraform.
44

5-
Other versions can be found at:
5+
Other versions in .NET/C# can be found at:
66

77
- [.NET CoffeeShop with Microservices approach](https://github.com/thangchung/coffeeshop-on-nomad)
88
- [.NET CoffeeShop with Modular Monolith approach](https://github.com/thangchung/coffeeshop-modular)
@@ -23,6 +23,7 @@ Other versions can be found at:
2323
- [sirupsen/logrus](https://github.com/sirupsen/logrus)
2424
- [samber/lo](https://github.com/samber/lo)
2525
- [automaxprocs/maxprocs](go.uber.org/automaxprocs/maxprocs)
26+
- [stretchr/testify](github.com/stretchr/testify)
2627
- golang/glog
2728
- google/uuid
2829
- google.golang.org/genproto
@@ -45,16 +46,16 @@ No. | Service | URI
4546
1 | grpc-gateway | [http://localhost:5000](http://localhost:5000)
4647
2 | product service | [http://localhost:5001](http://localhost:5001)
4748
3 | counter service | [http://localhost:5002](http://localhost:5002)
48-
4 | barista service |
49-
5 | kitchen service |
49+
4 | barista service | worker only
50+
5 | kitchen service | worker only
5051
6 | web | [http://localhost:8888](http://localhost:8888)
5152

5253
## Starting project
5354

54-
Jump into `.devcontainer`, then
55+
Jump into [`.devcontainer`](https://code.visualstudio.com/docs/devcontainers/containers), then
5556

5657
```bash
57-
> make compose
58+
> make docker-compose
5859
```
5960

6061
From `vscode` => Press F1 => Type `Simple Browser View` => Choose it and enter [http://localhost:8888](http://localhost:8888).
@@ -82,6 +83,10 @@ The details of how to run it can be find at [deployment with Nomad, Consult Conn
8283

8384
## Development
8485

86+
### Clean Domain-driven Design
87+
88+
![clean_ddd](docs/clean_ddd.svg)
89+
8590
### Generate dependency injection instances with wire
8691

8792
```bash

cmd/barista/main.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,17 @@ func main() {
4646
// integrate Logrus with the slog logger
4747
slog.New(logger.NewLogrusHandler(logrus.StandardLogger()))
4848

49-
a, err := app.InitApp(cfg, postgres.DBConnString(cfg.PG.DsnURL), rabbitmq.RabbitMQConnStr(cfg.RabbitMQ.URL))
49+
a, cleanup, err := app.InitApp(cfg, postgres.DBConnString(cfg.PG.DsnURL), rabbitmq.RabbitMQConnStr(cfg.RabbitMQ.URL))
5050
if err != nil {
5151
slog.Error("failed init app", err)
5252
cancel()
5353
}
5454

55-
defer a.AMQPConn.Close()
56-
defer a.PG.Close()
57-
5855
a.CounterOrderPub.Configure(
5956
pkgPublisher.ExchangeName("counter-order-exchange"),
6057
pkgPublisher.BindingKey("counter-order-routing-key"),
6158
pkgPublisher.MessageTypeName("barista-order-updated"),
6259
)
63-
defer a.CounterOrderPub.CloseChan()
6460

6561
a.Consumer.Configure(
6662
pkgConsumer.ExchangeName("barista-order-exchange"),
@@ -84,8 +80,10 @@ func main() {
8480

8581
select {
8682
case v := <-quit:
83+
cleanup()
8784
slog.Info("signal.Notify", v)
8885
case done := <-ctx.Done():
86+
cleanup()
8987
slog.Info("ctx.Done", done)
9088
}
9189
}

cmd/counter/main.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func main() {
5555
<-ctx.Done()
5656
}()
5757

58-
prepareApp(ctx, cancel, cfg, server)
58+
cleanup := prepareApp(ctx, cancel, cfg, server)
5959

6060
// gRPC Server.
6161
address := fmt.Sprintf("%s:%d", cfg.HTTP.Host, cfg.HTTP.Port)
@@ -89,36 +89,33 @@ func main() {
8989

9090
select {
9191
case v := <-quit:
92+
cleanup()
9293
slog.Info("signal.Notify", v)
9394
case done := <-ctx.Done():
94-
slog.Info("ctx.Done", done)
95+
cleanup()
96+
slog.Info("ctx.Done", "app done", done)
9597
}
9698
}
9799

98-
func prepareApp(ctx context.Context, cancel context.CancelFunc, cfg *config.Config, server *grpc.Server) {
99-
a, err := app.InitApp(cfg, postgres.DBConnString(cfg.PG.DsnURL), rabbitmq.RabbitMQConnStr(cfg.RabbitMQ.URL), server)
100+
func prepareApp(ctx context.Context, cancel context.CancelFunc, cfg *config.Config, server *grpc.Server) func() {
101+
a, cleanup, err := app.InitApp(cfg, postgres.DBConnString(cfg.PG.DsnURL), rabbitmq.RabbitMQConnStr(cfg.RabbitMQ.URL), server)
100102
if err != nil {
101103
slog.Error("failed init app", err)
102104
cancel()
103105
<-ctx.Done()
104106
}
105107

106-
// defer a.AMQPConn.Close()
107-
// defer a.PG.Close()
108-
109108
a.BaristaOrderPub.Configure(
110109
pkgPublisher.ExchangeName("barista-order-exchange"),
111110
pkgPublisher.BindingKey("barista-order-routing-key"),
112111
pkgPublisher.MessageTypeName("barista-order-created"),
113112
)
114-
// defer a.BaristaOrderPub.CloseChan()
115113

116114
a.KitchenOrderPub.Configure(
117115
pkgPublisher.ExchangeName("kitchen-order-exchange"),
118116
pkgPublisher.BindingKey("kitchen-order-routing-key"),
119117
pkgPublisher.MessageTypeName("kitchen-order-created"),
120118
)
121-
// defer a.KitchenOrderPub.CloseChan()
122119

123120
a.Consumer.Configure(
124121
pkgConsumer.ExchangeName("counter-order-exchange"),
@@ -135,4 +132,6 @@ func prepareApp(ctx context.Context, cancel context.CancelFunc, cfg *config.Conf
135132
<-ctx.Done()
136133
}
137134
}()
135+
136+
return cleanup
138137
}

cmd/kitchen/main.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,17 @@ func main() {
4646
// integrate Logrus with the slog logger
4747
slog.New(logger.NewLogrusHandler(logrus.StandardLogger()))
4848

49-
a, err := app.InitApp(cfg, postgres.DBConnString(cfg.PG.DsnURL), rabbitmq.RabbitMQConnStr(cfg.RabbitMQ.URL))
49+
a, cleanup, err := app.InitApp(cfg, postgres.DBConnString(cfg.PG.DsnURL), rabbitmq.RabbitMQConnStr(cfg.RabbitMQ.URL))
5050
if err != nil {
5151
slog.Error("failed init app", err)
5252
cancel()
5353
}
5454

55-
defer a.AMQPConn.Close()
56-
defer a.PG.Close()
57-
5855
a.CounterOrderPub.Configure(
5956
pkgPublisher.ExchangeName("counter-order-exchange"),
6057
pkgPublisher.BindingKey("counter-order-routing-key"),
6158
pkgPublisher.MessageTypeName("kitchen-order-updated"),
6259
)
63-
defer a.CounterOrderPub.CloseChan()
6460

6561
a.Consumer.Configure(
6662
pkgConsumer.ExchangeName("kitchen-order-exchange"),
@@ -84,8 +80,10 @@ func main() {
8480

8581
select {
8682
case v := <-quit:
83+
cleanup()
8784
slog.Info("signal.Notify", v)
8885
case done := <-ctx.Done():
86+
cleanup()
8987
slog.Info("ctx.Done", done)
9088
}
9189
}

docs/clean_ddd.svg

Lines changed: 16 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)