Simple Bank
This repository contains the codes of the Backend Master Class course by TECH SCHOOL on our Youtube channel.
You can also find it on Udemy at this link.
In this course, you will learn step-by-step how to design, develop and deploy a backend web service from scratch. I believe the best way to learn programming is to build a real application. Therefore, throughout the course, you will learn how to build a backend web service for a simple bank. It will provide APIs for the frontend to do the following things:
- Create and manage bank accounts.
- Record all balance changes to each of the accounts.
- Perform a money transfer between 2 accounts.
The programming language we will use to develop the service is Golang, but the course is not just about coding in Go. The course is divided into 3 main parts:
-
In the first part, you will learn deeply about how to design the database, generate codes to talk to the DB in a consistent and reliable way using transactions, understand the DB isolation levels, and how to use it correctly in production. Besides the database, you will also learn how to use docker for local development, how to use Git to manage your codes, and how to use Github Action to run unit tests automatically.
-
In the second part, you will learn how to build a set of RESTful HTTP APIs using Gin - one of the most popular Golang frameworks for building web services. This includes everything from loading app configs, mocking DB for more robust unit tests, handling errors, authenticating users, and securing the APIs with JWT and PASETO access tokens.
-
In the last part, you will learn how to build your app with Docker and deploy it to a production Kubernetes cluster on AWS. The lectures are very detailed with a step-by-step guide, from how to build a minimal docker image, set up a free-tier AWS account, create a production database, store and retrieve production secrets, create a Kubernetes cluster with EKS, use Github Action to automatically build and deploy the image to the EKS cluster, buy a domain name and route the traffics to the service, secure the connection with HTTPs and auto-renew TLS certificate from Let's Encrypt.
This course is designed with a lot of details, so that everyone, even with very little programming experience can understand and do it by themselves. I strongly believe that after the course, you would be able to work much more confidently and effectively in your projects.
Course videos
- Lecture #1: Design DB schema and generate SQL code with dbdiagram.io
- Lecture #2: Install & use Docker + Postgres + TablePlus to create DB schema
- Lecture #3: How to write & run database migration in Golang
- Lecture #4: Generate CRUD Golang code from SQL | Compare db/sql, gorm, sqlx & sqlc
- Lecture #5: Write unit tests for database CRUD with random data in Golang
- Lecture #6: A clean way to implement database transaction in Golang
- Lecture #7: DB transaction lock & How to handle deadlock in Golang
- Lecture #8: How to avoid deadlock in DB transaction? Queries order matters!
- Lecture #9: Deeply understand transaction isolation levels & read phenomena in MySQL & PostgreSQL
- Lecture #10: Setup Github Actions for Golang + Postgres to run automated tests
- Lecture #11: Implement RESTful HTTP API in Go using Gin
- Lecture #12: Load config from file & environment variables in Go with Viper
- Lecture #13: Mock DB for testing HTTP API in Go and achieve 100% coverage
- Lecture #14: Implement transfer money API with a custom params validator
- Lecture #15: Add users table with unique & foreign key constraints in PostgreSQL
- Lecture #16: How to handle DB errors in Golang correctly
- Lecture #17: How to securely store passwords? Hash password in Go with Bcrypt!
- Lecture #18: How to write stronger unit tests with a custom gomock matcher
- Lecture #19: Why PASETO is better than JWT for token-based authentication?
- Lecture #20: How to create and verify JWT & PASETO token in Golang
- Lecture #21: Implement login user API that returns PASETO or JWT access token in Go
- Lecture #22: Implement authentication middleware and authorization rules in Golang using Gin
- Lecture #23: Build a minimal Golang Docker image with a multistage Dockerfile
- Lecture #24: How to use docker network to connect 2 stand-alone containers
- Lecture #25: How to write docker-compose file and control service start-up orders with wait-for.sh
- Lecture #26: How to create a free tier AWS account
- Lecture #27: Auto build & push docker image to AWS ECR with Github Actions
- Lecture #28: How to create a production DB on AWS RDS
- Lecture #29: Store & retrieve production secrets with AWS secrets manager
- Lecture #30: Kubernetes architecture & How to create an EKS cluster on AWS
- Lecture #31: How to use kubectl & k9s to connect to a kubernetes cluster on AWS EKS
- Lecture #32: How to deploy a web app to Kubernetes cluster on AWS EKS
- Lecture #33: Register a domain name & set up A-record using Route53
- Lecture #34: How to use Ingress to route traffics to different services in Kubernetes
- Lecture #35: Automatic issue TLS certificates in Kubernetes with Let's Encrypt
- Lecture #36: Automatic deploy to Kubernetes with Github Action
Simple bank service
The service that we’re going to build is a simple bank. It will provide APIs for the frontend to do following things:
- Create and manage bank accounts, which are composed of owner’s name, balance, and currency.
- Record all balance changes to each of the account. So every time some money is added to or subtracted from the account, an account entry record will be created.
- Perform a money transfer between 2 accounts. This should happen within a transaction, so that either both accounts’ balance are updated successfully or none of them are.
Setup local development
Install tools
-
brew install golang-migrate
-
npm install -g dbdocs dbdocs login
-
npm install -g @dbml/cli dbml2sql --version
-
brew install sqlc
-
go install github.com/golang/mock/mockgen@v1.6.0
Setup infrastructure
-
Create the bank-network
make network
-
Start postgres container:
make postgres
-
Create simple_bank database:
make createdb
-
Run db migration up all versions:
make migrateup
-
Run db migration up 1 version:
make migrateup1
-
Run db migration down all versions:
make migratedown
-
Run db migration down 1 version:
make migratedown1
Documentation
-
Generate DB documentation:
make db_docs
-
Access the DB documentation at this address. Password:
secret
How to generate code
-
Generate schema SQL file with DBML:
make db_schema
-
Generate SQL CRUD with sqlc:
make sqlc
-
Generate DB mock with gomock:
make mock
-
Create a new db migration:
migrate create -ext sql -dir db/migration -seq <migration_name>
How to run
-
Run server:
make server
-
Run test:
make test
Deploy to kubernetes cluster
-
Install nginx ingress controller:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.48.1/deploy/static/provider/aws/deploy.yaml
-
kubectl apply -f https://github.com/jetstack/cert-manager/releases/download/v1.4.0/cert-manager.yaml