Skip to content

Project to practice making Restful API in Golang.

Notifications You must be signed in to change notification settings

wiliamhw/simplebank

Repository files navigation

Simplebank

Taken from a Udemy Course named Backend Master Class (Golang + PostgreSQL + Kubernetes).
This project implements all subjects from the course, except AWS, Kubernetes, and gRPC.
I also modified some code on API unit test and application configuration.

Simple bank service

This project will provide APIs for the frontend to do following things:

  1. Create and manage bank accounts, which are composed of owner’s name, balance, and currency.
  2. 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.
  3. 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

Tools

  • Docker

  • TablePlus

  • Golang

  • Migrate

    curl -L https://packagecloud.io/golang-migrate/migrate/gpgkey | apt-key add -
    echo "deb https://packagecloud.io/golang-migrate/migrate/ubuntu/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/migrate.list
    apt-get update
    apt-get install -y migrate
  • DB Docs

    npm install -g dbdocs
    dbdocs login
  • DBML CLI

    npm install -g @dbml/cli
    dbml2sql --version
  • Sqlc

    sudo snap install sqlc
  • Gomock

    go install github.com/golang/mock/mockgen

Setup infrastructure

  • 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

  • The Postman API collection and environtment can be exported using JSON files inside doc/postman folder.

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 mockdb
  • 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