This project is designed to provide a clean architecture for the MeLi API.
- Node.js 20.0.0 or above
- Clone the project
git clone https://github.com/mau-io/meli-clean-architecture.git
- Go to the project directory
cd meli-clean-architecture
- Install dependencies
npm install
- Start the server
npm run start
This project uses Node.js and Express.js. To run this project, you will need to add the following environment variables:
PROJECT_NAME
: The name of the projectENVIRONMENT
: The environment where the server is running. It could be 'development' or 'production'PROJECT_VERSION
: The current version of the projectSERVER_HOSTNAME
: The hostname of the serverSERVER_PORT
: The port where the server will listeningMELI_SERVICE_URL
: The URL of the Mercado Libre API serviceMELI_SERVICE_TOKEN
: The token for the Mercado Libre API serviceMELI_SERVICE_FAKE_TOKEN
: This is a mock token for the Mercado Libre API service. When this token is used, actual requests to the Mercado Libre API will not be made. Instead, mocked data will be returned.MELI_SERVICE_TIMEOUT
: The timeout setting for the Mercado Libre API serviceMELI_SERVICE_ITEMS_CACHE_TTL
: The time-to-live setting for the Mercado Libre API items cacheMELI_SERVICE_SEARCH_CACHE_TTL
: The time-to-live setting for the Mercado Libre API search cache
You can add these variables in a .env
file at the root of your project:
PROJECT_NAME=MeLi
ENVIRONMENT=development
PROJECT_VERSION=0.0.0
SERVER_HOSTNAME=localhost
SERVER_PORT=3030
MELI_SERVICE_URL=https://api.mercadolibre.com
MELI_SERVICE_TOKEN=xxxxx
MELI_SERVICE_FAKE_TOKEN=xxxxx
MELI_SERVICE_TIMEOUT=3500
MELI_SERVICE_ITEMS_CACHE_TTL=20000
MELI_SERVICE_SEARCH_CACHE_TTL=5000
To install this project follow these steps:
- Clone the repository
git clone https://github.com/mau-io/meli-clean-architecture.git
- Install the dependencies
npm install
- Start the project
npm run start
In the project directory, you can run:
Starts the application in the development mode.
Open http://localhost:3030 to view it in the browser.
This script is used to start the server and watch for any changes in the 'src' directory. The server will automatically restart whenever changes are detected.
This script is used to start the Jest test runner.
This script is used to run ESLint which is a static code analysis tool for identifying problematic patterns found in JavaScript code. It's used for checking the code for readability, maintainability, and functionality errors.
This script is used to generate documentation for the project using JSDoc.
This API provides functionality to interact with the Mercado Libre marketplace. With this API, you can search for items and get item details. It provides endpoints to perform these operations.
GET
/api/v1/search
(Performs a search for items in a specific Mercado Libre site)
name type data type description query
required string The search term site
required string The Mercado Libre site for the search. sort
optional string The sorting order. limit
optional number The maximum number of results to return. offset
optional number The number of results to skip before starting to return results.
http code content-type response 200
application/json
{ "paging": { "total": Number, "offset": Number, "limit": Number }, "categories": [String, ...], "items": [ { "id": String, "title": String, "price": { "currency": String, "amount": Number, "decimals": Number }, "picture": String, "condition": String, "free_shipping": Boolean }, ... ] } ``` |
curl -X GET -H "Content-Type: application/json" -H "x-auth-token: your_token" "http://localhost:3030/api/v1/search?query=iphone&site=MLA"
GET
/api/v1/items/:id
(Fetches details of a specific item)
name type data type description id
required string The Mercado Libre item Id.
http code content-type response 200
application/json
{ "author": { "name": String, "lastname": String }, "item": { "id": String, "title": String, "price": { "currency": String, "amount": Number, "decimals": Number }, "picture": String, "condition": String, "free_shipping": Boolean, "sold_quantity": Number, "description": String } } ``` |
curl -X GET -H "Content-Type: application/json" -H "x-auth-token: your_token" "http://localhost:3030/api/v1/items/MLA816019440"
sequenceDiagram
participant Client
participant ExpressController
participant AppController
participant UseCase
participant DataRepository
participant HttpClient
Client->>ExpressController: API Request (Client makes an API request to the server)
ExpressController->>AppController: forwardRequest() (ExpressController forwards the request to AppController)
AppController->>UseCase: execute() (AppController executes the corresponding use case)
UseCase->>DataRepository: read() (UseCase asks for data from the repository)
DataRepository->>HttpClient: fetch() (DataRepository uses HttpClient to get data)
HttpClient-->>DataRepository: HTTP Response (HttpClient returns the HTTP response)
DataRepository-->>UseCase: Data (DataRepository provides the fetched data to UseCase)
UseCase-->>AppController: Data (UseCase returns the data to AppController)
AppController-->>ExpressController: Response Data (AppController provides the response data to ExpressController)
ExpressController-->>Client: API Response (ExpressController sends the API response back to the client)
This project uses JSDoc to generate its documentation. The documentation will include details about all the JavaScript files found in the ./src
directory.
To generate the documentation, we use the following command:
npm run docs
After running the command, the generated documentation will be available in the ./docs/
directory. You can open the index.html
file in your browser to view the documentation.
This tutorial will walk you through the two basic Docker commands: docker build
and docker run
.
The Docker build
command is used to build Docker images from a Dockerfile and a context. The context is the set of files in the directory specified in the command, or in this case, the current directory (designated by the dot).
docker build -t app-meli .
Breaking down this command, -t app-meli
is used to tag the image with the name app-meli
, and .
specifies that the Dockerfile is in the current directory.
After running this command, you'll have a Docker image named app-meli
that's ready to run.
The Docker run
command is used to create a container from a Docker image and start the container.
docker run -p 3030:3030 --env-file .env app-meli
In this command, -p 3030:3030
maps the host's port 3030 to the container's port 3030. This is essential for accessing web services running on the container. Following that, app-meli
specifies the image to create the container from.
Once this command is executed, your application will be running in a Docker container and can be accessed at
http://localhost:3030
.
Once you have your Docker image, you can deploy your application to Kubernetes.
Before applying the deployment, you need to create a ConfigMap from your .env file. You can do this using the following commands:
kubectl create configmap my-config-map --from-env-file=.env
Confirm that the ConfigMap has been successfully created:
kubectl get configmaps my-config-map -o yaml
The Kubernetes manifest can then be applied with:
kubectl apply -f deployment.yaml
To monitor the running Pods, the following commands can be used:
kubectl get pods
kubectl get deployments
kubectl get services
In case the Pods need to be deleted, use:
kubectl delete service my-app
kubectl delete deployment my-app
Now you can access your application through the LoadBalancer on your local machine at http://localhost:3030
.