Skip to content

About

Transactional banking API in Go and PostgreSQL: sqlc-generated queries, deadlock-free transfers via ascending account-ID lock ordering, and PASETO/JWT auth behind a single Maker interface.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

secure_bank

A transactional banking API in Go: accounts, a double-entry ledger, transfers between accounts, and session-based authentication. Built to get the parts that are easy to get subtly wrong actually right — concurrent money movement, token handling, and schema evolution.

What is interesting here

Transfers cannot deadlock. A transfer touches two account rows. Two transfers running in opposite directions between the same pair of accounts will deadlock if each grabs its rows in its own order, and the failure is intermittent enough to survive casual testing. Both updates happen inside one database transaction, and accounts are always locked in ascending account-ID order, so opposing transfers queue instead of deadlocking. There is a concurrency test that runs transfers in both directions at once and asserts the balances still reconcile.

Tokens sit behind one interface. token.Maker has two implementations, PASETO and JWT, and the server depends only on the interface. Swapping the token format is a one-line change in server setup, and both implementations run against the same test suite. Payloads carry expiry, and refresh-token sessions are checked for revocation, owner mismatch and expiry before a new access token is issued.

Queries are generated, not hand-written. sqlc compiles the SQL in db/query into type-safe Go, so a column rename becomes a compile error rather than a runtime one. Migrations are versioned with golang-migrate.

Handlers are tested without a database. gomock generates a Store mock from the interface, so handler tests cover authorisation and validation paths in milliseconds; the data layer has its own integration tests against real PostgreSQL.

Stack

Go · Gin · PostgreSQL · sqlc · golang-migrate · PASETO · JWT · gomock · Docker · GitHub Actions

Running it

make postgres      # start PostgreSQL in Docker
make createdb      # create the database
make migrateup     # apply migrations
make server        # run the API
make test          # run everything
make mock          # regenerate the store mock

docker-compose up brings up the API and the database together; wait-for holds the API back until PostgreSQL is accepting connections.

Layout

api/      HTTP handlers, routing, auth middleware, request validation
db/
  migration/  versioned schema
  query/      SQL that sqlc compiles
  sqlc/       generated type-safe Go
  mock/       gomock-generated Store for handler tests
token/    Maker interface, PASETO and JWT implementations, payloads
util/     config loading, password hashing, random helpers

CI

GitHub Actions runs the full suite against a PostgreSQL service container on every push, and builds the image.

About

Transactional banking API in Go and PostgreSQL: sqlc-generated queries, deadlock-free transfers via ascending account-ID lock ordering, and PASETO/JWT auth behind a single Maker interface.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages