/API_JustifyText

A simple REST API on Node.JS that justifies a text passed as a parameter

Primary LanguageTypeScript

API_JustifyText

A simple REST API on Node.JS that justifies a text passed as a parameter.

Project Description

This app implements a REST API using Node.JS/Typescript, it consists of two endpoints: /api/token and /api/justify. To obtain access to the /api/justify endpoint, a token must be acquired by sending a POST request to /api/token with a JSON body containing the email address. Once authenticated, sending a POST request with text content to /api/justify will return the justified text. The justification algorithm is custom-built. Any other routes will give you an 404 Not Found error.

A daily rate limit of 80,000 words is enforced, and the length of each justified text line is limited to 80 characters.

The app covers aspects such as authentication, algorithm implementation, string manipulation, Node.js environment setup (including routes, controllers, and middlewares), database management, error handling, deployment of the API, and Dockerization.

NodeJS Express.js Postgres Docker JWT GitHub Jest AWS

Structure

Here's the structure of the project directory

API_JustifyText/
├── docker-compose.yml
├── .env
├── README.md
└── api/
    ├── src/
    │   ├── controllers/
    │   ├── db/
    │   ├── middleware/
    │   ├── models/
    │   ├── routes/
    │   ├── utils/
    │   └── server.ts
    ├── tests/
    ├── Dockerfile
    ├── package.json
    ├── package-lock.json
    ├── tsconfig.json
    └── jest.config.js

Usage

To use this API, you can follow these steps:

  1. Obtain a unique Token by sending a POST request to http://16.171.206.86:3000/api/token with a JSON body:
    curl --request POST \
     --url 'http://16.171.206.86:3000/api/token/?email=test%40example.com' \
     --header 'Content-Type: application/json' \
     --data '{ "email":"test@example.com" }'
  2. Make a POST request with the header "Authorization" and with the value "Bearer ${YOUR_TOKEN}" to http://16.171.206.86:3000/api/justify with a plain/text body as Content-Type for getting your text justified. Here's the cURL command:
    curl -X POST \
    -H "Authorization: Bearer YOUR_TOKEN" \
    -H "Content-Type: text/plain" \
    -d "YOU VERY LONG TEXT YOU WANT TO BE JUSTIFIED" \
    http://16.171.206.86:3000/api/justify

I recommend to use Insomnia and a generator of text like lipsum for making requests, it's easier, especially for including text content in the request body and to look at the output. It also memorizes the URL and the all the headers values.

How run locally the project

Prerequisites

You'll need to have docker-compose, please refer to the official Docker documentation to install it.

Installation

If you want to run locally this project on your computer, you can clone this repo:

  1. Clone the repo
git clone https://github.com/trobert42/API_JustifyText.git
cd API_JustifyText/
  1. Set the environment .env
POSTGRES_USER='your_username'
POSTGRES_PASSWORD='your_pwd'
JWT_SECRET='your_secret'
  1. Change the variables as you desire
const wordsLimitPerDay = 80000;
const lineLenLimit = 80;
  1. Build and run the containers with docker-compose
docker-compose up --build -d # "-d" flag stands for "detached" mode, it starts the services in the background
docker-compose logs -f # "-f" stands for "follow", it allows you to continuously see and follow the output of the logs from the container

Some helpful commands to stop containers and clean volumes

 docker-compose -f docker-compose.yml down -v #watch out, it cleans the entire database
 docker system prune -a -f --volumes

💬 Notes

Difficulties

What was really challenging is for me to apply the right methods to handle the whole project. My formation is based on self-taught so every informations come from forums, videos, articles on the internet. Sometimes, some informations are pretty old or deprecated because tools are updated meanwhile or there are just better ones today.

I used Nest.Js before and this framework is a bit different for the structure project process compared to this one. But i manage to find some logic on how i should structure the folders/files and keep it simple.

One thing that was unknown to me is the sql environment, i used Prisma as a ORM before and for this little assessment, with no use of ORM tools, i had to look for sql commands to access values in my db. Thankfully, i just needed to retrieve data or updates some of it, so nothing really complex.

I find the justifyText algorithm very vague. Even on these online websites that justifies your text, it does not give everytime the same output. At first i had a problem with the ‘\n’ character because i didn't manage it. I realized a working algorithm but i know there are better and optimal ways to do it.

An another thing i don’t have are the good practices for the commits and git managing. I dont know when i have to commit or how i should write my message so its professional.

What was easier

For the final project of my web school common core, I focused on authentication and API integration, so this task didn't feel new to me. I was already familiar with the Backend/Docker environment, and there's a lot of documentation available.

I enjoyed working on this small project, although I did spend a bit too much time on the text justification algorithm. Nonetheless, I was able to learn more about AWS and unit testing, which are new technologies to me.

Particularities of the projet

This assessment serves as an exercise, and it is acknowledged that some aspects of the project may lack practical relevance or context.

There is one unique token which is given by the endpoint /api/token. If you lose it, you won't be able to receive it again and you can't use the same email because credentials will be taken.

This token will not expire.

Words are defined by spaces, so "-" or "123" are valids.

Every day at midnight, the word count is reset to zero. If you have 10 words left free, you can't put a request with body more than 10 words long.

Documentation

If you want to follow how i managed the project throrough the days, here's my notion's page ⬅️ about this project

Nodejs project init

Online justify text tools

Nodejs Middleware

Others tools (pool, node-cron)

Deployment with AWS

Jest for Test

Upcoming Features

How to run tests with Jest

I did not yet implemented all the tests. Stay tuned, i'll work on it.