Backend services for ingritofood
IngritoFood -> See what recipes can be made from the ingredients you have at hand. Made with ReactJS and C++ backend and a TCP Server is written in C++ to communicate between the front end and the back end. Web-based virtual recipe book in which the user can append the ingredients they have on hand and the application parses through thousands of recipe
The backend of the application is responsible for handling the server-side operations, including communication with external APIs, processing client requests, and sending responses back to the client. This documentation provides an overview of the backend components, their functionality, and how they work together to deliver the required functionality.
The backend code is organized into several files:
-
CMakeLists.txt
: The CMake configuration file for building the backend project. -
get_receipe_by_id.h
: A header file containing thegetRecipeById
class, which provides functionality to retrieve recipe information by its ID from the Spoonacular API. -
get_receipe_by_ingri.h
: A header file containing thegetRecipeByIngri
class, which provides functionality to retrieve recipe information by ingredients from the Spoonacular API. -
http_tcpserver.h
: A header file defining theTcpServer
class, which implements a TCP server for handling client connections and processing HTTP requests. -
server.cpp
: The main entry point of the backend application, where theTcpServer
instance is created and started. -
http_tcpServer.cpp
: The implementation file for theTcpServer
class, which includes methods for starting the server, accepting connections, receiving and parsing client requests, and sending responses.
-
CMakeLists.txt
:- Purpose: This file is used by CMake to configure the build process for the backend project. It includes necessary dependencies, such as the
cpr
andnlohmann_json
libraries, and sets up the build targets. - Usage: This file should be placed in the root directory of the backend project and used with CMake to generate build files for the project.
- Purpose: This file is used by CMake to configure the build process for the backend project. It includes necessary dependencies, such as the
-
get_receipe_by_id.h
:- Purpose: This header file defines the
getRecipeById
class, which encapsulates the functionality to retrieve recipe information by its ID from the Spoonacular API. - Functionality: The
getRecipeById
class provides a methodmakeReq
that takes a recipe ID as input, constructs the API request URL, and performs a GET request to the Spoonacular API. It handles error cases and returns the response text. - Usage: To use this functionality, include the
get_receipe_by_id.h
header file in the desired source file and create an instance of thegetRecipeById
class. Call themakeReq
method with the recipe ID to retrieve the recipe information.
- Purpose: This header file defines the
-
get_receipe_by_ingri.h
:- Purpose: This header file defines the
getRecipeByIngri
class, which provides functionality to retrieve recipe information by ingredients from the Spoonacular API. - Functionality: The
getRecipeByIngri
class includes a static methodmakeReq
that takes a list of ingredients as input, constructs the API request URL, and performs a GET request to the Spoonacular API. It handles error cases and returns the response text. - Usage: To use this functionality, include the
get_receipe_by_ingri.h
header file in the desired source file. Call the static methodmakeReq
of thegetRecipeByIngri
class, passing the list of ingredients as a parameter, to retrieve the recipe information.
- Purpose: This header file defines the
-
http_tcpserver.h
:- Purpose: This header file defines the
TcpServer
class, which implements a TCP server for handling client connections and processing HTTP requests. - Functionality: The
TcpServer
class includes methods for starting the server, accepting connections, receiving and parsing client requests, and sending responses. It uses the Winsock library for network communication. - Usage: To use this class, include the
http_tcpserver.h
header file in the desired source file. Create an instance of theTcpServer
class, providing the IP address and port number to listen on. Call thestartListen
method to start the server and handle incoming client connections.
- Purpose: This header file defines the
-
server.cpp
:- Purpose: This file contains the main entry point of the backend application. It creates an instance of the
TcpServer
class and starts the server to listen for incoming client connections. - Usage: Compile and run this file to start the backend server. Make sure to include the necessary dependencies and configure the project build correctly.
- Purpose: This file contains the main entry point of the backend application. It creates an instance of the
-
http_tcpServer.cpp
:- Purpose: This implementation file provides the implementation of the methods defined in the
TcpServer
class. - Functionality: The
TcpServer
class includes methods for starting the server, accepting connections, receiving and parsing client requests, and sending responses. ThebuildResponse
method constructs the HTTP response based on the client request, and thesendResponse
method sends the response back to the client. - Usage: This file is already included in the backend project. It is automatically compiled and linked when building the project.
- Purpose: This implementation file provides the implementation of the methods defined in the
-
Clone the Repository:
-
Open a terminal or command prompt.
-
Navigate to the directory where you want to clone the repository.
-
Run the following command to clone the repository:
git clone https://github.com/louremipsum/ingritofood-backend.git
-
-
Install Dependencies:
-
Ensure that you have CMake installed on your system. If not, download and install it from the official CMake website (https://cmake.org/).
-
Navigate to the root directory of the cloned repository.
-
Create a build directory:
mkdir build cd build
-
Run CMake to generate the build files:
cmake ..
-
This will configure the build process based on the
CMakeLists.txt
file.
-
-
Build the Project:
-
After running CMake, you can now build the project.
-
Execute the following command:
cmake --build .
-
This will build the backend project and generate the executable file.
-
-
Run the Backend Server:
-
Once the build process is complete, you can run the backend server.
-
Execute the following command:
./server
-
The server will start listening for incoming client connections.
-
-
Interact with the Backend:
- With the backend server running, you can now interact with it.
- Open a web browser or use an API testing tool (e.g., Postman) to send HTTP requests to the server's IP address and port number.
- The server will receive the requests, process them, and send back appropriate responses.
The backend server exposes the following API endpoints:
GET /recipe/{id}
: Retrieves a recipe by its ID.GET /recipes?ingredients={ingredients}
: Retrieves recipes based on specified ingredients.
Get Recipe by ID
GET /recipe/12345
Get Recipes by Ingredients
GET /recipes?ingredients=apple,banana
You can check it out in the frontend part too.
That's it! You have now set up the repository and run the backend server. You can start testing and integrating the backend with your application.
The backend of the application consists of several components working together to handle server-side operations. The TcpServer
class acts as the server, processing client connections and HTTP requests. The getRecipeById
and getRecipeByIngri
classes provide the functionality to retrieve recipe information from the Spoonacular API based on recipe IDs and ingredients, respectively. By understanding the purpose, functionality, and usage of these components, you can effectively utilize and extend the backend to meet your application requirements.