Welcome to the Blog API project! This RESTful API application is designed to manage a blog with features including user authentication, post management, and comment handling. This README provides comprehensive instructions for setup, usage, and testing.
- User Management: Register, log in, and retrieve user profile information.
- Post Management: Create, read, update, and delete blog posts.
- Comment Management: Create, read, update, and delete comments on posts.
- Pagination: Implemented for retrieving lists of posts and comments.
- Search Functionality: Added to the comments retrieval endpoint for filtering by content.
- Authentication: JWT-based user authentication for secure access.
- Authorization: Ensures users can only modify their own posts and comments.
- Deployment: Dockerized application with deployment configurations for AWS EC2.
- Docker - For containerization.
- Docker Compose - For managing multi-container Docker applications.
- Go - For local development and building the application.
- Database Configuration:
-
Create a
.envfile in the root directory of the project. -
Add the following environment variables to configure your database:
DB_URI=mysql://username:password@tcp(localhost:3306)/ DB_NAME=testdb
-
-
Build Docker Image:
docker build -t blog-api -f blog-api.dockerfile . -
Run with Docker Compose:
docker-compose up --build
This command will start the application along with the database and other required services. Access the API at
http://localhost:80.
- Environment Variables: Configure required environment variables in the
.envfile or through your cloud provider's configuration settings.
-
Unit and Integration Tests:
Ensure your database is set up and accessible as per the configuration in your test environment.
go test ./...This command will run all tests, including those for CRUD operations and pagination functionality.
-
POST
/api/users/register- Register a new user.-
Request Body:
{ "username": "john_doe", "email": "john@example.com", "password": "securepassword" }
-
-
POST
/api/users/login- Authenticate user and receive a JWT token.-
Request Body:
{ "username": "john_doe", "password": "securepassword" } -
Response:
{ "status": 200, "message": "login successful", "data": { "authorization": "your generated token is eyJhbGciOiJIU......" } }
-
-
GET
/api/users/profile- Get user profile information (Authenticated).- Headers:
Authorization: token
- Headers:
-
GET
/api/posts- Retrieve all posts (Paginated).-
Example Request:
GET /api/posts?page=1&limit=10 -
Response:
{ "status": 200, "message": "success", "data": { "posts": [ { "id": 1, "title": "My first post", "content": "Once upon a time...", "authorId": 1, "createdAt": "2024-07-26 07:56:12", "updatedAt": "2024-07-26 07:56:12" }, .... ] }
-
-
GET
/api/posts/{id}- Retrieve a single post by ID. -
POST
/api/posts- Create a new post (Authenticated).-
Request Body:
{ "title": "My First Post", "content": "This is the content of the post." }
-
-
PUT
/api/posts/{id}- Update a post by ID (Authenticated & Author only).-
Request Body:
{ "title": "Updated Post Title", "content": "Updated content." }
-
-
DELETE
/api/posts/{id}- Delete a post by ID (Authenticated & Author only).
-
GET
/api/posts/{postId}/comments- Retrieve all comments for a post (Paginated).- Example Request:
GET /api/posts/1/comments?page=1&limit=10
- Example Request:
-
POST
/api/posts/{postId}/comments- Create a new comment on a post (Authenticated).-
Request Body:
{ "content": "Great post!" }
-
-
PUT
/api/comments/{id}- Update a comment by ID (Authenticated & Author only).-
Request Body:
{ "content": "Updated comment." }
-
-
DELETE
/api/comments/{id}- Delete a comment by ID (Authenticated & Author only).
- Pagination: Implemented for both posts and comments. Use query parameters
pageandlimitto control pagination. - Search: For post, you can use the
searchquery parameter to filter posts by content.
- Dockerfile:
blog-api.dockerfile- Defines the application image. Ensure it includes all dependencies and configurations. - Docker Compose:
docker-compose.yml- Manages the application and database services.
The application is configured to run on an AWS EC2 instance. For the binary application to run best on AWS, some changes would have to be made to both the app and on the server.
For a comprehensive set of tests and sample data, refer to the Postman collection linked below:
This collection includes example requests and responses for all API endpoints, which will help you verify and test the functionality of the API.
- Update the
.envfile or environment settings with the correct database URL and other configurations for different environments (development, staging, production). - For further clarity, please contact me at vuictory.agbabune@gmail.com.
This application was created in response to interview request from Intel Region.