This is a simple API that performs basic CRUD for Users and Tasks.
This API uses:
To use the API, clone the repo: https://github.com/Clearbryan/touraxis. Once cloned, navigate to the root folder cd touraxis and install the packages using npm install.
There are a few environment variables required to run the API. In the root folder, create a .env file. Copy and paste the following environment variables:
APP_SECRET=touraxis
PORT=3000
DB_PASS=riYsahtAbMY0b51V
DB_USER=chetekwebrian
DB_NAME=touraxis
Once you've pasted the environment variables into the .env file, the app is ready to start. Run the following command:
npm run touraxisWhen this command is run, two things happen:
- It starts the application on the specified
PORT(from the environment variables). - It spawns a new process that runs a task every 30 minutes and logs the outcome of the task activity to the console.
You may use any REST client to test the API. However, for the purposes of this documentation, we will use Postman. Please note Postman Chrome Extension DOES NOT allow requests over HTTP connections but rather HTTPS. For this reason we need to use the Postman Desktop Client
Some API endpoints are protected and the user needs to be logged in to access them. The API has the following endpoints:
-
http://localhost:{PORT}/api/users/- Accepts aPOSTrequest to create a new user. Example request:{ "username": "yourusername", "first_name": "your first name", "last_name": "your last name", "password": "your password" } -
http://localhost:{PORT}/api/users/login- Accepts aPOSTrequest for user login. Example request:{ "username": "yourusername", "password": "your password" }When successful, this endpoint returns a JSON Web Token that can be used to access the other protected endpoints. Example response:
{ "success": true, "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7Il9pZCI6IjY2Y2YzZjhiNmZlNGNiNDc3YTUyZmQ4MyIsInVzZXJuYW1lIjoiY2xlYXJicnlhbiIsImZpcnN0X25hbWUiOiJCcmlhbiIsImxhc3RfbmFtZSI6IkNoZXRla3dlIiwicGFzc3dvcmQiOiIkMmEkMTAkZ2lDOTQwREFiYUdEY2N4TXRuVU40ZUhaZTZBLzhrdm9nbWlDUU9pdk9xLmtxUTU5b2N5Qi4iLCJfX3YiOjB9LCJpYXQiOjE3MjQ4NjU0OTIsImV4cCI6MTcyNDg2OTA5Mn0.nUcBxvyYeH0YfQ10bX-aYh_J0FduEN-wbAsy4wmxLfU", "user_name": "clearbryan", "user_id": "66cf3f8b6fe4cb477a52fd83" }Please note that the token expires after 60 minutes and you'll need to log in again to get a new token if the token has expired.
For every request to a protected endpoint, this token needs to be included in the request headers as
Authorization: Bearer <token>. See screenshot below: -
http://localhost:{PORT}/api/users- Accepts aGETrequest to list all users. -
http://localhost:{PORT}/api/users/{user_id}- Accepts:GETrequest to retrieve a specific user.PUTrequest to update a user. Example request:
{ "first_name": "John", "last_name": "Doe" }DELETErequest to delete a user.
-
http://localhost:{PORT}/api/users/{user_id}/tasks- Accepts:POSTrequest to create a task. Example request:
{ "name": "Review Code for Security Vulnerabilities", "description": "Conduct a security review of the codebase to identify and address potential vulnerabilities.", "status": "Pending" }By default a task is created with a
next_execute_date_timeset to 24 hrs from the time the task was created. Every 30 minutes, the API runs a shedulled task to check for tasks in a Pending state and if found, check if thenext_execute_date_timehas passed and update the tasks to Done.GETrequest to list all tasks for a specific user.
-
http://localhost:{PORT}/api/users/{user_id}/tasks/{task_id}- Accepts:GETrequest to retrieve a specific task.PUTrequest to update a task. Example request:
{ "name": "Review Code for Security Vulnerabilities", "description": "Conduct a security review of the codebase to identify and address potential vulnerabilities.", "status": "Completed" }DELETErequest to delete a task.
