205 lines
7.8 KiB
Markdown
Executable File
205 lines
7.8 KiB
Markdown
Executable File
# Bankline — Online Payment System Backend
|
|
|
|
Bankline is the HTTP backend for an online payment platform. It acts as the main API gateway: it accepts client requests, handles admin authentication, stores data in PostgreSQL, and manages file assets on local disk.
|
|
|
|
## Overview
|
|
|
|
The service is built as a modular Go application. Admin-facing endpoints are exposed under `/v1/admin`. JWT-based authentication protects sensitive routes. Swagger is available for interactive API exploration.
|
|
|
|
### Current capabilities
|
|
|
|
- **Admin authentication** — login with email/password, JWT access and refresh tokens
|
|
- **Token refresh** — exchange a valid refresh token for a new token pair
|
|
- **Admin profile** — retrieve authenticated admin profile data
|
|
- **File storage** — upload, serve, and delete files from the local `public/` directory
|
|
- **API documentation** — Swagger UI at `/swagger/index.html`
|
|
|
|
### Planned / in progress
|
|
|
|
Additional Swagger endpoints (categories, settings) are documented but not yet implemented.
|
|
|
|
## Tech stack
|
|
|
|
| Layer | Technology |
|
|
|-------|------------|
|
|
| Language | Go 1.26 |
|
|
| HTTP framework | [Gin](https://github.com/gin-gonic/gin) |
|
|
| Database | PostgreSQL 14 |
|
|
| DB driver | [pgx](https://github.com/jackc/pgx) |
|
|
| Auth | JWT (HS256) + bcrypt password hashing |
|
|
| API docs | [Swaggo](https://github.com/swaggo/swag) |
|
|
| Config | YAML via [cleanenv](https://github.com/ilyakaznacheev/cleanenv) |
|
|
| Migrations | [golang-migrate](https://github.com/golang-migrate/migrate) |
|
|
| Containerization | Docker & Docker Compose |
|
|
|
|
## Architecture
|
|
|
|
```
|
|
┌─────────────┐ HTTP ┌──────────────────┐
|
|
│ Clients │ ────────────► │ Bankline API │
|
|
│ (Admin UI) │ │ (Gin, :5050) │
|
|
└─────────────┘ └────────┬─────────┘
|
|
│
|
|
┌────────┴────────┐
|
|
▼ ▼
|
|
┌────────────┐ ┌────────────┐
|
|
│ PostgreSQL │ │ Logs │
|
|
│ (main-db) │ │ ./logs/ │
|
|
└────────────┘ └────────────┘
|
|
```
|
|
|
|
### Project structure
|
|
|
|
```
|
|
.
|
|
├── app.go # Application entry point
|
|
├── config.yaml # Runtime configuration
|
|
├── docker-compose.yaml # Local development stack
|
|
├── db/migrations/ # SQL migrations
|
|
├── db/seed.sql # Consolidated, idempotent seed data
|
|
├── docs/ # Generated Swagger docs
|
|
├── internal/
|
|
│ ├── appresult/ # Response helpers & JWT middleware
|
|
│ ├── config/ # Config loader
|
|
│ ├── shared/ # Cross-module DTOs, HTTP/SQL/query helpers
|
|
│ ├── handlers/manager/ # Route registration (composition root)
|
|
│ ├── identity/ # auth, rbac, permission, role, user, userbranch, activitylog
|
|
│ ├── cards/ # card orders, balances, PIN orders, rekwizit, etc.
|
|
│ ├── lending/ # loan orders, repayments, documents, letters, types
|
|
│ ├── org/ # branches, provinces
|
|
│ ├── currency/, dashboard/, backup/
|
|
└── pkg/
|
|
├── client/postgresql/ # PostgreSQL connection pool
|
|
├── repository/genericrepo/ # Shared List/GetByID/Delete repository helper
|
|
├── logging/ # Structured logging
|
|
└── utils/ # JWT & file utilities
|
|
```
|
|
|
|
## Getting started
|
|
|
|
### Prerequisites
|
|
|
|
- [Docker](https://www.docker.com/) and Docker Compose
|
|
- [Go 1.26+](https://go.dev/dl/) (for local development)
|
|
- [golang-migrate CLI](https://github.com/golang-migrate/migrate/tree/master/cmd/migrate) (for database migrations)
|
|
|
|
### Run with Docker (recommended)
|
|
|
|
1. Clone the repository:
|
|
|
|
```bash
|
|
git clone <repository-url>
|
|
cd online-back
|
|
```
|
|
|
|
2. Start all services:
|
|
|
|
```bash
|
|
docker compose up -d --build
|
|
```
|
|
|
|
3. Apply database migrations (from the host, with PostgreSQL exposed on port `5424`):
|
|
|
|
```bash
|
|
make migrate
|
|
```
|
|
|
|
The API will be available at `http://localhost:5050`.
|
|
|
|
### Docker services
|
|
|
|
| Service | Container | Port(s) | Description |
|
|
|---------|-----------|---------|-------------|
|
|
| API | `main-app` | `5050` | Bankline HTTP server |
|
|
| Database | `main-db` | `5424 → 5432` | PostgreSQL |
|
|
|
|
### Run locally (without Docker)
|
|
|
|
1. Start PostgreSQL (or use `docker compose up main-db -d`).
|
|
|
|
2. Copy `config.example.yaml` to `config.yaml`, set `storage.host` to `localhost`, and fill in your database credentials and a JWT secret.
|
|
|
|
3. Apply migrations:
|
|
|
|
```bash
|
|
make migrate
|
|
```
|
|
|
|
4. Create required directories:
|
|
|
|
```bash
|
|
mkdir -p logs public/images
|
|
```
|
|
|
|
5. Run the application:
|
|
|
|
```bash
|
|
go run app.go
|
|
```
|
|
|
|
> **Note:** Replace default credentials before deploying to production.
|
|
|
|
|
|
|
|
### Authentication
|
|
|
|
Protected endpoints require an `Authorization` header with a valid JWT access token:
|
|
|
|
```
|
|
Authorization: <accessToken>
|
|
```
|
|
|
|
|
|
|
|
### Swagger
|
|
|
|
Interactive API docs: [http://localhost:5005/swagger/index.html](http://localhost:5005/swagger/index.html)
|
|
|
|
## Database migrations
|
|
|
|
Requires the [golang-migrate](https://github.com/golang-migrate/migrate) CLI.
|
|
|
|
```bash
|
|
# Create a new migration
|
|
make create-migration name='migration_name'
|
|
|
|
# Apply all pending migrations
|
|
make migrate
|
|
|
|
# Roll back the last migration
|
|
make migrate-rollback
|
|
```
|
|
|
|
Migration files live in `db/migrations/`.
|
|
|
|
> **Current practice:** the schema currently lives in a single `db/migrations/20260702125108_init.{up,down}.sql` pair, edited in place as the schema evolves rather than appended to. This means an already-migrated database won't pick up later edits to `init` automatically (`golang-migrate` sees it as already applied) — a fresh `make migrate` against an empty database is the reliable path. Switching to append-only migrations going forward is worth deciding on as a team.
|
|
|
|
Seed data (`db/seed.sql`) loads a consolidated, idempotent set of test accounts/roles/branches:
|
|
|
|
```bash
|
|
make seed
|
|
```
|
|
|
|
## Roles & permissions (RBAC)
|
|
|
|
- **Permission = module.** One code per module (`cards`, `loans`, `users`, `roles`, `branches`, `provinces`, `currencies`, `payment-settings`, `backups`, `activity-logs`, `dashboard`), defined in `internal/identity/rbac/perm`. Holding it grants read and write in that module. The catalog is synced to the `permissions` table on startup; the API only lets roles pick from it.
|
|
- **Role = permissions + scope.** `scope` decides whose records: `own` (clients — only their own), `branch` (the user's `branch_user` branches, possibly several), `all` (every branch). E.g. a head of loans is `scope=all` + `loans`. `is_super` roles hold every permission; `is_system` roles (`superadmin`, `user`) cannot be deleted or re-scoped.
|
|
- **One role per user:** `users.role_id`.
|
|
- **Every `/api/admin` route declares its policy** with `rbac.Group(router, path, policy)` — `Require(perm.X)`, `RequireStaff`, `Catalog`, `StaffRead`, `Staff`, `Authenticated`, `Public`. Startup fails if a route has none, and an undeclared route is denied at runtime.
|
|
- **No escalation:** a caller can only create/assign roles within their own scope and permissions, cannot touch superadmin unless superadmin, cannot change their own role, and cannot remove the last superadmin.
|
|
- Principals are cached per user; DB triggers bump `rbac_version` on role/permission/branch changes, which clears the cache within ~2s.
|
|
|
|
## Development
|
|
|
|
### Run tests
|
|
|
|
```bash
|
|
mkdir -p logs
|
|
go test ./...
|
|
```
|
|
|
|
### Regenerate Swagger docs
|
|
|
|
```bash
|
|
swag init -g app.go
|
|
``` |