This is the backend of a Trading Platform in which user can buy/sell stocks and manage their portfolio along with real time stock price updation. The users also can view the details of individual stocks before making a buy. Also all transactions are stored in database.
-
config: Contains DB configuration. -
controllersThis contains all the ControllersorderController.go:This controls the buy and sell mechanismpriceController.go: This controls the background price updation mechanismstockController.go: This manages the mechanism for get all stocks and get stocks by their id.userController.go: Responsible for registering user and getting user information.
-
databasedb.go: This contains DB connection and seeding of stocks in DB.
-
modelsuser.go: Defines schema for userstock.go: Defines schema for stocksportfolio.go: Defines schema for portfolioorder.go: Defines schema for order made
-
routes: Contains all the routes
- Clone the Repository
git clone https://github.com/ABHINAVGARG05/trading-platform- Install Dependencies
go mod tidy- Install Air
go install github.com/air-verse/air@latest
air init- Run Dockerized
docker-compose up --build- Install Docker and Docker Compose:Ensure Docker and Docker Compose are installed on your server.
sudo apt update
sudo apt install docker.io docker-compose -y- Clone Repository on Server:
git clone https://github.com/ABHINAVGARG05/trading-platform3)Set Up Environment Variables:
Create a .env and docker-compose.yml files with environment variables for database configuration.
4)Build and start the application
docker-compose up --build1)Horizontal Scaling
using Docker-compose.yml file scaling can be done by running multiple instances
2)Database Scaling To support a large number of users and transactions, consider using managed database services like Amazon RDS, which provide high availability and automated backups.
3)Cache Layer Use Redis or Memcached to cache frequently requested data (e.g., stock prices) and reduce database load.
- Endpoint:
POST /api/v1/user/register - Request Body:
{
"ID": "userid",
"Username": "username",
"Balance": "Balanace"
}- Endpoint:
POST /api/v1/stocks - ResponseBody:
[
{
"ID": "number",
"Symbol": "string",
"Price": "number",
"Volume": "number",
"Supply": "number",
"Demand": "number",
"CreatedAt": "string",
"UpdatedAt": "string"
}
]- EndPoint:
POST /api/v1/stock/id - Response Body:
{
"type": "object",
"properties": {
"ID": {
"type": "integer"
},
"Symbol": {
"type": "string"
},
"Price": {
"type": "number"
},
"Volume": {
"type": "integer"
},
"Supply": {
"type": "integer"
},
"Demand": {
"type": "integer"
},
"CreatedAt": {
"type": "string",
"format": "date-time"
},
"UpdatedAt": {
"type": "string",
"format": "date-time"
}
}
}- Endpoint:
api/v1/stock/buy - Request Body:
{
"user_id": user-id ,
"stock_id": stock-id,
"quantity":quantity
}- Response Body:
{
"type": "object",
"properties": {
"ID": {
"type": "number"
},
"UserID": {
"type": "number"
},
"StockID": {
"type": "number"
},
"Type": {
"type": "string"
},
"Quantity": {
"type": "number"
},
"Price": {
"type": "number"
},
"CreatedAt": {
"type": "string"
},
"UpdatedAt": {
"type": "string"
}
}
}- EndPoint:
api/v1/stock/sell - Request Body:
{
"user_id": user-id ,
"stock_id": stock-id,
"quantity":quantity
}- Response Body:
{
"type": "object",
"properties": {
"ID": {
"type": "number"
},
"UserID": {
"type": "number"
},
"StockID": {
"type": "number"
},
"Type": {
"type": "string"
},
"Quantity": {
"type": "number"
},
"Price": {
"type": "number"
},
"CreatedAt": {
"type": "string"
},
"UpdatedAt": {
"type": "string"
}
}
}- Endpoint:
api/v1/user/id - Response Body:
{
"type": "object",
"properties": {
"user": {
"type": "object",
"properties": {
"ID": {
"type": "number"
},
"Username": {
"type": "string"
},
"Balance": {
"type": "number"
},
"CreatedAt": {
"type": "string"
},
"UpdatedAt": {
"type": "string"
}
}
},
"portfolio": {
"type": "array",
"items": {
"type": "object",
"properties": {
"ID": {
"type": "number"
},
"UserID": {
"type": "number"
},
"StockID": {
"type": "number"
},
"Quantity": {
"type": "number"
},
"AvgPrice": {
"type": "number"
},
"CreatedAt": {
"type": "string"
},
"UpdatedAt": {
"type": "string"
}
}
}
}
}
}- Use Postman
- Import Postman Collection to test the API endpoints.
- Set the environment variables if necessary.