/Api_C

Primary LanguageC

Rest Api in C

Docker used for Contanization

http://35.199.94.103/ link to test(temp)

@latest


Contents


About


This is a one week project where the objective is to create a Rest Api and a Cli using C and any supplemental library. This challange is an opportunity to take the first step towards the job market with 42.


HTTP Routes

Method Uri
/ GET
/pokemon GET
/pokemon POST
/pokemon/$(id) GET
/pokemon/$(id) PUT
/pokemon/$(id) DELETE

Tools and Technologies


Docker and Server instalation

The image is privated but in the future i wish to lauch it.

Make sure you have Docker installed in your environment

I'm not using docker-composer since this was a one week project, but i will implement it in the next project

Building Server

  • Images needed:

  • docker pull mysql
    docker pull ubuntu
    docker pull debian
    
  • Volumes:

  •   docker volume create --name Logs
      docker volume create --name Database-volume
    
  • Build:

  • 	docker build -t api ./Api
        docker build -t cli ./Cli
    	docker build -t my_sql ./Database
    
  • Network:

  • docker network create database_network
    
  • Run:

  •   docker run --network database_network -v Database-volume:/var/lib/mysql --name=mysql_container my_sql #wait set up then close the terminal and open again, or use -d flag and wait a minute to give it time to start the database
      docker run -ti -d  --network database_network --name=api_container -p 8000:8000 -v Logs:/Logs api
      #in the first time we set up api_container it can take 1 to 2 minutes since it will seed our database with some data from the Api https://pokeapi.co/ . Remove -d flag to see the proccess
    
  • Now the Api server is up and can be accessed throught localhost:8000

  • Try access localhost:8000/pokemon as a test. If the container connection stop it means the mysql doesn't finish its set up, just run docker start api_container again and the application will work normally

  • You can use docker commands to manage the containers

Simple Commands

  • docker stop [container]
    docker start [container]
    docker run -ti --rm --volumes-from api_container cli#open cli and navegate throught the logs
    

Api Fluxograma


Cli Fluxograma


Documentation

Host/Endpoint

Considere localhost:800 as the Host

  • http://localhost:8000/

    • Brief: Initial page
      Response:
        status 200
        json with welcome message
      
  • http://localhost:8000/pokemon

    • Brief: Returns in json all the pokemons stored into the database
      Response:(json)
      [
        {
          "id": 1,
          "gen": 1,
          "name": "Bulbasaur",
          "type": "Grass"
        },
         {
          "id": 2,
          "gen": 1,
          "name": "Charmeleon",
          "type": "Fire"
        },
        {
          "id": 3,
          "gen": 1,
          "name": "Blastoise",
          "type": "Water"
        }
        ...
        ...
        ...
      ]
      
  • http://localhost:8000/pokemon/1

    • Brief: Return a json array with the pokemon of the passed id,
      or status 404 if this pokemon doesn't exist in our database
      Response:(json)
      [
        {
          "id" : 1,
          "gen": 1,
          "name": "Bulbasaur",
          "type": "Grass"
        }
      ]
      
  • http://localhost:8000/pokemon

    •   Brief: Create a new pokemon into the database
      
        Request:(json)
        {
          "name": "my_new_pokemon",
          "type": "my_type",
          "gen": 3
        }
        Response:
            status 202 in success
            status 500 or 400 in case of error
      
  • http://localhost:8000/pokemon/1

    •   Brief: Update a pokemon of id with new values
      
        Request:(json)
        {
          "name": "my_new_pokemon",
          "type": "my_type",
          "gen": 1
        }
        Response:
           status 200 in success
           status 500 in case of error
      
  • http://localhost:8000/pokemon/1

    •   Brief: Delete pokemon of id
        Response:
           status 200 in success
           status 500 in case of error
      

Cli Documentation

Cli provides 3 interfaces

  1. Total Requests

  1. Show all Requests of a pair Method Uri

  1. Show information about an specific request

  • You can press [space] to see the menu helper
  • Navegate throught the table using w/s or up down
  • Select an item with [enter]
  • [backspace] to return to the previous page
  • [q] to exit the application


Collaborators


  • Gabriel (gsilva-v)
  • Leonardo (lfilipe-)

Bibliography


Notion Most part of the links used is inside notion