Skip to content

Repository files navigation

Projeto livraria - curso/módulo 04 - API REST, testes e deploy, da Imersão Alura Javascript de 100 horas (Bookstore project - course/module 04 - REST API, testing and deployment, from the 100-hours Alura Javascript Immersion program)

API / backend of the Bookstore project (Alura Immersion). Here is the web / front-end side: https://github.com/Goliass/imersivo_100h_javascript_expert_full_stack__bookstore_web

Main features

  • system health check
  • login (session creation)
  • listing, creation and deletion of categories
  • listing, creation and deletion of publishers
  • listing, creation, updating and deletion of users (authors)
  • listing, creation, updating and deletion of books

How to set / run the project LOCALLY

Aplication modules

  • install them: $ npm install

Database

  • starting it first time

    $ docker run -d --name bookstore-db --env POSTGRES_PASSWORD=[choose_a_pass] --env POSTGRES_USER=[choose_a_user] --env POSTGRES_DB=bookstore -p 5432:5432 postgres:18.1

    • Write down the user and pass for later use.
  • starting from the second time onwards

    $ docker start bookstore-db

    • note: for the above command work properly, the Docker Desktop application must be running
  • [info] stopping it

    $ docker stop bookstore-db

  • [info] checking it's running or not

    $ docker ps -a

API / application

  • .env file
    • create a .env file with following information:

      PORT=3333 # or choose another
      
      DATABASE_URL=postgresql://[user above]:[pass above]@localhost:5432/bookstore
      
      JWT_SECRET="[your_secret]"
      
      JWT_EXPIRES_IN="5m"
      
      # allowed URLs between commas or "" to allow all URLs
      CORS_ORIGIN=""
      
      # email of the users authorized to execute maintenance routes between commas
      MAINTENANCE_USERS_EMAILS=email1@email.com, email2@email.com
      

Database related

  • [run once] execute migrations (it generates the database tables): $ npx prisma migrate deploy

  • [run once] generate the code (client) that interact with the database: $ npx prisma generate

  • [info] to check the database "visually": $ npx prisma studio

  • [info] To populate the database automatically:

    • configure the environment variable SEED_DB_MOCK_DATA_USER_PASSWORD (in the .env file)
    • execute $ npm run db:populate.
    • ATTENTION: ALTHOUGH IT'S POSSIBLE, THE RECOMMENDATION IS NOT TO EXECUTE IT IN THE PRODUCTION ENVIRONMENT.

API / application

  • starting it

    $ npm run dev

    • [info] stopping it
      $ Ctrl C keys

Verifying if it's working

Registering users (authors), categories, publishers, and books

Option 1: via requests/routes (Postman)

  • download, install and open Postman
  • import the files *environment.json e *collection.json of the project folder postman
  • select the imported environment (section of the left panel "Items") and assign localhost:3333 to the variable API_BASE_URL
  • select the imported collection (section of the left panel "Items")
    • in the upper right corner, select the imported environment
  • returning to the collection previously selected (section of the left panel "Items"), execute (Send button) the "health check" request.
  • if successful execution (the request response status will be "200 OK"), only the first time (while the database is empty / doesn't have a single book registered), execute the following requests in this order:
    • folder users: POST create
    • folder sessions: POST create (login)
    • folder categories: POST create
    • folder publishers: POST create
    • folder books: POST create
    • [info]: For a registered user to be able to execute maintenance routes (POST, PUT, etc), their email must be assigned to the environment variable MAINTENANCE_USERS_EMAILS (in the .env file), and the API (backend) restarted; otherwise, they will ONLY be able to execute the listing (GET) routes.
  • execute the requests above and others as needed.

  • observation: if, after executing a request, the request response status is "401 unauthorized", execute the request "folder sessions: POST create (login)", and then execute that one request again.

Option 2: via website (frontend)

  • [info]: For a registered user to be able to perform maintenance options (create, update and delete) on the site, their email must be assigned to the environment variable MAINTENANCE_USERS_EMAILS (in the .env file), and the API (backend) restarted and the user log in (site / frontend) again; otherwise, they will ONLY be able to "perform" the listing options (visualize data).

How to set / run the project REMOTELY (using Render)

Database

  • on the Render website (after registration and login), access the options New > Postgres

  • follow the steps to create the database (free plan option)

  • after creating it, in the Connections section, write down the "Internal Database URL" and "External Database URL" for later use.

Code (locally)

  • install the modules: $ npm install

  • in the DATABASE_URL variable of the .env file, assign the "External Database URL" value.

  • [run once] execute migrations (it generates the database tables on the the database created on Render): $ npx prisma migrate deploy

Code (remotely)

  • upload the code to (create a repository for it on) GitHub

  • on the Render website, create a webservice, accessing the options New > Web Service

  • choose the respective repository

  • in the Language field, select Docker.

  • choose the Free instance type

  • in the Environment Variables section, copy and paste the the following information:

    PORT=3333 # or choose another
    
    DATABASE_URL=replace with the "Internal Database URL" value
    
    JWT_SECRET=[your_secret]
    
    JWT_EXPIRES_IN=5m
    
    # allowed URLs between commas or "" to allow all URLs
    CORS_ORIGIN=""
    
    # email of the users authorized to execute maintenance routes between commas
    MAINTENANCE_USERS_EMAILS=email1@email.com, email2@email.com
    
  • in the Advanced section, assign "/api/health" to the "Health Check Path" field

  • Click on "Deploy Web Service"

  • [info] The platform will deploy according to the instructions of the Dockerfile file.

  • If the deployment is successful, a message similar to the following will be displayed at the end of the Logs section of the Events option: "Your service is live".

  • Below this message, copy the URL that the platform created for the web service (similar to https://something.onrender.com)

Verifying if it's working

Registering users (authors), categories, publishers, and books via requests/routes (Postman)

  • download, install and open Postman

  • import the files *environment.json e *collection.json of the project folder postman

  • select the imported environment (section of the left panel "Items") and assign the URL copied above (eg.: https://something.onrender.com) to the variable API_BASE_URL

  • select the imported collection (section of the left panel "Items")

    • in the upper right corner, select the imported environment
  • returning to the collection previously selected (section of the left panel "Items"), execute (Send button) the "health check" request.

  • if successful execution (the request response status will be "200 OK"), only the first time (while the database is empty / doesn't have a single book registered), execute the following requests in this order:

    • folder users: POST create
    • folder sessions: POST create (login)
    • folder categories: POST create
    • folder publishers: POST create
    • folder books: POST create
    • [info]: For a registered user to be able to execute maintenance routes (POST, PUT, etc), their email must be assigned to the environment variable MAINTENANCE_USERS_EMAILS (in the .env file), and the API (backend) restarted; otherwise, they will ONLY be able to execute the listing (GET) routes.
  • execute the requests above and others as needed.

  • observation: if, after executing a request, the request response status is "401 unauthorized", execute the request "folder sessions: POST create (login)", and then execute that one request again.

  • example of the API in production: https://imersivo-100h-javascript-expert-full.onrender.com/api/health

About

Bookstore project - course/module 04 - REST API, testing and deployment, from the 100-hours Alura Javascript Immersion program

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages