Masterblog API is a simple RESTful API built with Flask that allows you to manage blog posts and comments. This project includes features to create, read, update, delete posts, and add comments to posts. The backend is complemented by a frontend application that interacts with the API to display posts and comments, and allows users to add new posts and comments.
- Create, Read, Update, Delete (CRUD) Posts
- Add Comments to Posts
- Sort and Paginate Posts
- Rate Limiting
- Cross-Origin Resource Sharing (CORS) Enabled
- Flask: Python web framework for building the API.
- Flask-Limiter: To apply rate limiting to API endpoints.
- Flask-CORS: To handle cross-origin requests.
- JavaScript: For frontend interactions and dynamic content loading.
- Python 3.x: Ensure Python 3 is installed on your machine.
- Pip: Python package installer.
-
Clone the repository:
git clone https://github.com/yourusername/blog-api.git cd blog-api -
Create a virtual environment (optional but recommended):
python -m venv venv
-
Activate the virtual environment:
-
On Windows:
venv\Scripts\activate
-
On macOS/Linux:
source venv/bin/activate
-
-
Install the required dependencies:
pip install -r requirements.txt
-
Create a
posts.jsonfile in the root directory (if it doesn't exist) with the following content:[]
-
Start the Flask server:
python app.py
The server will run on
http://127.0.0.1:5002.
-
Open
index.htmlin a web browser. Ensure that the API base URL is set correctly in the browser’s local storage. -
Use the interface to add, view, update, and delete posts, as well as add comments.
- GET
/api/posts: Retrieve a list of posts. - POST
/api/posts: Create a new post. - GET
/api/posts/<post_id>: Retrieve a specific post. - PUT
/api/posts/<post_id>: Update a specific post. - DELETE
/api/posts/<post_id>: Delete a specific post. - POST
/api/posts/<post_id>/comments: Add a comment to a specific post. - GET
/api/posts/search: Search for posts by title or content.
-
Add a New Post:
curl -X POST http://127.0.0.1:5002/api/posts -H "Content-Type: application/json" -d '{"title": "My First Post", "content": "This is the content of my first post."}'
-
Add a Comment:
curl -X POST http://127.0.0.1:5002/api/posts/1/comments -H "Content-Type: application/json" -d '{"comment": "Great post!"}'
If you'd like to contribute to this project, please fork the repository and submit a pull request with your changes. Be sure to follow these steps:
- Fork the repository.
- Create a feature branch.
- Commit your changes.
- Push to the branch.
- Open a pull request.