This is a simple C++ web application that fetches and displays random cat facts. It uses the Crow web framework for C++ to create a web server and the C++ REST SDK (cpprestsdk) to make HTTP requests to an external API.
This project relies on the following libraries:
The project uses vcpkg for managing these dependencies.
Make sure you have vcpkg installed and integrated with your development environment.
vcpkg install crow cpprestsdk
git clone https://github.com/yourusername/cpp-quote-app.git
cd cpp-quote-app
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=[path to vcpkg]/scripts/buildsystems/vcpkg.cmake
Replace [path to vcpkg] with the actual path to your vcpkg installation.
cmake --build build --config Release
After building the project, you can run the executable:
.\build\Release\quote_app.exe # On Windows
./build/quote_app # On Unix-like systems
The application will start a web server on http://localhost:18080. Open this URL in your web browser to see a random cat fact. Click the “Get New Fact” button to fetch and display a new fact.
The project consists of a single main.cpp file that contains all the application logic. Here’s a breakdown of its structure:
Header Inclusions: The file includes necessary headers for Crow, cpprestsdk, and standard C++ libraries.
fetchCatFact Function: This function uses cpprestsdk to make an HTTP GET request to the Cat Fact Ninja API. It handles the response asynchronously and extracts the cat fact from the JSON response.
generateHtml Function: This function creates an HTML template with embedded CSS for styling. It takes a cat fact as input and inserts it into the HTML structure.
Main Function:
The application uses modern C++ features and asynchronous programming to handle HTTP requests efficiently.
Caching: Implement a caching mechanism to store recently fetched cat facts, reducing API calls and improving response times.
Error Handling: Add more robust error handling for API requests and display user-friendly error messages.
Multiple Facts: Allow users to specify the number of facts they want to see at once.
Categories: If the API supports it, add an option for users to choose fact categories (e.g., behavior, history, science).
Styling Options: Allow users to customize the appearance of the page (e.g., dark mode, different color schemes).
Fact Saving: Implement a feature to let users save their favorite facts.
Share Feature: Add buttons to easily share facts on social media platforms.
Localization: Add support for multiple languages, both in the UI and potentially fetching translated facts if available.
Rate Limiting: Implement rate limiting to prevent abuse of the API and your server resources.
Metrics: Add logging and metrics to track usage patterns and performance.
Unit Tests: Develop a suite of unit tests to ensure reliability as the application grows.
Containerization: Create a Dockerfile to containerize the application for easier deployment and scaling.
If you encounter any issues:
Contributions are welcome! Please feel free to submit a Pull Request.
This project is open source and available under the MIT License.