Skip to content

REST apis for a banking system written in go, sqlc, Makefile

License

Notifications You must be signed in to change notification settings

satyajitnayk/bank-apis

Repository files navigation

bank-apis

  • REST apis for a banking system written in go
  • Imporatant notes to learn from this project notes
  • Database Design for this project DB Design
  • Multstage Build for docker & it's advantages Multistage Docker Build

run postgres

# Incase you have local postgres instance running - command to stop it for MAC OS
sudo -u postgres /Library/PostgreSQL/14/bin/pg_ctl -D /Library/PostgreSQL/14/data stop

# to run command inside postgres
docker exec -it postgres12 psql -U root

# check command working in root=#
select now();

# to pull docker image for potgres
docker run --name postgres12 -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=secret -d postgres:12-alpine

# check which containers running
docker ps

# list all conatiner
docker ps -a

# remove a docker conatiner
docker rm <conatinerName/conatinerId>

# start a docker conatiner
docker start <conatinerName/conatinerId>

# stop a runnig docker conatiner
docker stop <conatinerName/conatinerId>

# go inside shell of a conatiner
docker exec -it postgres12 /bin/bash

# create a database inside shell
createdb --username=root --owner=root <db_name>

# enter into the created database
psql <db_name>

#drop database
drop <db_name>

# exit from psql
\q

# exit from shell
exit

Install & use golang-migrate

brew install golang-migrate

# check version
migrate -version

# create initial migration file
migrate create -ext sql -dir db/migration -seq init_schema

# run migration
migrate -path db/migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" -verbose up

# create add_users migration files
migrate create -ext sql -dir db/migration -seq add_users

Usage of Makefile

# create postgres instance in docker
make postgres

# create db
make createdb

...
  • It uses database/sql native pkg & create methods to interact with DB.

    Note: Do not modify content generated by sqlc command

brew install sqlc

#check version
sqlc version

# initialize sqlc => a sqlc.yaml file will be generated
sqlc init

Generate DB Mock Store

mockgen -package mockdb -destination db/mock/store.go github.com/satyajitnayk/bank-apis/db/sqlc Store

Run Docker image for the project

# build inside project root directory
docker build -t simplebank:latest .

# run in dev mode
docker run --name simplebank -p 8080:8080 simplebank:latest

# run in production mode
docker run --name simplebank -p 8080:8080 -e GIN_MODE=release simplebank:latest

# remove docker image
docker rmi simplebank

# remove container
docker rm <conatiner_name/id>

# check n/w config of a conatiner
docker container inspect <<conatiner_name/id>
# final command
docker run --name simplebank --network bank-network -p 8080:8080 -e GIN_MODE=release  -e DB_SOURCE="postgresql://root:secret@postgres12:5432/simple_bank?sslmode=disable" simplebank:latest

Using PASETO for authentication & authorization

Authorization Rules

Action Authorization Rule
Create Account A logged-in user can only create an account for him/herself.
Get Account A logged-in user can only get accounts that he/she owns.
List Accounts A logged-in user can only list accounts that belong to him/her.
Transfer Money A logged-in user can only send money from his/her own account.

About

REST apis for a banking system written in go, sqlc, Makefile

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published