This is a robust, modular, and scalable banking system backend built with Go (Golang). It uses the gorilla/mux router for request handling and follows a clean separation of concerns.
- User Management: Create, retrieve, update, and delete user accounts.
- Transaction Handling: Perform transfers, withdrawals, and deposits. Handle race condition using row-level locking.
- Modular Design: Clear separation between handlers, internal logic (database/transaction), and data models.
- Environment Configuration: Uses a
.envfile for sensitive configuration.
The key components of the application are organized as follows:
| Directory/File | Purpose |
|---|---|
handlers/ |
HTTP request controllers for User and Transaction actions. |
internal/db/ |
Database connection and specific data access methods. |
internal/transaction/ |
Core business logic for handling funds and transfers. |
internal/user/ |
Core business logic for managing user accounts. |
internal/models/ |
Structs defining application data (e.g., User, Transaction). |
main.go |
Application entry point, configuration loading, and route definition. |
schema.sql |
SQL script for creating necessary database tables. |
.env.example |
Template for required environment variables. |
- Go (version 1.24+ recommended)
- A running PostgreSQL or equivalent database instance.
- Docker (optional, for containerized deployment)
-
Clone the repository:
git clone [repo-link] cd banking -
Set up environment variables: Create a file named
.envby copying the example and filling in your database credentials and secrets.cp .env.example .env
-
Install dependencies:
go mod tidy
-
Initialize the database: Run the setup script or manually execute the SQL from
schema.sqlto create tables../setup.sh # OR manually run the contents of schema.sql -
Run the application:
go run main.go
The server will start on
http://localhost:8080.
The API is accessible on port 8080 and handles the following routes:
| Method | Path | Description |
|---|---|---|
POST |
/user |
Creates a new user account. |
GET |
/user/{id} |
Retrieves a specific user by ID. |
GET |
/user |
Retrieves a list of users. |
PUT |
/user/{id} |
Updates user details (e.g., name). |
DELETE |
/user/{id} |
Deletes a user account. |
| Method | Path | Description |
|---|---|---|
PUT |
/transaction/transfer |
Transfers funds between two accounts. |
PUT |
/transaction/withdraw |
Withdraws funds from an account. |
PUT |
/transaction/deposit |
Deposits funds into an account. |
