code --install-extension esbenp.prettier-vscode
code --install-extension foxundermoon.shell-format
code --install-extension golang.go
code --install-extension matt-meyers.vscode-dbml
code --install-extension mtxr.sqltools
code --install-extension zxh404.vscode-proto3Create the db.dbml in to decouple the design from a specific
database. Then run dbdocs and dbml2sql to generate the docs and
schema.sql boilerplate, we'll choose Postgres.
# Install nvs.
export NVS_HOME="$HOME/.local/share/nvs"
git clone https://github.com/jasongin/nvs "$NVS_HOME"
. "$NVS_HOME/nvs.sh" install
# Install node lts.
nvs add lts
nvs use ltsnpm install -g dbdocs
dbdocs login
dbdocs build docs/db.dbml
dbdocs password —set secret —project go_clean_archnpm install -g @dbml/cli
dbml2sql --postgres -o docs/schemal.sql docs/db.dbmlInstall Docker and PostgresSQL image.
# Install Docker.
brew install docker
# Run Docker app so that we can access the `docker` command.
# Pull the PostgresSQL image.
docker pull postgres:15.2-alpine
# Check the downloaded image.
docker imagesCreates and runs a Docker container with the name postgres, using the official
postgres:15-alpine Docker image. The container is started as a background
process (-d flag) and is mapped to port 5432 of the host machine
(-p 127.0.0.1:5432:5432/tcp flag), which is the default port for PostgreSQL.
The container is also configured with the environment variables POSTGRES_USER
and POSTGRES_PASSWORD, which set the default username and password for the
PostgreSQL database. In this case, the username is set to root and the
password is set to password.
docker run --name postgres \
-p 127.0.0.1:5432:5432/tcp \
-e POSTGRES_USER=root \
-e POSTGRES_PASSWORD=password \
-d postgres:15.2-alpine# Enter the Postgres shell.
docker exec -it postgres psql -U root
# Try the following query in the shell.
SELECT now();Install TablePlus
# Install TablePlus.
brew install tableplusConnect to Postgres with the setting
# Install `migrate` command.
brew install golang-migrate
# Check the installed `migrate` command.
migrate --version
# Create the db migration directory.
mkdir -p db/migration
# Create the first migration script.
migrate create -ext sql -dir internal/db/migration -seq init_schemaNow, create a Makefile to save time and run the following:
# Run a PostgreSQL container.
make postgres
# Create a DB called "microservice" in this clean architecture.
make createdb
# Migrate up to create tables in the DB.
make migrateup# Install sqlc.
brew install sqlc
# Check the installed sqlc.
sqlc versionInitialize sqlc.yaml and copy the initial config from
Getting started with PostgreSQL
with some modifications.
sqlc initAdd the queries in user.sql, then make sqlc to
codegen.
# Codegen.
make sqlc
# Eliminate red lines inside `./internal/repository/user.sql.go`.
go mod init github.com/walkccc/go-clean-archInstall gRPC for Go
# Install protobuf
brew install protobufgo install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2Add "protoc": { "options": ["--proto_path=proto"] }, to
~/Library/Application Support/Code/User/settings.json to eliminate red lines.
# Generate gRPC code.
make proto
# Download required packages.
go mod tidyInstall Evans to test gRPC
# Install evans.
brew tap ktr0731/evans
brew install evans
# Start evans REPL.
make evansInstall GoMock.
# Install mockgen.
go install github.com/golang/mock/mockgen@v1.6.0
# Export the Go path.
# Add "export PATH=$PATH:~/go/bin" in your .zshrc or .bashrc
# Check the installed mockgen.
which mockgenServe HTTP request with gRPC-Gateway
- Add required protos from googleapis/google/api
- Add required tools
- Update annotation in microservice.proto
make proto# Creates a Docker image with the name "microservice" and the tag "latest" using
# the Dockerfile in the current directory.
docker build -t microservice:latest .docker-compose.yaml defines two services: postgres
and api.
- The
postgresservice sets up a PostgreSQL database container with some environment variables and a healthcheck. - The
apiservice builds a container from the Dockerfile, sets an environment variable with the database connection string (which will be used in start.sh), and depends on the postgres service to be healthy before starting up.
When you run docker compose up, Docker Compose starts up the containers for
both services, with the api service waiting for the postgres service to be
ready before starting.
# Make the start.sh file executable so that it can be run in Docker container.
chmod +x start.sh
# Start up all the services defined in a Docker Compose file.
docker compose upTo deploy to AWS, please read AWS deployment
