/go-automatic-chainsaw

Full Back-end system for simple bank app based on GO, boost by [postgres/Kubernetes/gPRC/sqlc/docker/gin/...]

Primary LanguageGoMIT LicenseMIT

go-automatic-chainsaw -> bank-app [WIP]

Roadmap

Section I : Working with database [Postgres]

๐Ÿ—’ 1. dbdiagram.io Design DB schema and generate SQL code and PDF as a report.



๐Ÿณ 2. init docker create container/images and run for test

  • postgres:latest hub
  • run database migration in Golang golang-migrate after this [check migrate --version]
    mkdir -p db/migration
    migrate create -ext sql -dir db/migration -seq init_schema  #you will see up/down migration files
    create Makefile in your root (to create/drop db with ease)
    make createdb # you should see db by name bank_db
     # after migrate up to find out migration version and stats
    ls -l db/migration


๐Ÿ—„๏ธ 3. initial type-safe SQL compiler by sqlc

also you can use ORM like GORM but after some research like.1, like.2,... we decide to use sqlc

-   install sqlc
docker pull sqlc/sqlc #then use docker run ...
#or
yay sqlc

then init sqlc and generate sqlc config like here , insure use postgres config

then create all needed schemas files like here and queries files like here based on your db find sample here

then run

make sqlc # after this ./db/sqlc directory should contain generated go files [account/entry/transfer/.../db/model]

then you can write your own custom query like AddAccountBalance or default standard CRUD and enjoy



๐Ÿงช 4. init Unit test by testify

go get github.com/stretchr/testify #install package
go mod tidy #to update all used packages

then init your main_test.go for all test connection to db

now you can your test by sample for your account CRUD like this.

then run

make test # run all test by coverage

after run you can check success coverage code by highlights


๐Ÿ‘จโ€๐Ÿ’ป 5. init CI/CD github Actions

  • to init git hub actions fot GO see the doc
  • then you need to setup postgres in your ci config see doc
  • then you need to setup migration in your ci config see doc

final config should look like this


๐Ÿธ 6. Implement RESTful HTTP API using Gin and Mock DB for testing HTTP API using Gomock

  • after install gin :

    • create api folder and server.go file for base service configuration
    • create main.go main.go file in root to init go for db,server
    • after make server you can use Thunder Client or postman to check and test api
  • after install gomock :

    • check installed is correctly by mockgen -version
    • make Store as interface and make Querier by set emit_interface: true in sqlc
    • create mock folder in db dir and :
      make mock #path project go package / path store (db/sqlc)
      check store.go

โš™๏ธ 7. Extra Configurations

  • ๐Ÿ”ฐ install viper as a configuration file solution.

    go get github.com/spf13/viper

    then create app.env file to define configs. and config.go file to load configs in app


  • ๐Ÿ”ฐ install paseto as a Platform-Agnostic Security Tokens. also you can use JWT, but after some research we decide to use paseto

    go get -u github.com/o1egl/paseto

    then create maker file to define base auth-token flow. and paseto_maker file to define paseto configs.

    ๐ŸŸจ Initial auth-middleware to handle user access to apis just by right token


๐Ÿณ 8. Dockerize app for release and [Build a minimal Golang Docker image with a multistage Dockerfile]

  • first create Dockerfile in root dir then go to docker-hub to find-out golang official image for docker write basic config for build your go app and run :

    docker build -t go-automatic-chainsaw:latest . #tag with name of app and base path

    so let's make it ease with docker-compose

    docker compose up