Skip to content

umarhadi/bank-server

Repository files navigation

Bank Server

This service 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.

Demo

This project already deployed, you can access Swagger at https://bank.api.umarhadi.dev/swagger.

Setup local development

Install tools

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 link.

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>

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
  • Install cert-manager:

    kubectl apply -f https://github.com/jetstack/cert-manager/releases/download/v1.4.0/cert-manager.yaml

Deploy to Fly.io

Check out the getting started guide here for installing the Fly CLI. Once you have the CLI installed, you can deploy the app with the following command:

  • Clone the repo:

    git clone https://github.com/umarhadi/bank-server.git && cd bank-server
  • Create postgres database:

    flyctl postgres create

    After that, you will get the database url, save it for later.

  • Set environment variable

    • DB_SOURCE

      flyctl secrets set DB_SOURCE="postgres://postgres:db_password@db_host:5432/postgres?sslmode=disable"

      Replace db_password and db_host with the actual password and host from the database url. Don't delete sslmode=disable part, if you delete it, the app will not be able to connect to the database because connection between the app and the database is not encrypted via fly.io internal network.

    • TOKEN_SYMMETRIC_KEY

      flyctl secrets set TOKEN_SYMMETRIC_KEY="your_token_symmetric_key"

      Replace your_token_symmetric_key with your own 32 symmetric key. You can generate it with this tool or

      openssl rand -hex 64 | head -c 32
    • Another environment variable

      You can change the default value of the environment variable in the fly.toml file.

  • Deploy the app:

    flyctl deploy