/fitmeup-backend

This is the Fitmeup App Backend repository for 3th edition of Devathon of PEE

Primary LanguageJavaScriptMIT LicenseMIT

Fitmeup App Backend

This is the backend of Fitmeup App of 3th edition of PEE Devhaton.

1. Description

Platform (social network like) for gyms, the idea is to have admins able to register gyms with the address and other details, adquire certifications, etc. Users will be able to signup in the registered gyms in the platform, associate themselves with the gym they're working out in and their registration will be validated for the gym as well. The users might publish opinions and feedback about the gym they're attending to.

Stack

  • NodeJS
  • Express
  • MongoDB
  • Docker and docker-compose

2. Start the project

2.1 Environment file

Env name Description
MONGO_USER Username to connect to database
MONGO_PASS Password to connect to the database
MONGO_URL Connection string to connect to database. It must follow this structure: mongodb://[MONGO_USER]:[MONGO_PASS]@[SERVER_NAME]:[DB_INCOMING_PORT], SERVER_NAME: localhost or docker-compose service name (in our case mongo)
API_INCOMING_PORT This is the port that clients are going to send requests
API_OUTCOMING_PORT This is the port that server is going to give responses
API_DEBUG_INCOMING_PORT The same above but for debugging purposes inside docker contianer
API_DEBUG_OUTCOMING_PORT The same above but for debugging purposes inside docker contianer
DB_INCOMING_PORT This is the port that database service will listen for new requests
DB_OUTCOMING_PORT This is the port that database service will respond the requests
MONGO_MANAGER_INCOMING_PORT This is an extra service to manage database via interface, this value defines the port that this service is going to recieve the requests
MONGO_MANAGER_OUTCOMING_PORT This is an extra service to manage database via interface, this value defines the port that this service is going to respond the requests
TOKEN_SECRET This value is for signing access tokens, it must be an alphanumeric string, for example: CCjgVyPcY1N4FDlI9vPtXpz4234OOMBp5UNUyogciNJQjMIM26Td
REFRESH_TOKEN_SECRET The same as TOKEN SECRET but for signing refresh tokens
TOKEN_EXP_TIME This value indicates how long the access token is going to be valid. Eg: 60, "2 days", "10h", "7d". A numeric value is interpreted as a seconds count. If you use a string be sure you provide the time units (days, hours, etc), otherwise milliseconds unit is used by default ("120" is equal to "120ms").
REFRESH_TOKEN_EXP_TIME This value indicates how long the refresh token is going to be valid. Eg: 60, "2 days", "10h", "7d". A numeric value is interpreted as a seconds count. If you use a string be sure you provide the time units (days, hours, etc), otherwise milliseconds unit is used by default ("120" is equal to "120ms").
BCRYPT_SALT This value indicates number of rounds to generate the password, it must be a number.
SUPERADMIN_EMAIL Email of super admin of the application, it must be a gmail account and have two step verification enabled
SUPERADMIN_PASS Password of super admin of the application, it must be an application password gotten in https://myaccount.google.com/security after enabling two step verification
API_LIMIT_CONTENT_LENGTH The limit of request body JSON, check Content-Length HTTP header
CONFIRMATION_TOKEN_SECRET The same as TOKEN SECRET but for signing confirmation tokens

2.2 Local

2.2.1 Prerequisits

  • NodeJS and NPM Download icon
  • MongoDB Download icon

We have to create an .env file in the project's root, then copy paste the environment variables names and give a value.

echo "MONGO_USER=
MONGO_PASS=
MONGO_URL=
API_INCOMING_PORT=
TOKEN_SECRET=
REFRESH_TOKEN_SECRET=
TOKEN_EXP_TIME=
REFRESH_TOKEN_EXP_TIME=
BCRYPT_SALT=
SUPERADMIN_EMAIL=
SUPERADMIN_PASS=
API_LIMIT_CONTENT_LENGTH=
CONFIRMATION_TOKEN_SECRET=
" > <project path>/.env

2.2.2 Start

  1. Set up .env file
  2. Install dependencies
npm install
  1. Start API
npm run dev

2.3 Docker

2.3.1 Prerequisits

  • Docker Desktop Download icon

We have to create an .env file in the project's root, then copy paste the environment variables names and give a value.

echo "MONGO_USER=
MONGO_PASS=
MONGO_URL=
API_INCOMING_PORT=
API_OUTCOMING_PORT=
API_DEBUG_INCOMING_PORT=
API_DEBUG_OUTCOMING_PORT=
DB_INCOMING_PORT=
DB_OUTCOMING_PORT=
MONGO_MANAGER_INCOMING_PORT=
MONGO_MANAGER_OUTCOMING_PORT=
TOKEN_SECRET=
REFRESH_TOKEN_SECRET=
TOKEN_EXP_TIME=
REFRESH_TOKEN_EXP_TIME=
BCRYPT_SALT=
SUPERADMIN_EMAIL=
SUPERADMIN_PASS=
API_LIMIT_CONTENT_LENGTH=
CONFIRMATION_TOKEN_SECRET=
" > <project path>/.env

2.3.2 Start

  1. Set up .env file
  2. Build and run docker containers in detached mode
docker-compose -f docker-compose.dev.yml up -d

3. Postman

We have also a Postman collection to test the API with all endpoints configured and a DEV environment. Please visit our Postman team workspace fitmeuppee.postman.co

4. Debug with Docker

We have by default this project with this tool activated, here we have the following things involved:

  1. VSCode launch.json file
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Docker: Attach to Node",
            "type": "node",
            "request": "attach",
            "port": 9229,
            "address": "localhost",
            "localRoot": "${workspaceFolder}/src",
            "remoteRoot": "/app/src",
            "protocol": "inspector",
            "skipFiles": [
                "${workspaceFolder}/node_modules/**/*.js",
                "<node_internals>/**/*.js"
            ]
        }
    ]
}
  1. In Dockerfile.dev file we have already the option to start the API with debug mode
CMD ["npm", "run", "debug"]
  • If you want to start the API without a debug mode just replace the above command to the following
CMD ["npm", "run", "dev"]
  1. Go to VSCode debug tool and click on play, then put a breakpoint to any endpoint and make a request to this one.

5. Run suite tests

  1. Local
npm run test
  1. Docker
docker-compose -f docker-compose.dev.yml exec api npm run test

Collaborators