Skip to content

Dev documentation

Robert Trzebiński edited this page Dec 8, 2024 · 26 revisions

Run locally on a Mac

Quick start

make deps
make dev
make start

Prerequisites

Install Homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Install go

brew install go

Install migrate.

brew install golang-migrate

Add GOPATH to your path

For bash:

echo 'export PATH='`go env GOPATH`'/bin:$PATH' >> ~/.bash_profile && source ~/.bash_profile

For Zsh:

echo 'export PATH='`go env GOPATH`'/bin:$PATH' >> ~/.zshrc && source ~/.zshrc

Makefile

Use make commands to manage the development environment.

make
target                         help
------                         ----
help                           Show this help
deps                           Install local environment dependencies
dev                            Prepare local dev environment (stop + start + migrate + seed)
start                          Start docker-compose containers
stop                           Stop docker-compose containers
ps                             Show running containers
restart                        Stop and start containers
destroy                        Stop containers and remove volumes
migrate                        Run db migrations (migrate up)
migrate-down                   Revert db migrations (migrate down)
migrate-drop                   Drop db without confirmation (migrate drop)
seed                           Seed the database with some example data
reseed                         Destroy, recreate and seed the database (no confirmation)
db                             Db CLI client connection
build                          Build client and server
run                            Build and run locally
test                           Test all
test-short                     Test short (unit)
go-deps-update                 Update GO dependencies
k8s-start                      Kubernetes create all objects (Docker hub tag 'latest' image)
k8s-status                     Kubernetes show all objects
k8s-stop                       Kubernetes delete all objects
k8s-reset                      Kubernetes stop and start
k8s-rollout                    Kubernetes rollout (Docker hub tag 'latest' image)
k8s-logs                       Kubernetes web app logs
k8s-sh                         Kubernetes web app shell
k8s-db                         Kubernetes db cli
k8s-db-migrate                 Kubernetes db migrate
k8s-db-seed                    Kubernetes db seed

Start local development environment

You will run Go natively from your machine, but docker compose is used to provide supporting containers:

Bring containers up:

make start

Bring containers down:

make stop

Bring containers down and remove volumes:

make destroy

Check containers status:

make ps

Run

make run

Open http://localhost:8000

Build only

make build

Run tests

make test

Short (unit) only

make test-short

DB Credentials

Note that PSQL is bound to a custom port 5430 - see ./docker-compose.yml.

Local db credentials to be used in SQL client:

localhost
u: postgres
p: postgres
db: postgres
port: 5430

DB Connection

make db
PGPASSWORD=postgres psql -U postgres -d postgres --port 5430 --host localhost

DB Migrations

Install migrate - run this (one off) before running your first migration:

go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@v4.15.2

Alternatively you can do that with brew:

brew install golang-migrate

Run migrations:

make migrate

Rollback:

make migrate-down

Create new migration:

migrate create -ext sql -dir migrations -seq create_table_foo

DB Seeders

Seed db

make seed

Re-seed db

make reseed

DB Check open connections

SELECT * FROM pg_stat_activity;

DB inspect when running integration test

Set a time.Sleep(1000 * time.Second) (or a debugger breakpoint), run test so it stops at the point when you can check DB state.

Use docker ps to find port of your DB container.

$ PGPASSWORD=password psql -U postgres -d testdb --port 49756 --host localhost
psql (10.22, server 14.5)
WARNING: psql major version 10, server major version 14.
         Some psql features might not work.
Type "help" for help.

testdb=# select * from exercise;
 id | lesson_id |               question               |                answer                
----+-----------+--------------------------------------+--------------------------------------
  1 |         1 | e6ac4b4f-a07e-48b6-9a6b-595426fe35a6 | fc38a6f8-ab33-4fb6-96da-46a27e53344b
  2 |         2 | another                              | b89fa3da-1a91-4a4f-b4e6-a2ea43410d3c
(2 rows)

Run in k8s cluster

$ make
(...)
k8s-deploy-all                 Kubernetes deploy all objects
k8s-delete-all                 Kubernetes delete all objects
k8s-deploy                     Kubernetes deploy
k8s-rollout                    Kubernetes rollout
k8s-delete                     Kubernetes delete
k8s-logs                       Kubernetes web app logs
k8s-sh                         Kubernetes web app shell
k8s-db                         Kubernetes db cli
k8s-db-migrate                 Kubernetes db migrate
k8s-db-seed                    Kubernetes db seed
k8s-db-backup-cronjob-deploy   Kubernetes db backup CRON job deploy
k8s-db-backup-cronjob-delete   Kubernetes db backup CRON job delete

Docker images

Web

https://hub.docker.com/r/rtrzebinski/sm4-web

docker build -f Dockerfile-web --platform linux/amd64 --tag rtrzebinski/sm4-web:latest .

docker push rtrzebinski/sm4-web:latest

docker run -p 8000:8000 rtrzebinski/sm4-web:latest

docker run -it rtrzebinski/sm4-web:latest /bin/sh

Worker

https://hub.docker.com/r/rtrzebinski/sm4-worker

docker build -f Dockerfile-worker --platform linux/amd64 --tag rtrzebinski/sm4-worker:latest .

docker push rtrzebinski/sm4-worker:latest

Auth

https://hub.docker.com/r/rtrzebinski/sm4-auth

docker build -f Dockerfile-auth --platform linux/amd64 --tag rtrzebinski/sm4-auth:latest .

docker push rtrzebinski/sm4-auth:latest