# Packages
# README
Arithmetic Calculator API
This project implements an API for arithmetic calculations and user management with authentication. It is developed in Go and uses MySQL as the database.
Table of Contents
- Prerequisites
- Run the Service Locally
- Build the Docker Image
- Run the Service with Docker Compose
- Postman Collection
- Endpoint Description
Prerequisites
Ensure you have the following installed on your local environment:
- Go (version 1.20 or higher)
- Docker and Docker Compose
- Postman
Run the Service Locally
Configuration
-
Clone the repository:
git clone https://github.com/Ignacio-J-Maylin/arithmetic-calculator.git cd arithmetic-calculator
-
Configure environment variables in a
.env
file at the project root:DB_HOST=localhost DB_PORT=3306 DB_USER=admin DB_PASSWORD=admin DB_NAME=arithmetic API_PORT=8080
-
Start a local MySQL database or use Docker (optional):
docker run -d --name arithmetic-db -e MYSQL_DATABASE=arithmetic -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin -e MYSQL_ROOT_PASSWORD=admin -p 3306:3306 mysql:8.0
Execution
-
Install dependencies:
go mod tidy
-
Run the application:
go run main.go
The service will be available at http://localhost:8080
.
Build the Docker Image
-
Build the Docker image with a tag:
docker build -t arithmetic-calculator:v1 .
-
Run the container:
docker run -d -p 8080:8080 --name arithmetic-backend arithmetic-calculator:v1
Run the Service with Docker Compose
- Verify the
docker-compose.yaml
file and ensure the configuration matches your environment. - Start the services:
docker-compose up -d
This will start the backend, frontend, and MySQL database. The backend will be available at http://localhost:8080
, and the frontend will be available at http://localhost:3000
.
Postman Collection
Import the Collection
- Copy the Postman collection JSON content (provided in the project) and save it as
postman_collection.json
. - Open Postman and select Import from the top menu.
- Drag and drop the
postman_collection.json
file or copy and paste its content. - Set the following global variables in Postman:
{{token}}
: Token generated after login.{{refresh_token}}
: Token to refresh the session.
Using the Collection
-
Test the login endpoint:
- Method:
POST
- URL:
http://localhost:8080/api/v1/login
- Body:
{ "username": "[email protected]", "password": "password123" }
The authentication token will be automatically set in Postman's global variables.
- Method:
-
Test other endpoints in the collection, such as
add_credits
,get_credits
, and arithmetic operations.
Endpoint Description
-
Authentication:
POST /api/v1/login
: Logs in and returns an access token.POST /api/v1/signup
: Registers a new user.POST /api/v1/logout
: Logs out.
-
Credit Management:
PUT /api/v1/users/credits
: Adds or removes credits.GET /api/v1/users/credits
: Gets the credit balance.
-
Arithmetic Operations:
POST /api/v1/users/operation
: Performs operations such as addition, subtraction, multiplication, division, etc.
-
Record History:
GET /api/v1/records/history
: Fetches the operation history.DELETE /api/v1/records/delete
: Deletes a specific record.
This README should serve as a comprehensive guide for any developer or tester working with your Go backend project.