Skip to content

Commit

Permalink
Merge pull request #24 from thangchung/feat/try-sqlc
Browse files Browse the repository at this point in the history
  • Loading branch information
thangchung authored Dec 28, 2022
2 parents 618b01a + c565ee6 commit 22d3319
Show file tree
Hide file tree
Showing 112 changed files with 8,000 additions and 2,331 deletions.
28 changes: 19 additions & 9 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// new files getting created as root, but you may need to update the USER_UID
// and USER_GID in .devcontainer/Dockerfile to match your user if not 1000.
"-u",
"vscode",
"root",
// Mount go mod cache
"-v",
"coffeeshop-gomodcache:/go/pkg",
Expand All @@ -22,6 +22,9 @@
"--security-opt",
"seccomp=unconfined"
],
"remoteUser": "root",
"containerUser": "root",
"forwardPorts": [5000, 8888, 5432, 15672],
// Use 'settings' to set *default* container specific settings.json values on container create.
// You can edit these settings after create using File > Preferences > Settings > Remote.
"settings": {
Expand Down Expand Up @@ -84,31 +87,38 @@
"maxStringLen": 2048
},
"apiVersion": 2
}
},
"terminal.integrated.profiles.linux": {
"zsh": {
"path": "/usr/bin/zsh"
}
},
"terminal.integrated.defaultProfile.linux": "zsh"
},
// Add the IDs of extensions you want installed when the container is created in the array below.
"extensions": [
"golang.go",
// optional:
"ms-azuretools.vscode-docker",
"ms-kubernetes-tools.vscode-kubernetes-tools",
"mutantdino.resourcemonitor",
"humao.rest-client",
"42crunch.vscode-openapi",
"heaths.vscode-guid",
"bungcip.better-toml",
"eamodio.gitlens",
"ms-vscode.makefile-tools",
"casualjim.gotemplate",
"davidanson.vscode-markdownlint",
"cweijan.vscode-mysql-client2",
"cweijan.vscode-database-client2",
"bierner.markdown-mermaid",
"hashicorp.hcl",
"fredwangwang.vscode-hcl-format"
// "ms-azuretools.vscode-docker",
// "ms-kubernetes-tools.vscode-kubernetes-tools",
// "42crunch.vscode-openapi",
// "eamodio.gitlens",
],
"postCreateCommand": "go version",
"postCreateCommand": "go version && go install github.com/google/wire/cmd/wire@latest && go install github.com/kyleconroy/sqlc/cmd/sqlc@latest",
"features": {
"ghcr.io/devcontainers/features/go:1": {
"version": "1.19"
"version": "1.19.4"
},
"ghcr.io/devcontainers/features/docker-in-docker:1": {
"moby": false
Expand Down
8 changes: 4 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ linters-settings:
check-generated: false
default-signifies-exhaustive: false
funlen:
lines: 65
statements: 40
lines: 100
statements: 50
gocognit:
min-complexity: 15
min-complexity: 40
gocyclo:
min-complexity: 10
min-complexity: 15
goconst:
min-len: 2
min-occurrences: 2
Expand Down
58 changes: 35 additions & 23 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
include .env
export

PRODUCT_BINARY_NAME=product.out
PROXY_BINARY_NAME=proxy.out

all: build test

build-product:
go build -tags migrate -o ./cmd/product/${PRODUCT_BINARY_NAME} github.com/thangchung/go-coffeeshop/cmd/product

build-proxy:
go build -tags migrate -o ./cmd/proxy/${PROXY_BINARY_NAME} github.com/thangchung/go-coffeeshop/cmd/proxy
run: run-product run-counter run-barista run-kitchen run-proxy run-web

run-product:
cd cmd/product && go mod tidy && go mod download && \
Expand Down Expand Up @@ -42,30 +35,49 @@ run-web:
CGO_ENABLED=0 go run github.com/thangchung/go-coffeeshop/cmd/web
.PHONY: run-web

test:
go test -v main.go
docker-compose: docker-compose-stop docker-compose-start
.PHONY: docker-compose

docker-compose-start:
docker-compose up --build
.PHONY: docker-compose-start

docker-compose-stop:
docker-compose down --remove-orphans -v
.PHONY: docker-compose-stop

docker-compose-core: docker-compose-core-stop docker-compose-core-start

docker-compose-core-start:
docker-compose -f docker-compose-core.yaml up --build
.PHONY: docker-compose-core-start

package:
docker-compose-core-stop:
docker-compose -f docker-compose-core.yaml down --remove-orphans -v
.PHONY: docker-compose-core-stop

docker-compose-build:
docker-compose down --remove-orphans -v
docker-compose build
.PHONY: package
.PHONY: docker-compose-build

compose-up: ### Run docker-compose
docker-compose up --build -d postgres && docker-compose logs -f
.PHONY: compose-up
wire:
cd internal/barista/app && wire && cd - && \
cd internal/counter/app && wire && cd - && \
cd internal/kitchen/app && wire && cd - && \
cd internal/product/app && wire && cd -
.PHONY: wire

compose-down: ### Down docker-compose
docker-compose down --remove-orphans
.PHONY: compose-down
sqlc:
sqlc generate
.PHONY: sqlc

docker-rm-volume: ### remove docker volume
docker volume rm go-clean-template_pg-data
.PHONY: docker-rm-volume
test:
go test -v main.go

linter-golangci: ### check by golangci linter
golangci-lint run
.PHONY: linter-golangci

clean:
go clean
rm ${PRODUCT_BINARY_NAME}
go clean
57 changes: 39 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
# go-coffeeshop

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

Other version can be found at:
Other versions in .NET/C# can be found at:

- [.NET CoffeeShop with Microservices approach](https://github.com/thangchung/coffeeshop-on-nomad)
- [.NET CoffeeShop with Modular Monolith approach](https://github.com/thangchung/coffeeshop-modular)

## Technical stack

- Backend building blocks
- [grpc-ecosystem/grpc-gateway/v2](https://github.com/grpc-ecosystem/grpc-gateway)
- [labstack/echo/v4](https://github.com/labstack/echo)
- [rabbitmq/amqp091-go](https://github.com/rabbitmq/amqp091-go)
- [jackc/pgx/v4](https://github.com/jackc/pgx)
- [Masterminds/squirrel](https://github.com/Masterminds/squirrel)
- [georgysavva/scany](https://github.com/georgysavva/scany)
- [kyleconroy/sqlc](https://github.com/kyleconroy/sqlc)
- [pq](github.com/lib/pq)
- [golang-migrate/migrate/v4](https://github.com/golang-migrate/migrate)
- Utils
- [google/wire](github.com/google/wire)
- [ilyakaznacheev/cleanenv](https://github.com/ilyakaznacheev/cleanenv)
- [sirupsen/logrus](https://github.com/sirupsen/logrus)
- golang.org/x/exp/slog
- [sirupsen/logrus](https://github.com/sirupsen/logrus)
- [samber/lo](https://github.com/samber/lo)
- [automaxprocs/maxprocs](go.uber.org/automaxprocs/maxprocs)
- [stretchr/testify](github.com/stretchr/testify)
- golang/glog
- google/uuid
- google.golang.org/genproto
Expand All @@ -33,7 +37,7 @@ Other version can be found at:

## CoffeeShop - Choreography Saga

![](docs/coffeeshop.svg)
![coffeeshop](docs/coffeeshop.svg)

## Services

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

## Starting project

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

```bash
> docker-compose -f docker-compose-full.yaml build
> docker-compose -f docker-compose-full.yaml up
> make docker-compose
```

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

## Screenshots
Expand All @@ -74,21 +77,39 @@ Enjoy!!!

## HashiCorp stack deployment

![](docs/coffeeshop_hashicorp.svg)
![coffeeshop_hashicorp](docs/coffeeshop_hashicorp.svg)

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

## Debug Apps
## Development

### Clean Domain-driven Design

![clean_ddd](docs/clean_ddd.svg)

### Generate dependency injection instances with wire

```bash
> make wire
```

### Generate code with sqlc

```bash
> make sqlc
```

### Debug Apps

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

## Trouble shooting
### Trouble shooting

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

## Roadmap

- Enhance project structure with DDD patterns
- Enhance project structure with DDD patterns
- Add testing
- Add and integrate with observability libs and tools
- Add user identity management (authentication and authorization)
Expand Down
2 changes: 1 addition & 1 deletion client.http
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ GET {{host}}/v1/api/item-types HTTP/1.1
content-type: application/json

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

###
Expand Down
6 changes: 3 additions & 3 deletions cmd/barista/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ http:

postgres:
pool_max: 2
url: postgres://postgres:P@ssw0rd@172.28.240.102:5432/postgres?sslmode=disable
dsn_url: host=127.0.0.1 user=postgres password=P@ssw0rd dbname=postgres sslmode=disable

rabbit_mq:
url: amqp://guest:guest@172.28.240.102:5672/
rabbitmq:
url: amqp://guest:guest@127.0.0.1:5672/

logger:
log_level: 'debug'
Expand Down
6 changes: 3 additions & 3 deletions cmd/barista/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ type (
configs.HTTP `yaml:"http"`
configs.Log `yaml:"logger"`
PG `yaml:"postgres"`
RabbitMQ `yaml:"rabbit_mq"`
RabbitMQ `yaml:"rabbitmq"`
}

PG struct {
PoolMax int `env-required:"true" yaml:"pool_max" env:"PG_POOL_MAX"`
URL string `env-required:"true" yaml:"url" env:"PG_URL"`
DsnURL string `env-required:"true" yaml:"dsn_url" env:"PG_DSN_URL"`
}

RabbitMQ struct {
Expand All @@ -37,7 +37,7 @@ func NewConfig() (*Config, error) {
}

// debug
fmt.Println(dir)
fmt.Println("config path: " + dir)

err = cleanenv.ReadConfig(dir+"/config.yml", cfg)
if err != nil {
Expand Down
Loading

0 comments on commit 22d3319

Please sign in to comment.