Skip to content

Commit 22d3319

Browse files
authored
Merge pull request #24 from thangchung/feat/try-sqlc
2 parents 618b01a + c565ee6 commit 22d3319

File tree

112 files changed

+8000
-2331
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+8000
-2331
lines changed

.devcontainer/devcontainer.json

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// new files getting created as root, but you may need to update the USER_UID
1212
// and USER_GID in .devcontainer/Dockerfile to match your user if not 1000.
1313
"-u",
14-
"vscode",
14+
"root",
1515
// Mount go mod cache
1616
"-v",
1717
"coffeeshop-gomodcache:/go/pkg",
@@ -22,6 +22,9 @@
2222
"--security-opt",
2323
"seccomp=unconfined"
2424
],
25+
"remoteUser": "root",
26+
"containerUser": "root",
27+
"forwardPorts": [5000, 8888, 5432, 15672],
2528
// Use 'settings' to set *default* container specific settings.json values on container create.
2629
// You can edit these settings after create using File > Preferences > Settings > Remote.
2730
"settings": {
@@ -84,31 +87,38 @@
8487
"maxStringLen": 2048
8588
},
8689
"apiVersion": 2
87-
}
90+
},
91+
"terminal.integrated.profiles.linux": {
92+
"zsh": {
93+
"path": "/usr/bin/zsh"
94+
}
95+
},
96+
"terminal.integrated.defaultProfile.linux": "zsh"
8897
},
8998
// Add the IDs of extensions you want installed when the container is created in the array below.
9099
"extensions": [
91100
"golang.go",
92101
// optional:
93-
"ms-azuretools.vscode-docker",
94-
"ms-kubernetes-tools.vscode-kubernetes-tools",
95102
"mutantdino.resourcemonitor",
96103
"humao.rest-client",
97-
"42crunch.vscode-openapi",
98104
"heaths.vscode-guid",
99105
"bungcip.better-toml",
100-
"eamodio.gitlens",
106+
"ms-vscode.makefile-tools",
101107
"casualjim.gotemplate",
102108
"davidanson.vscode-markdownlint",
103-
"cweijan.vscode-mysql-client2",
109+
"cweijan.vscode-database-client2",
104110
"bierner.markdown-mermaid",
105111
"hashicorp.hcl",
106112
"fredwangwang.vscode-hcl-format"
113+
// "ms-azuretools.vscode-docker",
114+
// "ms-kubernetes-tools.vscode-kubernetes-tools",
115+
// "42crunch.vscode-openapi",
116+
// "eamodio.gitlens",
107117
],
108-
"postCreateCommand": "go version",
118+
"postCreateCommand": "go version && go install github.com/google/wire/cmd/wire@latest && go install github.com/kyleconroy/sqlc/cmd/sqlc@latest",
109119
"features": {
110120
"ghcr.io/devcontainers/features/go:1": {
111-
"version": "1.19"
121+
"version": "1.19.4"
112122
},
113123
"ghcr.io/devcontainers/features/docker-in-docker:1": {
114124
"moby": false

.golangci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ linters-settings:
1212
check-generated: false
1313
default-signifies-exhaustive: false
1414
funlen:
15-
lines: 65
16-
statements: 40
15+
lines: 100
16+
statements: 50
1717
gocognit:
18-
min-complexity: 15
18+
min-complexity: 40
1919
gocyclo:
20-
min-complexity: 10
20+
min-complexity: 15
2121
goconst:
2222
min-len: 2
2323
min-occurrences: 2

Makefile

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
include .env
22
export
33

4-
PRODUCT_BINARY_NAME=product.out
5-
PROXY_BINARY_NAME=proxy.out
6-
74
all: build test
85

9-
build-product:
10-
go build -tags migrate -o ./cmd/product/${PRODUCT_BINARY_NAME} github.com/thangchung/go-coffeeshop/cmd/product
11-
12-
build-proxy:
13-
go build -tags migrate -o ./cmd/proxy/${PROXY_BINARY_NAME} github.com/thangchung/go-coffeeshop/cmd/proxy
6+
run: run-product run-counter run-barista run-kitchen run-proxy run-web
147

158
run-product:
169
cd cmd/product && go mod tidy && go mod download && \
@@ -42,30 +35,49 @@ run-web:
4235
CGO_ENABLED=0 go run github.com/thangchung/go-coffeeshop/cmd/web
4336
.PHONY: run-web
4437

45-
test:
46-
go test -v main.go
38+
docker-compose: docker-compose-stop docker-compose-start
39+
.PHONY: docker-compose
40+
41+
docker-compose-start:
42+
docker-compose up --build
43+
.PHONY: docker-compose-start
44+
45+
docker-compose-stop:
46+
docker-compose down --remove-orphans -v
47+
.PHONY: docker-compose-stop
48+
49+
docker-compose-core: docker-compose-core-stop docker-compose-core-start
50+
51+
docker-compose-core-start:
52+
docker-compose -f docker-compose-core.yaml up --build
53+
.PHONY: docker-compose-core-start
4754

48-
package:
55+
docker-compose-core-stop:
56+
docker-compose -f docker-compose-core.yaml down --remove-orphans -v
57+
.PHONY: docker-compose-core-stop
58+
59+
docker-compose-build:
4960
docker-compose down --remove-orphans -v
5061
docker-compose build
51-
.PHONY: package
62+
.PHONY: docker-compose-build
5263

53-
compose-up: ### Run docker-compose
54-
docker-compose up --build -d postgres && docker-compose logs -f
55-
.PHONY: compose-up
64+
wire:
65+
cd internal/barista/app && wire && cd - && \
66+
cd internal/counter/app && wire && cd - && \
67+
cd internal/kitchen/app && wire && cd - && \
68+
cd internal/product/app && wire && cd -
69+
.PHONY: wire
5670

57-
compose-down: ### Down docker-compose
58-
docker-compose down --remove-orphans
59-
.PHONY: compose-down
71+
sqlc:
72+
sqlc generate
73+
.PHONY: sqlc
6074

61-
docker-rm-volume: ### remove docker volume
62-
docker volume rm go-clean-template_pg-data
63-
.PHONY: docker-rm-volume
75+
test:
76+
go test -v main.go
6477

6578
linter-golangci: ### check by golangci linter
6679
golangci-lint run
6780
.PHONY: linter-golangci
6881

6982
clean:
70-
go clean
71-
rm ${PRODUCT_BINARY_NAME}
83+
go clean

README.md

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
# go-coffeeshop
22

3-
A coffee shop application with event-driven microservices has been written in Golang. Nomad, Consul Connect, Vault, and Terraform for deployment
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 version 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)
99

1010
## Technical stack
11+
1112
- Backend building blocks
1213
- [grpc-ecosystem/grpc-gateway/v2](https://github.com/grpc-ecosystem/grpc-gateway)
1314
- [labstack/echo/v4](https://github.com/labstack/echo)
1415
- [rabbitmq/amqp091-go](https://github.com/rabbitmq/amqp091-go)
15-
- [jackc/pgx/v4](https://github.com/jackc/pgx)
16-
- [Masterminds/squirrel](https://github.com/Masterminds/squirrel)
17-
- [georgysavva/scany](https://github.com/georgysavva/scany)
16+
- [kyleconroy/sqlc](https://github.com/kyleconroy/sqlc)
17+
- [pq](github.com/lib/pq)
1818
- [golang-migrate/migrate/v4](https://github.com/golang-migrate/migrate)
1919
- Utils
20+
- [google/wire](github.com/google/wire)
2021
- [ilyakaznacheev/cleanenv](https://github.com/ilyakaznacheev/cleanenv)
21-
- [sirupsen/logrus](https://github.com/sirupsen/logrus)
22+
- golang.org/x/exp/slog
23+
- [sirupsen/logrus](https://github.com/sirupsen/logrus)
2224
- [samber/lo](https://github.com/samber/lo)
25+
- [automaxprocs/maxprocs](go.uber.org/automaxprocs/maxprocs)
26+
- [stretchr/testify](github.com/stretchr/testify)
2327
- golang/glog
2428
- google/uuid
2529
- google.golang.org/genproto
@@ -33,7 +37,7 @@ Other version can be found at:
3337

3438
## CoffeeShop - Choreography Saga
3539

36-
![](docs/coffeeshop.svg)
40+
![coffeeshop](docs/coffeeshop.svg)
3741

3842
## Services
3943

@@ -42,20 +46,19 @@ No. | Service | URI
4246
1 | grpc-gateway | [http://localhost:5000](http://localhost:5000)
4347
2 | product service | [http://localhost:5001](http://localhost:5001)
4448
3 | counter service | [http://localhost:5002](http://localhost:5002)
45-
4 | barista service | [http://localhost:5003](http://localhost:5003)
46-
5 | kitchen service | [http://localhost:5004](http://localhost:5004)
47-
6 | web | [http://localhost:8080](http://localhost:8080)
49+
4 | barista service | worker only
50+
5 | kitchen service | worker only
51+
6 | web | [http://localhost:8888](http://localhost:8888)
4852

4953
## Starting project
5054

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

5357
```bash
54-
> docker-compose -f docker-compose-full.yaml build
55-
> docker-compose -f docker-compose-full.yaml up
58+
> make docker-compose
5659
```
5760

58-
From `vscode` => Press F1 => Type `Simple Browser View` => Choose it and enter [http://localhost:8080](http://localhost:8080).
61+
From `vscode` => Press F1 => Type `Simple Browser View` => Choose it and enter [http://localhost:8888](http://localhost:8888).
5962
Enjoy!!!
6063

6164
## Screenshots
@@ -74,21 +77,39 @@ Enjoy!!!
7477

7578
## HashiCorp stack deployment
7679

77-
![](docs/coffeeshop_hashicorp.svg)
80+
![coffeeshop_hashicorp](docs/coffeeshop_hashicorp.svg)
7881

7982
The details of how to run it can be find at [deployment with Nomad, Consult Connect and Vault](build/README.md).
8083

81-
## Debug Apps
84+
## Development
85+
86+
### Clean Domain-driven Design
87+
88+
![clean_ddd](docs/clean_ddd.svg)
89+
90+
### Generate dependency injection instances with wire
91+
92+
```bash
93+
> make wire
94+
```
95+
96+
### Generate code with sqlc
97+
98+
```bash
99+
> make sqlc
100+
```
101+
102+
### Debug Apps
82103

83104
[Debug golang app in monorepo](https://github.com/thangchung/go-coffeeshop/wiki/Golang#debug-app-in-monorepo)
84105

85-
## Trouble shooting
106+
### Trouble shooting
86107

87108
[Development project trouble shooting](https://github.com/thangchung/go-coffeeshop/wiki#trouble-shooting)
88109

89110
## Roadmap
90111

91-
- Enhance project structure with DDD patterns
112+
- Enhance project structure with DDD patterns
92113
- Add testing
93114
- Add and integrate with observability libs and tools
94115
- Add user identity management (authentication and authorization)

client.http

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ GET {{host}}/v1/api/item-types HTTP/1.1
55
content-type: application/json
66

77
###
8-
GET {{host}}/v1/api/items-by-types/COFFEE_WITH_ROOM,MUFFIN HTTP/1.1
8+
GET {{host}}/v1/api/items-by-types/COFFEE_WITH_ROOM,MUFFIN,COFFEE_BLACK,CROISSANT_CHOCOLATE HTTP/1.1
99
content-type: application/json
1010

1111
###

cmd/barista/config.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ http:
88

99
postgres:
1010
pool_max: 2
11-
url: postgres://postgres:P@ssw0rd@172.28.240.102:5432/postgres?sslmode=disable
11+
dsn_url: host=127.0.0.1 user=postgres password=P@ssw0rd dbname=postgres sslmode=disable
1212

13-
rabbit_mq:
14-
url: amqp://guest:guest@172.28.240.102:5672/
13+
rabbitmq:
14+
url: amqp://guest:guest@127.0.0.1:5672/
1515

1616
logger:
1717
log_level: 'debug'

cmd/barista/config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ type (
1515
configs.HTTP `yaml:"http"`
1616
configs.Log `yaml:"logger"`
1717
PG `yaml:"postgres"`
18-
RabbitMQ `yaml:"rabbit_mq"`
18+
RabbitMQ `yaml:"rabbitmq"`
1919
}
2020

2121
PG struct {
2222
PoolMax int `env-required:"true" yaml:"pool_max" env:"PG_POOL_MAX"`
23-
URL string `env-required:"true" yaml:"url" env:"PG_URL"`
23+
DsnURL string `env-required:"true" yaml:"dsn_url" env:"PG_DSN_URL"`
2424
}
2525

2626
RabbitMQ struct {
@@ -37,7 +37,7 @@ func NewConfig() (*Config, error) {
3737
}
3838

3939
// debug
40-
fmt.Println(dir)
40+
fmt.Println("config path: " + dir)
4141

4242
err = cleanenv.ReadConfig(dir+"/config.yml", cfg)
4343
if err != nil {

0 commit comments

Comments
 (0)