yoelfme / grin-api

API used to register and authenticate users, in addition to giving information about their nearest places of interest and their favorites

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Grin API

This service is the API used to register and authenticate users, in addition to giving information about their nearest places of interest and their favorites.

There is collection in Postman with all the information related to the endpoints exposed by this API, also we you run the server, the servve expose in its base path: / a UI with an implemantation of Swagger, where you can call all the endpoints.

Stack of technologies used

  • Node.js: JavaScript runtime built on Chrome's V8 JavaScript engine, used to run the server.
  • Hapi.js: Node.js framework used to buid application and services, with this we build our server that attend request for users, search and favorite places.
  • Mongo: NoSQL Database data that stores data in flexible, JSON-like document, I use it to save all data about users and places.
  • Redis: Redis is a in-memory data structure store, that has multiple use cases, but I used as a cache, to store all the users sessions and requests.
  • jwt: it's an standard that defines a way for securely transmitting information between parties as a JSON object, in this project we used to validate incoming requests from our users.
  • Jest: Javascript test framework for Node.js and the browser, I use it to build all of the integration tests.
  • Google Places API: An API owned by Google that allow us to query for place information. I used to search for places by query and proximity.

Some questions

Why Google Places API?

I made research about some a lot of services/API that offers information about places, a define their pros and cons, these are the services/API that we found:

  • Forsquare:

    • Pros:
      • Has pagination
      • Has distance from the user
      • Has limit of distance between the user and the places
    • Cons
      • Does not have sorting
      • A lot of unnecesary information of a places
  • Google Places API:

    • Pros:
      • Search by text string
      • Has sort by popularity(prominence) and by distance
      • We can choose the fields that we want
      • A lot of places in his database, and types of places
      • Clear documenation
    • Cons:
      • Does not have distance between the user and the places
      • Doest not have limit of places that we want
  • Here:

    • Pros:
      • Simple to use
      • We can query for text string
    • Cons:
      • Has less places than the other services
      • Does not have pagination
      • Does not have opotion to limit how many places we want

After that, I decide to use Google Places because even that we don't have pagination we can made it in our side, but we have a lot of flexibility to filter places and quick responses, and its documentation is clear.

Why I used for pagination:

I use a plugin for Hapi called hapi-pagination, but I found some limitations with this library, so I decided to fork it and made the changes that works with my use case, this is the final plugin: yoelfme/hapi-pagination, I made the changes with its tests and with that still persists the 100% of coverage 😎 that the plugin had.

Why I use JWT:

Because I think that its implemantation is too easy, and even so we have strong security.

Why I use bcrypt to hash and compare password:

Because it's a good way to prevent our passwords from being stolen or revealed, and compare passwords with bcrypt help us to prevent timing attack when the users made login.

How to Run

There are 2 ways to run the project locally, it could be with Docker or with Gulp

with Docker

To run the project using Docker, you have to follow the next steps:

  1. Install Docker and docker-compose
  2. Copy the example file with environments variables to a new file call .env.docker:
cp .env.example .env.docker
  1. Change the following environments variables:
    • GOOGLE_MAPS_API_KEY: Set your secret key provided by Google Maps
    • REDIS_HOST: Set to redis
    • APP_DB_HOST: Set to mongo
  2. Buid the image locally: docker-compose build
  3. Start to run the containers: docker-compose up, but if you want to run it in background, add the flag -d
  4. Visit localhost:3000

Available Containers

The availables containers are:

  • api: a container with a Node.js image, that run our Hapi Server
  • redis: a container used to manage the session users and cache data
  • mongo: a container used to store all the data related to users and places.

Advantages

  • Running it this way is the closest thing to running it in production

with Gulp

To run the project using Gulp, you have to follow the next steps, but first you need to install Redis and Mongo(also you can use Docker :tonge:), and expose their ports locally.

  1. Install npx
  2. Install nvm, a package used to manage multiple node.js versions
  3. Install the Node.js with nvm, run nvm install in the project root directory
  4. Copy the example file with environments variables to a new file call .env:
cp .env.example .env
  1. Change the following environments variables:
    • GOOGLE_MAPS_API_KEY: Set your secret key provided by Google Maps
  2. Install all the dependencies running: npm i
  3. Run the server with: npx gulp serve
  4. Visit localhost:3000

Tasks

  • serve: used to run the server locally, run it with: npx gulp serve
  • test: used to run all the test cases locally, run it with: npx gulp test

Advantages

  • running in this way, because we use nodemon with Gulp, we can make changes and see them in real time.

Created with ❤️ by Yoel Monzon

About

API used to register and authenticate users, in addition to giving information about their nearest places of interest and their favorites


Languages

Language:JavaScript 99.4%Language:Dockerfile 0.6%