A modern digital exchange system built with Go, utilizing Wire for dependency injection and Cobra for command-line interface management. This project provides a RESTful API for managing exchange operations, integrated with a PostgreSQL database.
- Overview
- Features
- Prerequisites
- Installation
- Usage
- Configuration
- Development
- Running with Docker
- API Documentation
- Contributing
- License
DigitalExchange is a backend application designed to handle exchange-related operations such as order management, balance tracking, and order book updates. It leverages:
- Go as the primary programming language.
- Wire for dependency injection to manage service and repository dependencies efficiently.
- Cobra to provide a command-line interface for running and managing the application.
- PostgreSQL as the database backend, managed via Docker.
The project structure includes API controllers, models, database migrations, and services, making it extensible and maintainable.
- RESTful API for creating and managing orders.
- Balance management for different assets.
- Order book tracking for exchange markets.
- CLI commands for application control using Cobra.
- Dockerized deployment with PostgreSQL integration.
- Go (version 1.20 or higher)
- Docker and Docker Compose (for running the application and database)
- Git (for cloning the repository)
- PostgreSQL client (optional, for manual database management)
- Clone the repository:
git clone https://github.com/Amin-mhr/DigitalExchange.git cd DigitalExchange - Install dependencies:
go mod download
- Install Wire (for dependency injection):
go install github.com/google/wire/cmd/wire@latest
- Install Cobra (for CLI):
go install github.com/spf13/cobra-cli@latest
- Ensure the
.envfile is configured with the correct environment variables (see Configuration). - Run the application using the Cobra CLI:
This starts the API server on the port specified in
go run main.go app bootstrap
.env(default: 8443).
Use go run main.go --help to see available commands. Example:
go run main.go app bootstrap: Starts the API server.go run main.go database migrate up: Runs database migrations.
Copy the .env.example file to .env and update the following variables:
# App
APP_NAME=exchange
APP_ENV=local
APP_PORT=8443
APP_HOST=localhost
APP_TZ=Asia/Tehran
# Database
DB_DRIVER=postgres
DB_HOST=postgres
DB_PORT=5431
DB_DATABASE="DigitalExchange-api"
DB_USERNAME="DigitalExchange"
DB_PASSWORD="myawesomepassword"
DB_SSL_MODE=disable
DB_MAX_OPEN_CONNECTIONS=100
DB_MAX_IDLE_CONNECTIONS=50
DB_CONNECTION_MAX_LIFETIME=3600- Note: Set
DB_HOST=postgresto connect to the Dockerized PostgreSQL service.
- Start the development environment:
go run main.go app bootstrap
- Test the API using tools like Postman or
curl.
Run the following command to generate Wire bindings:
wireEnsure the wire.go file is updated in the appropriate package (e.g., wire_gen.go).
Run migrations to set up the database schema:
go run main.go database migration upand run below command for rollback migrations:
go run main.go database migration downRun Seeder files to set up the data in schema:
go run main.go database seed runthis will fill database tables with test data.
- Build and start the services:
docker compose up --build
- Access the API at
http://localhost:8443and the database atlocalhost:5431.
docker compose downThe API includes endpoints for order management. Example request (using Postman):
- Endpoint:
POST /api/exchange/order/create - Body:
{ "exchange_name": 1, "client_order_id": "buy-btc-limit-20250530-001", "symbol": "BTCUSDT", "side": "buy", "type": "limit", "quantity": 0.2, "price": 60000.0, "time_in_force": "GTC" } - Refer to the
api/controllerspackage for more endpoints.
- Fork the repository.
- Create a new branch:
git checkout -b feature-branch. - Make your changes and commit:
git commit -m "Add new feature". - Push to the branch:
git push origin feature-branch. - Submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.


